]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
729e4ab9 A |
3 | /* |
4 | ******************************************************************************* | |
2ca993e8 | 5 | * Copyright (C) 2010-2016, International Business Machines Corporation and |
4388f060 | 6 | * others. All Rights Reserved. |
729e4ab9 A |
7 | ******************************************************************************* |
8 | */ | |
9 | ||
10 | #include "unicode/utypes.h" | |
11 | ||
12 | #if !UCONFIG_NO_FORMATTING | |
13 | ||
14 | #include "unicode/locdspnm.h" | |
2ca993e8 | 15 | #include "unicode/simpleformatter.h" |
f3c0d7a5 | 16 | #include "unicode/ucasemap.h" |
51004dcb | 17 | #include "unicode/ures.h" |
2ca993e8 | 18 | #include "unicode/udisplaycontext.h" |
51004dcb | 19 | #include "unicode/brkiter.h" |
2ca993e8 | 20 | #include "unicode/ucurr.h" |
729e4ab9 A |
21 | #include "cmemory.h" |
22 | #include "cstring.h" | |
b331163b | 23 | #include "mutex.h" |
729e4ab9 | 24 | #include "ulocimp.h" |
b331163b | 25 | #include "umutex.h" |
729e4ab9 | 26 | #include "ureslocs.h" |
51004dcb | 27 | #include "uresimp.h" |
729e4ab9 A |
28 | |
29 | #include <stdarg.h> | |
30 | ||
31 | /** | |
32 | * Concatenate a number of null-terminated strings to buffer, leaving a | |
33 | * null-terminated string. The last argument should be the null pointer. | |
34 | * Return the length of the string in the buffer, not counting the trailing | |
35 | * null. Return -1 if there is an error (buffer is null, or buflen < 1). | |
36 | */ | |
37 | static int32_t ncat(char *buffer, uint32_t buflen, ...) { | |
38 | va_list args; | |
39 | char *str; | |
40 | char *p = buffer; | |
41 | const char* e = buffer + buflen - 1; | |
42 | ||
43 | if (buffer == NULL || buflen < 1) { | |
44 | return -1; | |
45 | } | |
46 | ||
47 | va_start(args, buflen); | |
3d1f044b | 48 | while ((str = va_arg(args, char *)) != 0) { |
729e4ab9 | 49 | char c; |
3d1f044b | 50 | while (p != e && (c = *str++) != 0) { |
729e4ab9 A |
51 | *p++ = c; |
52 | } | |
53 | } | |
54 | *p = 0; | |
55 | va_end(args); | |
56 | ||
0f5d89e8 | 57 | return static_cast<int32_t>(p - buffer); |
729e4ab9 A |
58 | } |
59 | ||
60 | U_NAMESPACE_BEGIN | |
61 | ||
62 | //////////////////////////////////////////////////////////////////////////////////////////////////// | |
63 | ||
64 | // Access resource data for locale components. | |
65 | // Wrap code in uloc.c for now. | |
66 | class ICUDataTable { | |
51004dcb A |
67 | const char* path; |
68 | Locale locale; | |
729e4ab9 A |
69 | |
70 | public: | |
51004dcb A |
71 | ICUDataTable(const char* path, const Locale& locale); |
72 | ~ICUDataTable(); | |
729e4ab9 | 73 | |
51004dcb | 74 | const Locale& getLocale(); |
729e4ab9 | 75 | |
51004dcb A |
76 | UnicodeString& get(const char* tableKey, const char* itemKey, |
77 | UnicodeString& result) const; | |
78 | UnicodeString& get(const char* tableKey, const char* subTableKey, const char* itemKey, | |
79 | UnicodeString& result) const; | |
729e4ab9 | 80 | |
51004dcb A |
81 | UnicodeString& getNoFallback(const char* tableKey, const char* itemKey, |
82 | UnicodeString &result) const; | |
83 | UnicodeString& getNoFallback(const char* tableKey, const char* subTableKey, const char* itemKey, | |
84 | UnicodeString &result) const; | |
729e4ab9 A |
85 | }; |
86 | ||
87 | inline UnicodeString & | |
88 | ICUDataTable::get(const char* tableKey, const char* itemKey, UnicodeString& result) const { | |
51004dcb | 89 | return get(tableKey, NULL, itemKey, result); |
729e4ab9 A |
90 | } |
91 | ||
92 | inline UnicodeString & | |
93 | ICUDataTable::getNoFallback(const char* tableKey, const char* itemKey, UnicodeString& result) const { | |
51004dcb | 94 | return getNoFallback(tableKey, NULL, itemKey, result); |
729e4ab9 A |
95 | } |
96 | ||
97 | ICUDataTable::ICUDataTable(const char* path, const Locale& locale) | |
51004dcb | 98 | : path(NULL), locale(Locale::getRoot()) |
729e4ab9 A |
99 | { |
100 | if (path) { | |
3d1f044b | 101 | int32_t len = static_cast<int32_t>(uprv_strlen(path)); |
729e4ab9 A |
102 | this->path = (const char*) uprv_malloc(len + 1); |
103 | if (this->path) { | |
104 | uprv_strcpy((char *)this->path, path); | |
105 | this->locale = locale; | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
110 | ICUDataTable::~ICUDataTable() { | |
111 | if (path) { | |
112 | uprv_free((void*) path); | |
113 | path = NULL; | |
114 | } | |
115 | } | |
116 | ||
117 | const Locale& | |
118 | ICUDataTable::getLocale() { | |
119 | return locale; | |
120 | } | |
121 | ||
122 | UnicodeString & | |
123 | ICUDataTable::get(const char* tableKey, const char* subTableKey, const char* itemKey, | |
124 | UnicodeString &result) const { | |
125 | UErrorCode status = U_ZERO_ERROR; | |
126 | int32_t len = 0; | |
127 | ||
128 | const UChar *s = uloc_getTableStringWithFallback(path, locale.getName(), | |
129 | tableKey, subTableKey, itemKey, | |
130 | &len, &status); | |
4388f060 | 131 | if (U_SUCCESS(status) && len > 0) { |
729e4ab9 A |
132 | return result.setTo(s, len); |
133 | } | |
134 | return result.setTo(UnicodeString(itemKey, -1, US_INV)); | |
135 | } | |
136 | ||
137 | UnicodeString & | |
138 | ICUDataTable::getNoFallback(const char* tableKey, const char* subTableKey, const char* itemKey, | |
139 | UnicodeString& result) const { | |
140 | UErrorCode status = U_ZERO_ERROR; | |
141 | int32_t len = 0; | |
142 | ||
143 | const UChar *s = uloc_getTableStringWithFallback(path, locale.getName(), | |
144 | tableKey, subTableKey, itemKey, | |
145 | &len, &status); | |
146 | if (U_SUCCESS(status)) { | |
147 | return result.setTo(s, len); | |
148 | } | |
149 | ||
150 | result.setToBogus(); | |
151 | return result; | |
152 | } | |
153 | ||
154 | //////////////////////////////////////////////////////////////////////////////////////////////////// | |
155 | ||
4388f060 A |
156 | LocaleDisplayNames::~LocaleDisplayNames() {} |
157 | ||
729e4ab9 A |
158 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
159 | ||
160 | #if 0 // currently unused | |
161 | ||
162 | class DefaultLocaleDisplayNames : public LocaleDisplayNames { | |
163 | UDialectHandling dialectHandling; | |
164 | ||
165 | public: | |
166 | // constructor | |
167 | DefaultLocaleDisplayNames(UDialectHandling dialectHandling); | |
168 | ||
169 | virtual ~DefaultLocaleDisplayNames(); | |
170 | ||
171 | virtual const Locale& getLocale() const; | |
172 | virtual UDialectHandling getDialectHandling() const; | |
51004dcb | 173 | |
729e4ab9 A |
174 | virtual UnicodeString& localeDisplayName(const Locale& locale, |
175 | UnicodeString& result) const; | |
176 | virtual UnicodeString& localeDisplayName(const char* localeId, | |
177 | UnicodeString& result) const; | |
178 | virtual UnicodeString& languageDisplayName(const char* lang, | |
179 | UnicodeString& result) const; | |
180 | virtual UnicodeString& scriptDisplayName(const char* script, | |
181 | UnicodeString& result) const; | |
182 | virtual UnicodeString& scriptDisplayName(UScriptCode scriptCode, | |
183 | UnicodeString& result) const; | |
184 | virtual UnicodeString& regionDisplayName(const char* region, | |
185 | UnicodeString& result) const; | |
186 | virtual UnicodeString& variantDisplayName(const char* variant, | |
187 | UnicodeString& result) const; | |
188 | virtual UnicodeString& keyDisplayName(const char* key, | |
189 | UnicodeString& result) const; | |
190 | virtual UnicodeString& keyValueDisplayName(const char* key, | |
191 | const char* value, | |
192 | UnicodeString& result) const; | |
193 | }; | |
194 | ||
195 | DefaultLocaleDisplayNames::DefaultLocaleDisplayNames(UDialectHandling dialectHandling) | |
196 | : dialectHandling(dialectHandling) { | |
197 | } | |
198 | ||
199 | DefaultLocaleDisplayNames::~DefaultLocaleDisplayNames() { | |
200 | } | |
201 | ||
202 | const Locale& | |
203 | DefaultLocaleDisplayNames::getLocale() const { | |
204 | return Locale::getRoot(); | |
205 | } | |
206 | ||
207 | UDialectHandling | |
208 | DefaultLocaleDisplayNames::getDialectHandling() const { | |
209 | return dialectHandling; | |
210 | } | |
211 | ||
212 | UnicodeString& | |
213 | DefaultLocaleDisplayNames::localeDisplayName(const Locale& locale, | |
214 | UnicodeString& result) const { | |
215 | return result = UnicodeString(locale.getName(), -1, US_INV); | |
216 | } | |
217 | ||
218 | UnicodeString& | |
219 | DefaultLocaleDisplayNames::localeDisplayName(const char* localeId, | |
220 | UnicodeString& result) const { | |
221 | return result = UnicodeString(localeId, -1, US_INV); | |
222 | } | |
223 | ||
224 | UnicodeString& | |
225 | DefaultLocaleDisplayNames::languageDisplayName(const char* lang, | |
226 | UnicodeString& result) const { | |
227 | return result = UnicodeString(lang, -1, US_INV); | |
228 | } | |
229 | ||
230 | UnicodeString& | |
231 | DefaultLocaleDisplayNames::scriptDisplayName(const char* script, | |
232 | UnicodeString& result) const { | |
233 | return result = UnicodeString(script, -1, US_INV); | |
234 | } | |
235 | ||
236 | UnicodeString& | |
237 | DefaultLocaleDisplayNames::scriptDisplayName(UScriptCode scriptCode, | |
238 | UnicodeString& result) const { | |
239 | const char* name = uscript_getName(scriptCode); | |
240 | if (name) { | |
241 | return result = UnicodeString(name, -1, US_INV); | |
242 | } | |
243 | return result.remove(); | |
244 | } | |
245 | ||
246 | UnicodeString& | |
247 | DefaultLocaleDisplayNames::regionDisplayName(const char* region, | |
248 | UnicodeString& result) const { | |
249 | return result = UnicodeString(region, -1, US_INV); | |
250 | } | |
251 | ||
252 | UnicodeString& | |
253 | DefaultLocaleDisplayNames::variantDisplayName(const char* variant, | |
254 | UnicodeString& result) const { | |
255 | return result = UnicodeString(variant, -1, US_INV); | |
256 | } | |
257 | ||
258 | UnicodeString& | |
259 | DefaultLocaleDisplayNames::keyDisplayName(const char* key, | |
260 | UnicodeString& result) const { | |
261 | return result = UnicodeString(key, -1, US_INV); | |
262 | } | |
263 | ||
264 | UnicodeString& | |
265 | DefaultLocaleDisplayNames::keyValueDisplayName(const char* /* key */, | |
266 | const char* value, | |
267 | UnicodeString& result) const { | |
268 | return result = UnicodeString(value, -1, US_INV); | |
269 | } | |
270 | ||
271 | #endif // currently unused class DefaultLocaleDisplayNames | |
272 | ||
273 | //////////////////////////////////////////////////////////////////////////////////////////////////// | |
274 | ||
275 | class LocaleDisplayNamesImpl : public LocaleDisplayNames { | |
51004dcb A |
276 | Locale locale; |
277 | UDialectHandling dialectHandling; | |
278 | ICUDataTable langData; | |
279 | ICUDataTable regionData; | |
2ca993e8 A |
280 | SimpleFormatter separatorFormat; |
281 | SimpleFormatter format; | |
282 | SimpleFormatter keyTypeFormat; | |
51004dcb | 283 | UDisplayContext capitalizationContext; |
f3c0d7a5 | 284 | #if !UCONFIG_NO_BREAK_ITERATION |
2ca993e8 | 285 | BreakIterator* capitalizationBrkIter; |
f3c0d7a5 A |
286 | #else |
287 | UObject* capitalizationBrkIter; | |
288 | #endif | |
57a6839d A |
289 | UnicodeString formatOpenParen; |
290 | UnicodeString formatReplaceOpenParen; | |
291 | UnicodeString formatCloseParen; | |
292 | UnicodeString formatReplaceCloseParen; | |
3d1f044b | 293 | UnicodeString formatParenCloseOpen; |
57a6839d | 294 | UDisplayContext nameLength; |
340931cb | 295 | UDisplayContext substitute; |
51004dcb A |
296 | |
297 | // Constants for capitalization context usage types. | |
298 | enum CapContextUsage { | |
299 | kCapContextUsageLanguage, | |
300 | kCapContextUsageScript, | |
301 | kCapContextUsageTerritory, | |
302 | kCapContextUsageVariant, | |
303 | kCapContextUsageKey, | |
57a6839d | 304 | kCapContextUsageKeyValue, |
51004dcb A |
305 | kCapContextUsageCount |
306 | }; | |
57a6839d A |
307 | // Capitalization transforms. For each usage type, indicates whether to titlecase for |
308 | // the context specified in capitalizationContext (which we know at construction time) | |
309 | UBool fCapitalization[kCapContextUsageCount]; | |
729e4ab9 A |
310 | |
311 | public: | |
51004dcb A |
312 | // constructor |
313 | LocaleDisplayNamesImpl(const Locale& locale, UDialectHandling dialectHandling); | |
314 | LocaleDisplayNamesImpl(const Locale& locale, UDisplayContext *contexts, int32_t length); | |
315 | virtual ~LocaleDisplayNamesImpl(); | |
316 | ||
317 | virtual const Locale& getLocale() const; | |
318 | virtual UDialectHandling getDialectHandling() const; | |
319 | virtual UDisplayContext getContext(UDisplayContextType type) const; | |
320 | ||
321 | virtual UnicodeString& localeDisplayName(const Locale& locale, | |
322 | UnicodeString& result) const; | |
323 | virtual UnicodeString& localeDisplayName(const char* localeId, | |
324 | UnicodeString& result) const; | |
325 | virtual UnicodeString& languageDisplayName(const char* lang, | |
326 | UnicodeString& result) const; | |
327 | virtual UnicodeString& scriptDisplayName(const char* script, | |
328 | UnicodeString& result) const; | |
329 | virtual UnicodeString& scriptDisplayName(UScriptCode scriptCode, | |
330 | UnicodeString& result) const; | |
331 | virtual UnicodeString& regionDisplayName(const char* region, | |
332 | UnicodeString& result) const; | |
333 | virtual UnicodeString& variantDisplayName(const char* variant, | |
334 | UnicodeString& result) const; | |
335 | virtual UnicodeString& keyDisplayName(const char* key, | |
336 | UnicodeString& result) const; | |
337 | virtual UnicodeString& keyValueDisplayName(const char* key, | |
338 | const char* value, | |
339 | UnicodeString& result) const; | |
729e4ab9 | 340 | private: |
51004dcb | 341 | UnicodeString& localeIdName(const char* localeId, |
340931cb | 342 | UnicodeString& result, bool substitute) const; |
2ca993e8 A |
343 | UnicodeString& regionShortDisplayName(const char* region, // Apple-specific |
344 | UnicodeString& result) const; | |
51004dcb A |
345 | UnicodeString& appendWithSep(UnicodeString& buffer, const UnicodeString& src) const; |
346 | UnicodeString& adjustForUsageAndContext(CapContextUsage usage, UnicodeString& result) const; | |
2ca993e8 A |
347 | UnicodeString& scriptDisplayName(const char* script, UnicodeString& result, UBool skipAdjust) const; |
348 | UnicodeString& regionDisplayName(const char* region, UnicodeString& result, UBool skipAdjust) const; | |
349 | UnicodeString& variantDisplayName(const char* variant, UnicodeString& result, UBool skipAdjust) const; | |
350 | UnicodeString& keyDisplayName(const char* key, UnicodeString& result, UBool skipAdjust) const; | |
351 | UnicodeString& keyValueDisplayName(const char* key, const char* value, | |
352 | UnicodeString& result, UBool skipAdjust) const; | |
51004dcb | 353 | void initialize(void); |
f3c0d7a5 A |
354 | |
355 | struct CapitalizationContextSink; | |
729e4ab9 A |
356 | }; |
357 | ||
358 | LocaleDisplayNamesImpl::LocaleDisplayNamesImpl(const Locale& locale, | |
359 | UDialectHandling dialectHandling) | |
51004dcb A |
360 | : dialectHandling(dialectHandling) |
361 | , langData(U_ICUDATA_LANG, locale) | |
362 | , regionData(U_ICUDATA_REGION, locale) | |
51004dcb | 363 | , capitalizationContext(UDISPCTX_CAPITALIZATION_NONE) |
57a6839d | 364 | , capitalizationBrkIter(NULL) |
b331163b | 365 | , nameLength(UDISPCTX_LENGTH_FULL) |
340931cb | 366 | , substitute(UDISPCTX_SUBSTITUTE) |
729e4ab9 | 367 | { |
51004dcb A |
368 | initialize(); |
369 | } | |
729e4ab9 | 370 | |
51004dcb A |
371 | LocaleDisplayNamesImpl::LocaleDisplayNamesImpl(const Locale& locale, |
372 | UDisplayContext *contexts, int32_t length) | |
373 | : dialectHandling(ULDN_STANDARD_NAMES) | |
374 | , langData(U_ICUDATA_LANG, locale) | |
375 | , regionData(U_ICUDATA_REGION, locale) | |
51004dcb | 376 | , capitalizationContext(UDISPCTX_CAPITALIZATION_NONE) |
57a6839d | 377 | , capitalizationBrkIter(NULL) |
b331163b | 378 | , nameLength(UDISPCTX_LENGTH_FULL) |
340931cb | 379 | , substitute(UDISPCTX_SUBSTITUTE) |
51004dcb A |
380 | { |
381 | while (length-- > 0) { | |
382 | UDisplayContext value = *contexts++; | |
383 | UDisplayContextType selector = (UDisplayContextType)((uint32_t)value >> 8); | |
384 | switch (selector) { | |
385 | case UDISPCTX_TYPE_DIALECT_HANDLING: | |
386 | dialectHandling = (UDialectHandling)value; | |
387 | break; | |
388 | case UDISPCTX_TYPE_CAPITALIZATION: | |
389 | capitalizationContext = value; | |
390 | break; | |
b331163b | 391 | case UDISPCTX_TYPE_DISPLAY_LENGTH: |
57a6839d A |
392 | nameLength = value; |
393 | break; | |
340931cb A |
394 | case UDISPCTX_TYPE_SUBSTITUTE_HANDLING: |
395 | substitute = value; | |
396 | break; | |
b331163b A |
397 | case UADISPCTX_TYPE_LENGTH: // Apple-specific |
398 | nameLength = (value == UADISPCTX_LENGTH_SHORT)? UDISPCTX_LENGTH_SHORT: UDISPCTX_LENGTH_FULL; | |
399 | break; | |
51004dcb A |
400 | default: |
401 | break; | |
402 | } | |
403 | } | |
404 | initialize(); | |
405 | } | |
729e4ab9 | 406 | |
f3c0d7a5 A |
407 | struct LocaleDisplayNamesImpl::CapitalizationContextSink : public ResourceSink { |
408 | UBool hasCapitalizationUsage; | |
409 | LocaleDisplayNamesImpl& parent; | |
410 | ||
411 | CapitalizationContextSink(LocaleDisplayNamesImpl& _parent) | |
412 | : hasCapitalizationUsage(FALSE), parent(_parent) {} | |
413 | virtual ~CapitalizationContextSink(); | |
414 | ||
415 | virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/, | |
416 | UErrorCode &errorCode) { | |
417 | ResourceTable contexts = value.getTable(errorCode); | |
418 | if (U_FAILURE(errorCode)) { return; } | |
419 | for (int i = 0; contexts.getKeyAndValue(i, key, value); ++i) { | |
420 | ||
421 | CapContextUsage usageEnum; | |
422 | if (uprv_strcmp(key, "key") == 0) { | |
423 | usageEnum = kCapContextUsageKey; | |
424 | } else if (uprv_strcmp(key, "keyValue") == 0) { | |
425 | usageEnum = kCapContextUsageKeyValue; | |
426 | } else if (uprv_strcmp(key, "languages") == 0) { | |
427 | usageEnum = kCapContextUsageLanguage; | |
428 | } else if (uprv_strcmp(key, "script") == 0) { | |
429 | usageEnum = kCapContextUsageScript; | |
430 | } else if (uprv_strcmp(key, "territory") == 0) { | |
431 | usageEnum = kCapContextUsageTerritory; | |
432 | } else if (uprv_strcmp(key, "variant") == 0) { | |
433 | usageEnum = kCapContextUsageVariant; | |
434 | } else { | |
435 | continue; | |
436 | } | |
437 | ||
438 | int32_t len = 0; | |
439 | const int32_t* intVector = value.getIntVector(len, errorCode); | |
440 | if (U_FAILURE(errorCode)) { return; } | |
441 | if (len < 2) { continue; } | |
442 | ||
443 | int32_t titlecaseInt = (parent.capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU) ? intVector[0] : intVector[1]; | |
444 | if (titlecaseInt == 0) { continue; } | |
445 | ||
446 | parent.fCapitalization[usageEnum] = TRUE; | |
447 | hasCapitalizationUsage = TRUE; | |
448 | } | |
449 | } | |
450 | }; | |
451 | ||
452 | // Virtual destructors must be defined out of line. | |
453 | LocaleDisplayNamesImpl::CapitalizationContextSink::~CapitalizationContextSink() {} | |
454 | ||
51004dcb A |
455 | void |
456 | LocaleDisplayNamesImpl::initialize(void) { | |
457 | LocaleDisplayNamesImpl *nonConstThis = (LocaleDisplayNamesImpl *)this; | |
458 | nonConstThis->locale = langData.getLocale() == Locale::getRoot() | |
459 | ? regionData.getLocale() | |
460 | : langData.getLocale(); | |
4388f060 | 461 | |
57a6839d | 462 | UnicodeString sep; |
51004dcb A |
463 | langData.getNoFallback("localeDisplayPattern", "separator", sep); |
464 | if (sep.isBogus()) { | |
57a6839d | 465 | sep = UnicodeString("{0}, {1}", -1, US_INV); |
51004dcb | 466 | } |
57a6839d | 467 | UErrorCode status = U_ZERO_ERROR; |
2ca993e8 | 468 | separatorFormat.applyPatternMinMaxArguments(sep, 2, 2, status); |
51004dcb A |
469 | |
470 | UnicodeString pattern; | |
471 | langData.getNoFallback("localeDisplayPattern", "pattern", pattern); | |
472 | if (pattern.isBogus()) { | |
473 | pattern = UnicodeString("{0} ({1})", -1, US_INV); | |
474 | } | |
2ca993e8 | 475 | format.applyPatternMinMaxArguments(pattern, 2, 2, status); |
57a6839d A |
476 | if (pattern.indexOf((UChar)0xFF08) >= 0) { |
477 | formatOpenParen.setTo((UChar)0xFF08); // fullwidth ( | |
478 | formatReplaceOpenParen.setTo((UChar)0xFF3B); // fullwidth [ | |
479 | formatCloseParen.setTo((UChar)0xFF09); // fullwidth ) | |
480 | formatReplaceCloseParen.setTo((UChar)0xFF3D); // fullwidth ] | |
3d1f044b A |
481 | formatParenCloseOpen.setTo(u")(", 2); // fullwidth FF09 FF08 redundant parens |
482 | ||
57a6839d A |
483 | } else { |
484 | formatOpenParen.setTo((UChar)0x0028); // ( | |
485 | formatReplaceOpenParen.setTo((UChar)0x005B); // [ | |
486 | formatCloseParen.setTo((UChar)0x0029); // ) | |
487 | formatReplaceCloseParen.setTo((UChar)0x005D); // ] | |
3d1f044b | 488 | formatParenCloseOpen.setTo(u") (", 3); // halfwidth redundant parens |
57a6839d | 489 | } |
51004dcb A |
490 | |
491 | UnicodeString ktPattern; | |
492 | langData.get("localeDisplayPattern", "keyTypePattern", ktPattern); | |
493 | if (ktPattern.isBogus()) { | |
494 | ktPattern = UnicodeString("{0}={1}", -1, US_INV); | |
495 | } | |
2ca993e8 | 496 | keyTypeFormat.applyPatternMinMaxArguments(ktPattern, 2, 2, status); |
51004dcb A |
497 | |
498 | uprv_memset(fCapitalization, 0, sizeof(fCapitalization)); | |
499 | #if !UCONFIG_NO_BREAK_ITERATION | |
57a6839d A |
500 | // Only get the context data if we need it! This is a const object so we know now... |
501 | // Also check whether we will need a break iterator (depends on the data) | |
502 | UBool needBrkIter = FALSE; | |
503 | if (capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU || capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_STANDALONE) { | |
f3c0d7a5 A |
504 | LocalUResourceBundlePointer resource(ures_open(NULL, locale.getName(), &status)); |
505 | if (U_FAILURE(status)) { return; } | |
506 | CapitalizationContextSink sink(*this); | |
507 | ures_getAllItemsWithFallback(resource.getAlias(), "contextTransforms", sink, status); | |
508 | if (status == U_MISSING_RESOURCE_ERROR) { | |
509 | // Silently ignore. Not every locale has contextTransforms. | |
510 | status = U_ZERO_ERROR; | |
511 | } else if (U_FAILURE(status)) { | |
512 | return; | |
57a6839d | 513 | } |
f3c0d7a5 | 514 | needBrkIter = sink.hasCapitalizationUsage; |
57a6839d A |
515 | } |
516 | // Get a sentence break iterator if we will need it | |
517 | if (needBrkIter || capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE) { | |
518 | status = U_ZERO_ERROR; | |
519 | capitalizationBrkIter = BreakIterator::createSentenceInstance(locale, status); | |
520 | if (U_FAILURE(status)) { | |
521 | delete capitalizationBrkIter; | |
522 | capitalizationBrkIter = NULL; | |
51004dcb | 523 | } |
51004dcb A |
524 | } |
525 | #endif | |
729e4ab9 A |
526 | } |
527 | ||
528 | LocaleDisplayNamesImpl::~LocaleDisplayNamesImpl() { | |
f3c0d7a5 | 529 | #if !UCONFIG_NO_BREAK_ITERATION |
57a6839d | 530 | delete capitalizationBrkIter; |
f3c0d7a5 A |
531 | #endif |
532 | } | |
729e4ab9 A |
533 | |
534 | const Locale& | |
535 | LocaleDisplayNamesImpl::getLocale() const { | |
51004dcb | 536 | return locale; |
729e4ab9 A |
537 | } |
538 | ||
539 | UDialectHandling | |
540 | LocaleDisplayNamesImpl::getDialectHandling() const { | |
51004dcb A |
541 | return dialectHandling; |
542 | } | |
543 | ||
544 | UDisplayContext | |
545 | LocaleDisplayNamesImpl::getContext(UDisplayContextType type) const { | |
546 | switch (type) { | |
547 | case UDISPCTX_TYPE_DIALECT_HANDLING: | |
548 | return (UDisplayContext)dialectHandling; | |
549 | case UDISPCTX_TYPE_CAPITALIZATION: | |
550 | return capitalizationContext; | |
b331163b | 551 | case UDISPCTX_TYPE_DISPLAY_LENGTH: |
57a6839d | 552 | return nameLength; |
340931cb A |
553 | case UDISPCTX_TYPE_SUBSTITUTE_HANDLING: |
554 | return substitute; | |
b331163b A |
555 | case UADISPCTX_TYPE_LENGTH: // Apple-specific |
556 | return (nameLength == UDISPCTX_LENGTH_SHORT)? UADISPCTX_LENGTH_SHORT: UADISPCTX_LENGTH_STANDARD; | |
51004dcb A |
557 | default: |
558 | break; | |
559 | } | |
560 | return (UDisplayContext)0; | |
561 | } | |
562 | ||
563 | UnicodeString& | |
564 | LocaleDisplayNamesImpl::adjustForUsageAndContext(CapContextUsage usage, | |
565 | UnicodeString& result) const { | |
566 | #if !UCONFIG_NO_BREAK_ITERATION | |
340931cb A |
567 | // check to see whether we need to titlecase result-- we check the first word (up to the first space), |
568 | // and only titlecase the result if that first word contains no capital letters | |
569 | UBool needToTitlecase = TRUE; | |
570 | for (int32_t i = 0; needToTitlecase && i < result.length(); i++) { | |
571 | UChar32 c = result.char32At(i); | |
572 | if (u_isupper(c) || u_istitle(c)) { | |
573 | needToTitlecase = FALSE; | |
574 | } else if (u_isspace(c)) { | |
575 | break; | |
576 | } | |
577 | } | |
578 | ||
579 | if (needToTitlecase && capitalizationBrkIter!= NULL && | |
57a6839d A |
580 | ( capitalizationContext==UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE || fCapitalization[usage] ) ) { |
581 | // note fCapitalization[usage] won't be set unless capitalizationContext is UI_LIST_OR_MENU or STANDALONE | |
340931cb A |
582 | static UMutex capitalizationBrkIterLock; |
583 | Mutex lock(&capitalizationBrkIterLock); | |
57a6839d | 584 | result.toTitle(capitalizationBrkIter, locale, U_TITLECASE_NO_LOWERCASE | U_TITLECASE_NO_BREAK_ADJUSTMENT); |
51004dcb A |
585 | } |
586 | #endif | |
587 | return result; | |
729e4ab9 A |
588 | } |
589 | ||
590 | UnicodeString& | |
3d1f044b | 591 | LocaleDisplayNamesImpl::localeDisplayName(const Locale& loc, |
729e4ab9 | 592 | UnicodeString& result) const { |
3d1f044b | 593 | if (loc.isBogus()) { |
2ca993e8 A |
594 | result.setToBogus(); |
595 | return result; | |
596 | } | |
729e4ab9 A |
597 | UnicodeString resultName; |
598 | ||
3d1f044b | 599 | const char* lang = loc.getLanguage(); |
729e4ab9 A |
600 | if (uprv_strlen(lang) == 0) { |
601 | lang = "root"; | |
602 | } | |
3d1f044b A |
603 | const char* script = loc.getScript(); |
604 | const char* country = loc.getCountry(); | |
605 | const char* variant = loc.getVariant(); | |
729e4ab9 A |
606 | |
607 | UBool hasScript = uprv_strlen(script) > 0; | |
608 | UBool hasCountry = uprv_strlen(country) > 0; | |
609 | UBool hasVariant = uprv_strlen(variant) > 0; | |
610 | ||
3d1f044b A |
611 | // For stylistic reasons, always load dialect names for `zh` and `yue`. <rdar://50750364> |
612 | // Also for `ks`, 'pa, 'ur'. <rdar://50687287> | |
613 | UBool forceDialect = (uprv_strcmp(lang, "zh") == 0 || uprv_strcmp(lang, "yue") == 0 || | |
614 | uprv_strcmp(lang, "ks") == 0 || uprv_strcmp(lang, "pa") == 0 || uprv_strcmp(lang, "ur") == 0); | |
615 | ||
616 | if (forceDialect || dialectHandling == ULDN_DIALECT_NAMES) { | |
729e4ab9 A |
617 | char buffer[ULOC_FULLNAME_CAPACITY]; |
618 | do { // loop construct is so we can break early out of search | |
619 | if (hasScript && hasCountry) { | |
620 | ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", script, "_", country, (char *)0); | |
340931cb | 621 | localeIdName(buffer, resultName, false); |
729e4ab9 A |
622 | if (!resultName.isBogus()) { |
623 | hasScript = FALSE; | |
624 | hasCountry = FALSE; | |
625 | break; | |
626 | } | |
627 | } | |
628 | if (hasScript) { | |
629 | ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", script, (char *)0); | |
340931cb | 630 | localeIdName(buffer, resultName, false); |
729e4ab9 A |
631 | if (!resultName.isBogus()) { |
632 | hasScript = FALSE; | |
633 | break; | |
634 | } | |
635 | } | |
636 | if (hasCountry) { | |
637 | ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", country, (char*)0); | |
340931cb | 638 | localeIdName(buffer, resultName, false); |
729e4ab9 A |
639 | if (!resultName.isBogus()) { |
640 | hasCountry = FALSE; | |
641 | break; | |
642 | } | |
643 | } | |
644 | } while (FALSE); | |
645 | } | |
646 | if (resultName.isBogus() || resultName.isEmpty()) { | |
340931cb A |
647 | localeIdName(lang, resultName, substitute == UDISPCTX_SUBSTITUTE); |
648 | if (resultName.isBogus()) { | |
649 | result.setToBogus(); | |
650 | return result; | |
651 | } | |
729e4ab9 A |
652 | } |
653 | ||
654 | UnicodeString resultRemainder; | |
655 | UnicodeString temp; | |
729e4ab9 A |
656 | UErrorCode status = U_ZERO_ERROR; |
657 | ||
658 | if (hasScript) { | |
340931cb A |
659 | UnicodeString script_str = scriptDisplayName(script, temp, TRUE); |
660 | if (script_str.isBogus()) { | |
661 | result.setToBogus(); | |
662 | return result; | |
663 | } | |
664 | resultRemainder.append(script_str); | |
729e4ab9 A |
665 | } |
666 | if (hasCountry) { | |
340931cb A |
667 | UnicodeString region_str = regionShortDisplayName(country, temp); // Apple modification |
668 | if (region_str.isBogus()) { | |
669 | result.setToBogus(); | |
670 | return result; | |
671 | } | |
672 | appendWithSep(resultRemainder, region_str); | |
729e4ab9 A |
673 | } |
674 | if (hasVariant) { | |
340931cb A |
675 | UnicodeString variant_str = variantDisplayName(variant, temp, TRUE); |
676 | if (variant_str.isBogus()) { | |
677 | result.setToBogus(); | |
678 | return result; | |
679 | } | |
680 | appendWithSep(resultRemainder, variant_str); | |
729e4ab9 | 681 | } |
57a6839d A |
682 | resultRemainder.findAndReplace(formatOpenParen, formatReplaceOpenParen); |
683 | resultRemainder.findAndReplace(formatCloseParen, formatReplaceCloseParen); | |
729e4ab9 | 684 | |
3d1f044b | 685 | LocalPointer<StringEnumeration> e(loc.createKeywords(status)); |
2ca993e8 | 686 | if (e.isValid() && U_SUCCESS(status)) { |
729e4ab9 A |
687 | UnicodeString temp2; |
688 | char value[ULOC_KEYWORD_AND_VALUES_CAPACITY]; // sigh, no ULOC_VALUE_CAPACITY | |
689 | const char* key; | |
690 | while ((key = e->next((int32_t *)0, status)) != NULL) { | |
0f5d89e8 | 691 | value[0] = 0; |
3d1f044b | 692 | loc.getKeywordValue(key, value, ULOC_KEYWORD_AND_VALUES_CAPACITY, status); |
0f5d89e8 | 693 | if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING) { |
2ca993e8 A |
694 | return result; |
695 | } | |
696 | keyDisplayName(key, temp, TRUE); | |
57a6839d A |
697 | temp.findAndReplace(formatOpenParen, formatReplaceOpenParen); |
698 | temp.findAndReplace(formatCloseParen, formatReplaceCloseParen); | |
2ca993e8 | 699 | keyValueDisplayName(key, value, temp2, TRUE); |
57a6839d A |
700 | temp2.findAndReplace(formatOpenParen, formatReplaceOpenParen); |
701 | temp2.findAndReplace(formatCloseParen, formatReplaceCloseParen); | |
4388f060 A |
702 | if (temp2 != UnicodeString(value, -1, US_INV)) { |
703 | appendWithSep(resultRemainder, temp2); | |
704 | } else if (temp != UnicodeString(key, -1, US_INV)) { | |
705 | UnicodeString temp3; | |
2ca993e8 | 706 | keyTypeFormat.format(temp, temp2, temp3, status); |
4388f060 A |
707 | appendWithSep(resultRemainder, temp3); |
708 | } else { | |
709 | appendWithSep(resultRemainder, temp) | |
710 | .append((UChar)0x3d /* = */) | |
711 | .append(temp2); | |
712 | } | |
729e4ab9 | 713 | } |
729e4ab9 A |
714 | } |
715 | ||
716 | if (!resultRemainder.isEmpty()) { | |
2ca993e8 | 717 | format.format(resultName, resultRemainder, result.remove(), status); |
3d1f044b A |
718 | int32_t indexCloseOpen = result.indexOf(formatParenCloseOpen); |
719 | if (indexCloseOpen >= 0) { // replace redundant close-open parens with separator <rdar://problem/50687287> | |
720 | UnicodeString tail(result, indexCloseOpen + formatParenCloseOpen.length()); // section after close-open | |
721 | result.retainBetween(0, indexCloseOpen); // section before close-open | |
722 | appendWithSep(result, tail); // combine using separator | |
723 | } | |
51004dcb | 724 | return adjustForUsageAndContext(kCapContextUsageLanguage, result); |
729e4ab9 A |
725 | } |
726 | ||
51004dcb A |
727 | result = resultName; |
728 | return adjustForUsageAndContext(kCapContextUsageLanguage, result); | |
729e4ab9 A |
729 | } |
730 | ||
731 | UnicodeString& | |
732 | LocaleDisplayNamesImpl::appendWithSep(UnicodeString& buffer, const UnicodeString& src) const { | |
57a6839d A |
733 | if (buffer.isEmpty()) { |
734 | buffer.setTo(src); | |
735 | } else { | |
2ca993e8 | 736 | const UnicodeString *values[2] = { &buffer, &src }; |
57a6839d | 737 | UErrorCode status = U_ZERO_ERROR; |
2ca993e8 | 738 | separatorFormat.formatAndReplace(values, 2, buffer, NULL, 0, status); |
51004dcb | 739 | } |
51004dcb | 740 | return buffer; |
729e4ab9 A |
741 | } |
742 | ||
743 | UnicodeString& | |
744 | LocaleDisplayNamesImpl::localeDisplayName(const char* localeId, | |
745 | UnicodeString& result) const { | |
51004dcb | 746 | return localeDisplayName(Locale(localeId), result); |
729e4ab9 A |
747 | } |
748 | ||
51004dcb | 749 | // private |
729e4ab9 A |
750 | UnicodeString& |
751 | LocaleDisplayNamesImpl::localeIdName(const char* localeId, | |
340931cb | 752 | UnicodeString& result, bool substitute) const { |
b331163b A |
753 | if (nameLength == UDISPCTX_LENGTH_SHORT) { |
754 | langData.getNoFallback("Languages%short", localeId, result); | |
57a6839d A |
755 | if (!result.isBogus()) { |
756 | return result; | |
757 | } | |
758 | } | |
340931cb A |
759 | if (substitute) { |
760 | return langData.get("Languages", localeId, result); | |
761 | } else { | |
762 | return langData.getNoFallback("Languages", localeId, result); | |
763 | } | |
729e4ab9 A |
764 | } |
765 | ||
766 | UnicodeString& | |
767 | LocaleDisplayNamesImpl::languageDisplayName(const char* lang, | |
768 | UnicodeString& result) const { | |
51004dcb A |
769 | if (uprv_strcmp("root", lang) == 0 || uprv_strchr(lang, '_') != NULL) { |
770 | return result = UnicodeString(lang, -1, US_INV); | |
771 | } | |
b331163b | 772 | if (nameLength == UDISPCTX_LENGTH_SHORT) { |
2ca993e8 | 773 | langData.getNoFallback("Languages%short", lang, result); |
57a6839d A |
774 | if (!result.isBogus()) { |
775 | return adjustForUsageAndContext(kCapContextUsageLanguage, result); | |
776 | } | |
777 | } | |
340931cb A |
778 | if (substitute == UDISPCTX_SUBSTITUTE) { |
779 | langData.get("Languages", lang, result); | |
780 | } else { | |
781 | langData.getNoFallback("Languages", lang, result); | |
782 | } | |
51004dcb | 783 | return adjustForUsageAndContext(kCapContextUsageLanguage, result); |
729e4ab9 A |
784 | } |
785 | ||
786 | UnicodeString& | |
787 | LocaleDisplayNamesImpl::scriptDisplayName(const char* script, | |
2ca993e8 A |
788 | UnicodeString& result, |
789 | UBool skipAdjust) const { | |
790 | if (!skipAdjust) { // => prefer standalone | |
791 | langData.getNoFallback("Scripts%stand-alone", script, result); | |
b331163b A |
792 | if (!result.isBogus()) { |
793 | return adjustForUsageAndContext(kCapContextUsageScript, result); | |
794 | } | |
795 | } | |
2ca993e8 A |
796 | if (nameLength == UDISPCTX_LENGTH_SHORT) { |
797 | langData.getNoFallback("Scripts%short", script, result); | |
798 | if (!result.isBogus()) { | |
799 | return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageScript, result); | |
800 | } | |
801 | } | |
340931cb A |
802 | if (substitute == UDISPCTX_SUBSTITUTE) { |
803 | langData.get("Scripts", script, result); | |
804 | } else { | |
805 | langData.getNoFallback("Scripts", script, result); | |
806 | } | |
2ca993e8 A |
807 | return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageScript, result); |
808 | } | |
809 | ||
810 | UnicodeString& | |
811 | LocaleDisplayNamesImpl::scriptDisplayName(const char* script, | |
812 | UnicodeString& result) const { | |
813 | return scriptDisplayName(script, result, FALSE); | |
729e4ab9 A |
814 | } |
815 | ||
816 | UnicodeString& | |
817 | LocaleDisplayNamesImpl::scriptDisplayName(UScriptCode scriptCode, | |
818 | UnicodeString& result) const { | |
2ca993e8 | 819 | return scriptDisplayName(uscript_getName(scriptCode), result, FALSE); |
729e4ab9 A |
820 | } |
821 | ||
822 | UnicodeString& | |
823 | LocaleDisplayNamesImpl::regionDisplayName(const char* region, | |
2ca993e8 A |
824 | UnicodeString& result, |
825 | UBool skipAdjust) const { | |
b331163b | 826 | if (nameLength == UDISPCTX_LENGTH_SHORT) { |
2ca993e8 | 827 | regionData.getNoFallback("Countries%short", region, result); |
57a6839d | 828 | if (!result.isBogus()) { |
2ca993e8 | 829 | return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageTerritory, result); |
57a6839d A |
830 | } |
831 | } | |
340931cb A |
832 | if (substitute == UDISPCTX_SUBSTITUTE) { |
833 | regionData.get("Countries", region, result); | |
834 | } else { | |
835 | regionData.getNoFallback("Countries", region, result); | |
836 | } | |
2ca993e8 | 837 | return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageTerritory, result); |
57a6839d A |
838 | } |
839 | ||
2ca993e8 A |
840 | // private Apple, only for building localeDisplayName |
841 | // (use short region, don't adjust for context) | |
57a6839d A |
842 | UnicodeString& |
843 | LocaleDisplayNamesImpl::regionShortDisplayName(const char* region, | |
844 | UnicodeString& result) const { | |
845 | if (uprv_strcmp(region, "PS") != 0) { | |
b331163b | 846 | regionData.getNoFallback("Countries%short", region, result); |
57a6839d | 847 | if (!result.isBogus()) { |
2ca993e8 | 848 | return result; |
57a6839d A |
849 | } |
850 | } | |
340931cb A |
851 | if (substitute == UDISPCTX_SUBSTITUTE) { |
852 | regionData.get("Countries", region, result); | |
853 | } else { | |
854 | regionData.getNoFallback("Countries", region, result); | |
855 | } | |
2ca993e8 | 856 | return result; |
729e4ab9 A |
857 | } |
858 | ||
2ca993e8 A |
859 | UnicodeString& |
860 | LocaleDisplayNamesImpl::regionDisplayName(const char* region, | |
861 | UnicodeString& result) const { | |
862 | return regionDisplayName(region, result, FALSE); | |
863 | } | |
864 | ||
865 | ||
729e4ab9 A |
866 | UnicodeString& |
867 | LocaleDisplayNamesImpl::variantDisplayName(const char* variant, | |
2ca993e8 A |
868 | UnicodeString& result, |
869 | UBool skipAdjust) const { | |
b331163b | 870 | // don't have a resource for short variant names |
340931cb A |
871 | if (substitute == UDISPCTX_SUBSTITUTE) { |
872 | langData.get("Variants", variant, result); | |
873 | } else { | |
874 | langData.getNoFallback("Variants", variant, result); | |
875 | } | |
2ca993e8 A |
876 | return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageVariant, result); |
877 | } | |
878 | ||
879 | UnicodeString& | |
880 | LocaleDisplayNamesImpl::variantDisplayName(const char* variant, | |
881 | UnicodeString& result) const { | |
882 | return variantDisplayName(variant, result, FALSE); | |
729e4ab9 A |
883 | } |
884 | ||
885 | UnicodeString& | |
886 | LocaleDisplayNamesImpl::keyDisplayName(const char* key, | |
2ca993e8 A |
887 | UnicodeString& result, |
888 | UBool skipAdjust) const { | |
b331163b | 889 | // don't have a resource for short key names |
340931cb A |
890 | if (substitute == UDISPCTX_SUBSTITUTE) { |
891 | langData.get("Keys", key, result); | |
892 | } else { | |
893 | langData.getNoFallback("Keys", key, result); | |
894 | } | |
2ca993e8 A |
895 | return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKey, result); |
896 | } | |
897 | ||
898 | UnicodeString& | |
899 | LocaleDisplayNamesImpl::keyDisplayName(const char* key, | |
900 | UnicodeString& result) const { | |
901 | return keyDisplayName(key, result, FALSE); | |
729e4ab9 A |
902 | } |
903 | ||
904 | UnicodeString& | |
905 | LocaleDisplayNamesImpl::keyValueDisplayName(const char* key, | |
906 | const char* value, | |
2ca993e8 A |
907 | UnicodeString& result, |
908 | UBool skipAdjust) const { | |
b331163b A |
909 | if (uprv_strcmp(key, "currency") == 0) { |
910 | // ICU4C does not have ICU4J CurrencyDisplayInfo equivalent for now. | |
911 | UErrorCode sts = U_ZERO_ERROR; | |
912 | UnicodeString ustrValue(value, -1, US_INV); | |
913 | int32_t len; | |
b331163b | 914 | const UChar *currencyName = ucurr_getName(ustrValue.getTerminatedBuffer(), |
340931cb | 915 | locale.getBaseName(), UCURR_LONG_NAME, nullptr /* isChoiceFormat */, &len, &sts); |
b331163b A |
916 | if (U_FAILURE(sts)) { |
917 | // Return the value as is on failure | |
918 | result = ustrValue; | |
919 | return result; | |
920 | } | |
921 | result.setTo(currencyName, len); | |
2ca993e8 | 922 | return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result); |
b331163b A |
923 | } |
924 | ||
925 | if (nameLength == UDISPCTX_LENGTH_SHORT) { | |
340931cb | 926 | langData.getNoFallback("Types%short", key, value, result); |
b331163b | 927 | if (!result.isBogus()) { |
2ca993e8 | 928 | return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result); |
b331163b A |
929 | } |
930 | } | |
340931cb A |
931 | if (substitute == UDISPCTX_SUBSTITUTE) { |
932 | langData.get("Types", key, value, result); | |
933 | } else { | |
934 | langData.getNoFallback("Types", key, value, result); | |
935 | } | |
2ca993e8 A |
936 | return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result); |
937 | } | |
938 | ||
939 | UnicodeString& | |
940 | LocaleDisplayNamesImpl::keyValueDisplayName(const char* key, | |
941 | const char* value, | |
942 | UnicodeString& result) const { | |
943 | return keyValueDisplayName(key, value, result, FALSE); | |
729e4ab9 A |
944 | } |
945 | ||
946 | //////////////////////////////////////////////////////////////////////////////////////////////////// | |
947 | ||
948 | LocaleDisplayNames* | |
949 | LocaleDisplayNames::createInstance(const Locale& locale, | |
950 | UDialectHandling dialectHandling) { | |
51004dcb A |
951 | return new LocaleDisplayNamesImpl(locale, dialectHandling); |
952 | } | |
953 | ||
954 | LocaleDisplayNames* | |
955 | LocaleDisplayNames::createInstance(const Locale& locale, | |
956 | UDisplayContext *contexts, int32_t length) { | |
957 | if (contexts == NULL) { | |
958 | length = 0; | |
959 | } | |
960 | return new LocaleDisplayNamesImpl(locale, contexts, length); | |
729e4ab9 A |
961 | } |
962 | ||
963 | U_NAMESPACE_END | |
964 | ||
965 | //////////////////////////////////////////////////////////////////////////////////////////////////// | |
966 | ||
967 | U_NAMESPACE_USE | |
968 | ||
51004dcb | 969 | U_CAPI ULocaleDisplayNames * U_EXPORT2 |
729e4ab9 A |
970 | uldn_open(const char * locale, |
971 | UDialectHandling dialectHandling, | |
972 | UErrorCode *pErrorCode) { | |
973 | if (U_FAILURE(*pErrorCode)) { | |
974 | return 0; | |
975 | } | |
976 | if (locale == NULL) { | |
977 | locale = uloc_getDefault(); | |
978 | } | |
979 | return (ULocaleDisplayNames *)LocaleDisplayNames::createInstance(Locale(locale), dialectHandling); | |
980 | } | |
981 | ||
51004dcb A |
982 | U_CAPI ULocaleDisplayNames * U_EXPORT2 |
983 | uldn_openForContext(const char * locale, | |
984 | UDisplayContext *contexts, int32_t length, | |
985 | UErrorCode *pErrorCode) { | |
986 | if (U_FAILURE(*pErrorCode)) { | |
987 | return 0; | |
988 | } | |
989 | if (locale == NULL) { | |
990 | locale = uloc_getDefault(); | |
991 | } | |
992 | return (ULocaleDisplayNames *)LocaleDisplayNames::createInstance(Locale(locale), contexts, length); | |
993 | } | |
994 | ||
995 | ||
996 | U_CAPI void U_EXPORT2 | |
729e4ab9 A |
997 | uldn_close(ULocaleDisplayNames *ldn) { |
998 | delete (LocaleDisplayNames *)ldn; | |
999 | } | |
1000 | ||
51004dcb | 1001 | U_CAPI const char * U_EXPORT2 |
729e4ab9 A |
1002 | uldn_getLocale(const ULocaleDisplayNames *ldn) { |
1003 | if (ldn) { | |
1004 | return ((const LocaleDisplayNames *)ldn)->getLocale().getName(); | |
1005 | } | |
1006 | return NULL; | |
1007 | } | |
1008 | ||
51004dcb | 1009 | U_CAPI UDialectHandling U_EXPORT2 |
729e4ab9 A |
1010 | uldn_getDialectHandling(const ULocaleDisplayNames *ldn) { |
1011 | if (ldn) { | |
1012 | return ((const LocaleDisplayNames *)ldn)->getDialectHandling(); | |
1013 | } | |
1014 | return ULDN_STANDARD_NAMES; | |
1015 | } | |
1016 | ||
51004dcb A |
1017 | U_CAPI UDisplayContext U_EXPORT2 |
1018 | uldn_getContext(const ULocaleDisplayNames *ldn, | |
1019 | UDisplayContextType type, | |
1020 | UErrorCode *pErrorCode) { | |
1021 | if (U_FAILURE(*pErrorCode)) { | |
1022 | return (UDisplayContext)0; | |
1023 | } | |
1024 | return ((const LocaleDisplayNames *)ldn)->getContext(type); | |
1025 | } | |
1026 | ||
1027 | U_CAPI int32_t U_EXPORT2 | |
729e4ab9 A |
1028 | uldn_localeDisplayName(const ULocaleDisplayNames *ldn, |
1029 | const char *locale, | |
1030 | UChar *result, | |
1031 | int32_t maxResultSize, | |
1032 | UErrorCode *pErrorCode) { | |
1033 | if (U_FAILURE(*pErrorCode)) { | |
1034 | return 0; | |
1035 | } | |
1036 | if (ldn == NULL || locale == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) { | |
1037 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; | |
1038 | return 0; | |
1039 | } | |
1040 | UnicodeString temp(result, 0, maxResultSize); | |
1041 | ((const LocaleDisplayNames *)ldn)->localeDisplayName(locale, temp); | |
2ca993e8 A |
1042 | if (temp.isBogus()) { |
1043 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; | |
1044 | return 0; | |
1045 | } | |
729e4ab9 A |
1046 | return temp.extract(result, maxResultSize, *pErrorCode); |
1047 | } | |
1048 | ||
51004dcb | 1049 | U_CAPI int32_t U_EXPORT2 |
729e4ab9 A |
1050 | uldn_languageDisplayName(const ULocaleDisplayNames *ldn, |
1051 | const char *lang, | |
1052 | UChar *result, | |
1053 | int32_t maxResultSize, | |
1054 | UErrorCode *pErrorCode) { | |
1055 | if (U_FAILURE(*pErrorCode)) { | |
1056 | return 0; | |
1057 | } | |
1058 | if (ldn == NULL || lang == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) { | |
1059 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; | |
1060 | return 0; | |
1061 | } | |
1062 | UnicodeString temp(result, 0, maxResultSize); | |
1063 | ((const LocaleDisplayNames *)ldn)->languageDisplayName(lang, temp); | |
1064 | return temp.extract(result, maxResultSize, *pErrorCode); | |
1065 | } | |
1066 | ||
51004dcb | 1067 | U_CAPI int32_t U_EXPORT2 |
729e4ab9 A |
1068 | uldn_scriptDisplayName(const ULocaleDisplayNames *ldn, |
1069 | const char *script, | |
1070 | UChar *result, | |
1071 | int32_t maxResultSize, | |
1072 | UErrorCode *pErrorCode) { | |
1073 | if (U_FAILURE(*pErrorCode)) { | |
1074 | return 0; | |
1075 | } | |
1076 | if (ldn == NULL || script == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) { | |
1077 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; | |
1078 | return 0; | |
1079 | } | |
1080 | UnicodeString temp(result, 0, maxResultSize); | |
1081 | ((const LocaleDisplayNames *)ldn)->scriptDisplayName(script, temp); | |
1082 | return temp.extract(result, maxResultSize, *pErrorCode); | |
1083 | } | |
1084 | ||
51004dcb | 1085 | U_CAPI int32_t U_EXPORT2 |
729e4ab9 A |
1086 | uldn_scriptCodeDisplayName(const ULocaleDisplayNames *ldn, |
1087 | UScriptCode scriptCode, | |
1088 | UChar *result, | |
1089 | int32_t maxResultSize, | |
1090 | UErrorCode *pErrorCode) { | |
1091 | return uldn_scriptDisplayName(ldn, uscript_getName(scriptCode), result, maxResultSize, pErrorCode); | |
1092 | } | |
1093 | ||
51004dcb | 1094 | U_CAPI int32_t U_EXPORT2 |
729e4ab9 A |
1095 | uldn_regionDisplayName(const ULocaleDisplayNames *ldn, |
1096 | const char *region, | |
1097 | UChar *result, | |
1098 | int32_t maxResultSize, | |
1099 | UErrorCode *pErrorCode) { | |
1100 | if (U_FAILURE(*pErrorCode)) { | |
1101 | return 0; | |
1102 | } | |
1103 | if (ldn == NULL || region == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) { | |
1104 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; | |
1105 | return 0; | |
1106 | } | |
1107 | UnicodeString temp(result, 0, maxResultSize); | |
1108 | ((const LocaleDisplayNames *)ldn)->regionDisplayName(region, temp); | |
1109 | return temp.extract(result, maxResultSize, *pErrorCode); | |
1110 | } | |
1111 | ||
51004dcb | 1112 | U_CAPI int32_t U_EXPORT2 |
729e4ab9 A |
1113 | uldn_variantDisplayName(const ULocaleDisplayNames *ldn, |
1114 | const char *variant, | |
1115 | UChar *result, | |
1116 | int32_t maxResultSize, | |
1117 | UErrorCode *pErrorCode) { | |
1118 | if (U_FAILURE(*pErrorCode)) { | |
1119 | return 0; | |
1120 | } | |
1121 | if (ldn == NULL || variant == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) { | |
1122 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; | |
1123 | return 0; | |
1124 | } | |
1125 | UnicodeString temp(result, 0, maxResultSize); | |
1126 | ((const LocaleDisplayNames *)ldn)->variantDisplayName(variant, temp); | |
1127 | return temp.extract(result, maxResultSize, *pErrorCode); | |
1128 | } | |
1129 | ||
51004dcb | 1130 | U_CAPI int32_t U_EXPORT2 |
729e4ab9 A |
1131 | uldn_keyDisplayName(const ULocaleDisplayNames *ldn, |
1132 | const char *key, | |
1133 | UChar *result, | |
1134 | int32_t maxResultSize, | |
1135 | UErrorCode *pErrorCode) { | |
1136 | if (U_FAILURE(*pErrorCode)) { | |
1137 | return 0; | |
1138 | } | |
1139 | if (ldn == NULL || key == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) { | |
1140 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; | |
1141 | return 0; | |
1142 | } | |
1143 | UnicodeString temp(result, 0, maxResultSize); | |
1144 | ((const LocaleDisplayNames *)ldn)->keyDisplayName(key, temp); | |
1145 | return temp.extract(result, maxResultSize, *pErrorCode); | |
1146 | } | |
1147 | ||
51004dcb | 1148 | U_CAPI int32_t U_EXPORT2 |
729e4ab9 A |
1149 | uldn_keyValueDisplayName(const ULocaleDisplayNames *ldn, |
1150 | const char *key, | |
1151 | const char *value, | |
1152 | UChar *result, | |
1153 | int32_t maxResultSize, | |
1154 | UErrorCode *pErrorCode) { | |
1155 | if (U_FAILURE(*pErrorCode)) { | |
1156 | return 0; | |
1157 | } | |
1158 | if (ldn == NULL || key == NULL || value == NULL || (result == NULL && maxResultSize > 0) | |
1159 | || maxResultSize < 0) { | |
1160 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; | |
1161 | return 0; | |
1162 | } | |
1163 | UnicodeString temp(result, 0, maxResultSize); | |
1164 | ((const LocaleDisplayNames *)ldn)->keyValueDisplayName(key, value, temp); | |
1165 | return temp.extract(result, maxResultSize, *pErrorCode); | |
1166 | } | |
1167 | ||
1168 | #endif |