]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/locdspnm.cpp
ICU-62109.0.1.tar.gz
[apple/icu.git] / icuSources / common / locdspnm.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 2010-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
8 */
9
10 #include "unicode/utypes.h"
11
12 #if !UCONFIG_NO_FORMATTING
13
14 #include "unicode/locdspnm.h"
15 #include "unicode/simpleformatter.h"
16 #include "unicode/ucasemap.h"
17 #include "unicode/ures.h"
18 #include "unicode/udisplaycontext.h"
19 #include "unicode/brkiter.h"
20 #include "unicode/ucurr.h"
21 #include "cmemory.h"
22 #include "cstring.h"
23 #include "mutex.h"
24 #include "ulocimp.h"
25 #include "umutex.h"
26 #include "ureslocs.h"
27 #include "uresimp.h"
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);
48 while ((str = va_arg(args, char *))) {
49 char c;
50 while (p != e && (c = *str++)) {
51 *p++ = c;
52 }
53 }
54 *p = 0;
55 va_end(args);
56
57 return static_cast<int32_t>(p - buffer);
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 {
67 const char* path;
68 Locale locale;
69
70 public:
71 ICUDataTable(const char* path, const Locale& locale);
72 ~ICUDataTable();
73
74 const Locale& getLocale();
75
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;
80
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;
85 };
86
87 inline UnicodeString &
88 ICUDataTable::get(const char* tableKey, const char* itemKey, UnicodeString& result) const {
89 return get(tableKey, NULL, itemKey, result);
90 }
91
92 inline UnicodeString &
93 ICUDataTable::getNoFallback(const char* tableKey, const char* itemKey, UnicodeString& result) const {
94 return getNoFallback(tableKey, NULL, itemKey, result);
95 }
96
97 ICUDataTable::ICUDataTable(const char* path, const Locale& locale)
98 : path(NULL), locale(Locale::getRoot())
99 {
100 if (path) {
101 int32_t len = uprv_strlen(path);
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);
131 if (U_SUCCESS(status) && len > 0) {
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
156 LocaleDisplayNames::~LocaleDisplayNames() {}
157
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;
173
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 {
276 Locale locale;
277 UDialectHandling dialectHandling;
278 ICUDataTable langData;
279 ICUDataTable regionData;
280 SimpleFormatter separatorFormat;
281 SimpleFormatter format;
282 SimpleFormatter keyTypeFormat;
283 UDisplayContext capitalizationContext;
284 #if !UCONFIG_NO_BREAK_ITERATION
285 BreakIterator* capitalizationBrkIter;
286 #else
287 UObject* capitalizationBrkIter;
288 #endif
289 static UMutex capitalizationBrkIterLock;
290 UnicodeString formatOpenParen;
291 UnicodeString formatReplaceOpenParen;
292 UnicodeString formatCloseParen;
293 UnicodeString formatReplaceCloseParen;
294 UDisplayContext nameLength;
295
296 // Constants for capitalization context usage types.
297 enum CapContextUsage {
298 kCapContextUsageLanguage,
299 kCapContextUsageScript,
300 kCapContextUsageTerritory,
301 kCapContextUsageVariant,
302 kCapContextUsageKey,
303 kCapContextUsageKeyValue,
304 kCapContextUsageCount
305 };
306 // Capitalization transforms. For each usage type, indicates whether to titlecase for
307 // the context specified in capitalizationContext (which we know at construction time)
308 UBool fCapitalization[kCapContextUsageCount];
309
310 public:
311 // constructor
312 LocaleDisplayNamesImpl(const Locale& locale, UDialectHandling dialectHandling);
313 LocaleDisplayNamesImpl(const Locale& locale, UDisplayContext *contexts, int32_t length);
314 virtual ~LocaleDisplayNamesImpl();
315
316 virtual const Locale& getLocale() const;
317 virtual UDialectHandling getDialectHandling() const;
318 virtual UDisplayContext getContext(UDisplayContextType type) const;
319
320 virtual UnicodeString& localeDisplayName(const Locale& locale,
321 UnicodeString& result) const;
322 virtual UnicodeString& localeDisplayName(const char* localeId,
323 UnicodeString& result) const;
324 virtual UnicodeString& languageDisplayName(const char* lang,
325 UnicodeString& result) const;
326 virtual UnicodeString& scriptDisplayName(const char* script,
327 UnicodeString& result) const;
328 virtual UnicodeString& scriptDisplayName(UScriptCode scriptCode,
329 UnicodeString& result) const;
330 virtual UnicodeString& regionDisplayName(const char* region,
331 UnicodeString& result) const;
332 virtual UnicodeString& variantDisplayName(const char* variant,
333 UnicodeString& result) const;
334 virtual UnicodeString& keyDisplayName(const char* key,
335 UnicodeString& result) const;
336 virtual UnicodeString& keyValueDisplayName(const char* key,
337 const char* value,
338 UnicodeString& result) const;
339 private:
340 UnicodeString& localeIdName(const char* localeId,
341 UnicodeString& result) const;
342 UnicodeString& regionShortDisplayName(const char* region, // Apple-specific
343 UnicodeString& result) const;
344 UnicodeString& appendWithSep(UnicodeString& buffer, const UnicodeString& src) const;
345 UnicodeString& adjustForUsageAndContext(CapContextUsage usage, UnicodeString& result) const;
346 UnicodeString& scriptDisplayName(const char* script, UnicodeString& result, UBool skipAdjust) const;
347 UnicodeString& regionDisplayName(const char* region, UnicodeString& result, UBool skipAdjust) const;
348 UnicodeString& variantDisplayName(const char* variant, UnicodeString& result, UBool skipAdjust) const;
349 UnicodeString& keyDisplayName(const char* key, UnicodeString& result, UBool skipAdjust) const;
350 UnicodeString& keyValueDisplayName(const char* key, const char* value,
351 UnicodeString& result, UBool skipAdjust) const;
352 void initialize(void);
353
354 struct CapitalizationContextSink;
355 };
356
357 UMutex LocaleDisplayNamesImpl::capitalizationBrkIterLock = U_MUTEX_INITIALIZER;
358
359 LocaleDisplayNamesImpl::LocaleDisplayNamesImpl(const Locale& locale,
360 UDialectHandling dialectHandling)
361 : dialectHandling(dialectHandling)
362 , langData(U_ICUDATA_LANG, locale)
363 , regionData(U_ICUDATA_REGION, locale)
364 , capitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
365 , capitalizationBrkIter(NULL)
366 , nameLength(UDISPCTX_LENGTH_FULL)
367 {
368 initialize();
369 }
370
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)
376 , capitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
377 , capitalizationBrkIter(NULL)
378 , nameLength(UDISPCTX_LENGTH_FULL)
379 {
380 while (length-- > 0) {
381 UDisplayContext value = *contexts++;
382 UDisplayContextType selector = (UDisplayContextType)((uint32_t)value >> 8);
383 switch (selector) {
384 case UDISPCTX_TYPE_DIALECT_HANDLING:
385 dialectHandling = (UDialectHandling)value;
386 break;
387 case UDISPCTX_TYPE_CAPITALIZATION:
388 capitalizationContext = value;
389 break;
390 case UDISPCTX_TYPE_DISPLAY_LENGTH:
391 nameLength = value;
392 break;
393 case UADISPCTX_TYPE_LENGTH: // Apple-specific
394 nameLength = (value == UADISPCTX_LENGTH_SHORT)? UDISPCTX_LENGTH_SHORT: UDISPCTX_LENGTH_FULL;
395 break;
396 default:
397 break;
398 }
399 }
400 initialize();
401 }
402
403 struct LocaleDisplayNamesImpl::CapitalizationContextSink : public ResourceSink {
404 UBool hasCapitalizationUsage;
405 LocaleDisplayNamesImpl& parent;
406
407 CapitalizationContextSink(LocaleDisplayNamesImpl& _parent)
408 : hasCapitalizationUsage(FALSE), parent(_parent) {}
409 virtual ~CapitalizationContextSink();
410
411 virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
412 UErrorCode &errorCode) {
413 ResourceTable contexts = value.getTable(errorCode);
414 if (U_FAILURE(errorCode)) { return; }
415 for (int i = 0; contexts.getKeyAndValue(i, key, value); ++i) {
416
417 CapContextUsage usageEnum;
418 if (uprv_strcmp(key, "key") == 0) {
419 usageEnum = kCapContextUsageKey;
420 } else if (uprv_strcmp(key, "keyValue") == 0) {
421 usageEnum = kCapContextUsageKeyValue;
422 } else if (uprv_strcmp(key, "languages") == 0) {
423 usageEnum = kCapContextUsageLanguage;
424 } else if (uprv_strcmp(key, "script") == 0) {
425 usageEnum = kCapContextUsageScript;
426 } else if (uprv_strcmp(key, "territory") == 0) {
427 usageEnum = kCapContextUsageTerritory;
428 } else if (uprv_strcmp(key, "variant") == 0) {
429 usageEnum = kCapContextUsageVariant;
430 } else {
431 continue;
432 }
433
434 int32_t len = 0;
435 const int32_t* intVector = value.getIntVector(len, errorCode);
436 if (U_FAILURE(errorCode)) { return; }
437 if (len < 2) { continue; }
438
439 int32_t titlecaseInt = (parent.capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU) ? intVector[0] : intVector[1];
440 if (titlecaseInt == 0) { continue; }
441
442 parent.fCapitalization[usageEnum] = TRUE;
443 hasCapitalizationUsage = TRUE;
444 }
445 }
446 };
447
448 // Virtual destructors must be defined out of line.
449 LocaleDisplayNamesImpl::CapitalizationContextSink::~CapitalizationContextSink() {}
450
451 void
452 LocaleDisplayNamesImpl::initialize(void) {
453 LocaleDisplayNamesImpl *nonConstThis = (LocaleDisplayNamesImpl *)this;
454 nonConstThis->locale = langData.getLocale() == Locale::getRoot()
455 ? regionData.getLocale()
456 : langData.getLocale();
457
458 UnicodeString sep;
459 langData.getNoFallback("localeDisplayPattern", "separator", sep);
460 if (sep.isBogus()) {
461 sep = UnicodeString("{0}, {1}", -1, US_INV);
462 }
463 UErrorCode status = U_ZERO_ERROR;
464 separatorFormat.applyPatternMinMaxArguments(sep, 2, 2, status);
465
466 UnicodeString pattern;
467 langData.getNoFallback("localeDisplayPattern", "pattern", pattern);
468 if (pattern.isBogus()) {
469 pattern = UnicodeString("{0} ({1})", -1, US_INV);
470 }
471 format.applyPatternMinMaxArguments(pattern, 2, 2, status);
472 if (pattern.indexOf((UChar)0xFF08) >= 0) {
473 formatOpenParen.setTo((UChar)0xFF08); // fullwidth (
474 formatReplaceOpenParen.setTo((UChar)0xFF3B); // fullwidth [
475 formatCloseParen.setTo((UChar)0xFF09); // fullwidth )
476 formatReplaceCloseParen.setTo((UChar)0xFF3D); // fullwidth ]
477 } else {
478 formatOpenParen.setTo((UChar)0x0028); // (
479 formatReplaceOpenParen.setTo((UChar)0x005B); // [
480 formatCloseParen.setTo((UChar)0x0029); // )
481 formatReplaceCloseParen.setTo((UChar)0x005D); // ]
482 }
483
484 UnicodeString ktPattern;
485 langData.get("localeDisplayPattern", "keyTypePattern", ktPattern);
486 if (ktPattern.isBogus()) {
487 ktPattern = UnicodeString("{0}={1}", -1, US_INV);
488 }
489 keyTypeFormat.applyPatternMinMaxArguments(ktPattern, 2, 2, status);
490
491 uprv_memset(fCapitalization, 0, sizeof(fCapitalization));
492 #if !UCONFIG_NO_BREAK_ITERATION
493 // Only get the context data if we need it! This is a const object so we know now...
494 // Also check whether we will need a break iterator (depends on the data)
495 UBool needBrkIter = FALSE;
496 if (capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU || capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_STANDALONE) {
497 LocalUResourceBundlePointer resource(ures_open(NULL, locale.getName(), &status));
498 if (U_FAILURE(status)) { return; }
499 CapitalizationContextSink sink(*this);
500 ures_getAllItemsWithFallback(resource.getAlias(), "contextTransforms", sink, status);
501 if (status == U_MISSING_RESOURCE_ERROR) {
502 // Silently ignore. Not every locale has contextTransforms.
503 status = U_ZERO_ERROR;
504 } else if (U_FAILURE(status)) {
505 return;
506 }
507 needBrkIter = sink.hasCapitalizationUsage;
508 }
509 // Get a sentence break iterator if we will need it
510 if (needBrkIter || capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE) {
511 status = U_ZERO_ERROR;
512 capitalizationBrkIter = BreakIterator::createSentenceInstance(locale, status);
513 if (U_FAILURE(status)) {
514 delete capitalizationBrkIter;
515 capitalizationBrkIter = NULL;
516 }
517 }
518 #endif
519 }
520
521 LocaleDisplayNamesImpl::~LocaleDisplayNamesImpl() {
522 #if !UCONFIG_NO_BREAK_ITERATION
523 delete capitalizationBrkIter;
524 #endif
525 }
526
527 const Locale&
528 LocaleDisplayNamesImpl::getLocale() const {
529 return locale;
530 }
531
532 UDialectHandling
533 LocaleDisplayNamesImpl::getDialectHandling() const {
534 return dialectHandling;
535 }
536
537 UDisplayContext
538 LocaleDisplayNamesImpl::getContext(UDisplayContextType type) const {
539 switch (type) {
540 case UDISPCTX_TYPE_DIALECT_HANDLING:
541 return (UDisplayContext)dialectHandling;
542 case UDISPCTX_TYPE_CAPITALIZATION:
543 return capitalizationContext;
544 case UDISPCTX_TYPE_DISPLAY_LENGTH:
545 return nameLength;
546 case UADISPCTX_TYPE_LENGTH: // Apple-specific
547 return (nameLength == UDISPCTX_LENGTH_SHORT)? UADISPCTX_LENGTH_SHORT: UADISPCTX_LENGTH_STANDARD;
548 default:
549 break;
550 }
551 return (UDisplayContext)0;
552 }
553
554 UnicodeString&
555 LocaleDisplayNamesImpl::adjustForUsageAndContext(CapContextUsage usage,
556 UnicodeString& result) const {
557 #if !UCONFIG_NO_BREAK_ITERATION
558 // check to see whether we need to titlecase result
559 if ( result.length() > 0 && u_islower(result.char32At(0)) && capitalizationBrkIter!= NULL &&
560 ( capitalizationContext==UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE || fCapitalization[usage] ) ) {
561 // note fCapitalization[usage] won't be set unless capitalizationContext is UI_LIST_OR_MENU or STANDALONE
562 Mutex lock(&capitalizationBrkIterLock);
563 result.toTitle(capitalizationBrkIter, locale, U_TITLECASE_NO_LOWERCASE | U_TITLECASE_NO_BREAK_ADJUSTMENT);
564 }
565 #endif
566 return result;
567 }
568
569 UnicodeString&
570 LocaleDisplayNamesImpl::localeDisplayName(const Locale& locale,
571 UnicodeString& result) const {
572 if (locale.isBogus()) {
573 result.setToBogus();
574 return result;
575 }
576 UnicodeString resultName;
577
578 const char* lang = locale.getLanguage();
579 if (uprv_strlen(lang) == 0) {
580 lang = "root";
581 }
582 const char* script = locale.getScript();
583 const char* country = locale.getCountry();
584 const char* variant = locale.getVariant();
585
586 UBool hasScript = uprv_strlen(script) > 0;
587 UBool hasCountry = uprv_strlen(country) > 0;
588 UBool hasVariant = uprv_strlen(variant) > 0;
589
590 if (dialectHandling == ULDN_DIALECT_NAMES) {
591 char buffer[ULOC_FULLNAME_CAPACITY];
592 do { // loop construct is so we can break early out of search
593 if (hasScript && hasCountry) {
594 ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", script, "_", country, (char *)0);
595 localeIdName(buffer, resultName);
596 if (!resultName.isBogus()) {
597 hasScript = FALSE;
598 hasCountry = FALSE;
599 break;
600 }
601 }
602 if (hasScript) {
603 ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", script, (char *)0);
604 localeIdName(buffer, resultName);
605 if (!resultName.isBogus()) {
606 hasScript = FALSE;
607 break;
608 }
609 }
610 if (hasCountry) {
611 ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", country, (char*)0);
612 localeIdName(buffer, resultName);
613 if (!resultName.isBogus()) {
614 hasCountry = FALSE;
615 break;
616 }
617 }
618 } while (FALSE);
619 }
620 if (resultName.isBogus() || resultName.isEmpty()) {
621 localeIdName(lang, resultName);
622 }
623
624 UnicodeString resultRemainder;
625 UnicodeString temp;
626 UErrorCode status = U_ZERO_ERROR;
627
628 if (hasScript) {
629 resultRemainder.append(scriptDisplayName(script, temp, TRUE));
630 }
631 if (hasCountry) {
632 appendWithSep(resultRemainder, regionShortDisplayName(country, temp)); // Apple modification
633 }
634 if (hasVariant) {
635 appendWithSep(resultRemainder, variantDisplayName(variant, temp, TRUE));
636 }
637 resultRemainder.findAndReplace(formatOpenParen, formatReplaceOpenParen);
638 resultRemainder.findAndReplace(formatCloseParen, formatReplaceCloseParen);
639
640 LocalPointer<StringEnumeration> e(locale.createKeywords(status));
641 if (e.isValid() && U_SUCCESS(status)) {
642 UnicodeString temp2;
643 char value[ULOC_KEYWORD_AND_VALUES_CAPACITY]; // sigh, no ULOC_VALUE_CAPACITY
644 const char* key;
645 while ((key = e->next((int32_t *)0, status)) != NULL) {
646 value[0] = 0;
647 locale.getKeywordValue(key, value, ULOC_KEYWORD_AND_VALUES_CAPACITY, status);
648 if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING) {
649 return result;
650 }
651 keyDisplayName(key, temp, TRUE);
652 temp.findAndReplace(formatOpenParen, formatReplaceOpenParen);
653 temp.findAndReplace(formatCloseParen, formatReplaceCloseParen);
654 keyValueDisplayName(key, value, temp2, TRUE);
655 temp2.findAndReplace(formatOpenParen, formatReplaceOpenParen);
656 temp2.findAndReplace(formatCloseParen, formatReplaceCloseParen);
657 if (temp2 != UnicodeString(value, -1, US_INV)) {
658 appendWithSep(resultRemainder, temp2);
659 } else if (temp != UnicodeString(key, -1, US_INV)) {
660 UnicodeString temp3;
661 keyTypeFormat.format(temp, temp2, temp3, status);
662 appendWithSep(resultRemainder, temp3);
663 } else {
664 appendWithSep(resultRemainder, temp)
665 .append((UChar)0x3d /* = */)
666 .append(temp2);
667 }
668 }
669 }
670
671 if (!resultRemainder.isEmpty()) {
672 format.format(resultName, resultRemainder, result.remove(), status);
673 return adjustForUsageAndContext(kCapContextUsageLanguage, result);
674 }
675
676 result = resultName;
677 return adjustForUsageAndContext(kCapContextUsageLanguage, result);
678 }
679
680 UnicodeString&
681 LocaleDisplayNamesImpl::appendWithSep(UnicodeString& buffer, const UnicodeString& src) const {
682 if (buffer.isEmpty()) {
683 buffer.setTo(src);
684 } else {
685 const UnicodeString *values[2] = { &buffer, &src };
686 UErrorCode status = U_ZERO_ERROR;
687 separatorFormat.formatAndReplace(values, 2, buffer, NULL, 0, status);
688 }
689 return buffer;
690 }
691
692 UnicodeString&
693 LocaleDisplayNamesImpl::localeDisplayName(const char* localeId,
694 UnicodeString& result) const {
695 return localeDisplayName(Locale(localeId), result);
696 }
697
698 // private
699 UnicodeString&
700 LocaleDisplayNamesImpl::localeIdName(const char* localeId,
701 UnicodeString& result) const {
702 if (nameLength == UDISPCTX_LENGTH_SHORT) {
703 langData.getNoFallback("Languages%short", localeId, result);
704 if (!result.isBogus()) {
705 return result;
706 }
707 }
708 return langData.getNoFallback("Languages", localeId, result);
709 }
710
711 UnicodeString&
712 LocaleDisplayNamesImpl::languageDisplayName(const char* lang,
713 UnicodeString& result) const {
714 if (uprv_strcmp("root", lang) == 0 || uprv_strchr(lang, '_') != NULL) {
715 return result = UnicodeString(lang, -1, US_INV);
716 }
717 if (nameLength == UDISPCTX_LENGTH_SHORT) {
718 langData.getNoFallback("Languages%short", lang, result);
719 if (!result.isBogus()) {
720 return adjustForUsageAndContext(kCapContextUsageLanguage, result);
721 }
722 }
723 langData.get("Languages", lang, result);
724 return adjustForUsageAndContext(kCapContextUsageLanguage, result);
725 }
726
727 UnicodeString&
728 LocaleDisplayNamesImpl::scriptDisplayName(const char* script,
729 UnicodeString& result,
730 UBool skipAdjust) const {
731 if (!skipAdjust) { // => prefer standalone
732 langData.getNoFallback("Scripts%stand-alone", script, result);
733 if (!result.isBogus()) {
734 return adjustForUsageAndContext(kCapContextUsageScript, result);
735 }
736 }
737 if (nameLength == UDISPCTX_LENGTH_SHORT) {
738 langData.getNoFallback("Scripts%short", script, result);
739 if (!result.isBogus()) {
740 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageScript, result);
741 }
742 }
743 langData.get("Scripts", script, result);
744 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageScript, result);
745 }
746
747 UnicodeString&
748 LocaleDisplayNamesImpl::scriptDisplayName(const char* script,
749 UnicodeString& result) const {
750 return scriptDisplayName(script, result, FALSE);
751 }
752
753 UnicodeString&
754 LocaleDisplayNamesImpl::scriptDisplayName(UScriptCode scriptCode,
755 UnicodeString& result) const {
756 return scriptDisplayName(uscript_getName(scriptCode), result, FALSE);
757 }
758
759 UnicodeString&
760 LocaleDisplayNamesImpl::regionDisplayName(const char* region,
761 UnicodeString& result,
762 UBool skipAdjust) const {
763 if (nameLength == UDISPCTX_LENGTH_SHORT) {
764 regionData.getNoFallback("Countries%short", region, result);
765 if (!result.isBogus()) {
766 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageTerritory, result);
767 }
768 }
769 regionData.get("Countries", region, result);
770 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageTerritory, result);
771 }
772
773 // private Apple, only for building localeDisplayName
774 // (use short region, don't adjust for context)
775 UnicodeString&
776 LocaleDisplayNamesImpl::regionShortDisplayName(const char* region,
777 UnicodeString& result) const {
778 if (uprv_strcmp(region, "PS") != 0) {
779 regionData.getNoFallback("Countries%short", region, result);
780 if (!result.isBogus()) {
781 return result;
782 }
783 }
784 regionData.get("Countries", region, result);
785 return result;
786 }
787
788 UnicodeString&
789 LocaleDisplayNamesImpl::regionDisplayName(const char* region,
790 UnicodeString& result) const {
791 return regionDisplayName(region, result, FALSE);
792 }
793
794
795 UnicodeString&
796 LocaleDisplayNamesImpl::variantDisplayName(const char* variant,
797 UnicodeString& result,
798 UBool skipAdjust) const {
799 // don't have a resource for short variant names
800 langData.get("Variants", variant, result);
801 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageVariant, result);
802 }
803
804 UnicodeString&
805 LocaleDisplayNamesImpl::variantDisplayName(const char* variant,
806 UnicodeString& result) const {
807 return variantDisplayName(variant, result, FALSE);
808 }
809
810 UnicodeString&
811 LocaleDisplayNamesImpl::keyDisplayName(const char* key,
812 UnicodeString& result,
813 UBool skipAdjust) const {
814 // don't have a resource for short key names
815 langData.get("Keys", key, result);
816 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKey, result);
817 }
818
819 UnicodeString&
820 LocaleDisplayNamesImpl::keyDisplayName(const char* key,
821 UnicodeString& result) const {
822 return keyDisplayName(key, result, FALSE);
823 }
824
825 UnicodeString&
826 LocaleDisplayNamesImpl::keyValueDisplayName(const char* key,
827 const char* value,
828 UnicodeString& result,
829 UBool skipAdjust) const {
830 if (uprv_strcmp(key, "currency") == 0) {
831 // ICU4C does not have ICU4J CurrencyDisplayInfo equivalent for now.
832 UErrorCode sts = U_ZERO_ERROR;
833 UnicodeString ustrValue(value, -1, US_INV);
834 int32_t len;
835 UBool isChoice = FALSE;
836 const UChar *currencyName = ucurr_getName(ustrValue.getTerminatedBuffer(),
837 locale.getBaseName(), UCURR_LONG_NAME, &isChoice, &len, &sts);
838 if (U_FAILURE(sts)) {
839 // Return the value as is on failure
840 result = ustrValue;
841 return result;
842 }
843 result.setTo(currencyName, len);
844 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result);
845 }
846
847 if (nameLength == UDISPCTX_LENGTH_SHORT) {
848 langData.get("Types%short", key, value, result);
849 if (!result.isBogus()) {
850 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result);
851 }
852 }
853 langData.get("Types", key, value, result);
854 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result);
855 }
856
857 UnicodeString&
858 LocaleDisplayNamesImpl::keyValueDisplayName(const char* key,
859 const char* value,
860 UnicodeString& result) const {
861 return keyValueDisplayName(key, value, result, FALSE);
862 }
863
864 ////////////////////////////////////////////////////////////////////////////////////////////////////
865
866 LocaleDisplayNames*
867 LocaleDisplayNames::createInstance(const Locale& locale,
868 UDialectHandling dialectHandling) {
869 return new LocaleDisplayNamesImpl(locale, dialectHandling);
870 }
871
872 LocaleDisplayNames*
873 LocaleDisplayNames::createInstance(const Locale& locale,
874 UDisplayContext *contexts, int32_t length) {
875 if (contexts == NULL) {
876 length = 0;
877 }
878 return new LocaleDisplayNamesImpl(locale, contexts, length);
879 }
880
881 U_NAMESPACE_END
882
883 ////////////////////////////////////////////////////////////////////////////////////////////////////
884
885 U_NAMESPACE_USE
886
887 U_CAPI ULocaleDisplayNames * U_EXPORT2
888 uldn_open(const char * locale,
889 UDialectHandling dialectHandling,
890 UErrorCode *pErrorCode) {
891 if (U_FAILURE(*pErrorCode)) {
892 return 0;
893 }
894 if (locale == NULL) {
895 locale = uloc_getDefault();
896 }
897 return (ULocaleDisplayNames *)LocaleDisplayNames::createInstance(Locale(locale), dialectHandling);
898 }
899
900 U_CAPI ULocaleDisplayNames * U_EXPORT2
901 uldn_openForContext(const char * locale,
902 UDisplayContext *contexts, int32_t length,
903 UErrorCode *pErrorCode) {
904 if (U_FAILURE(*pErrorCode)) {
905 return 0;
906 }
907 if (locale == NULL) {
908 locale = uloc_getDefault();
909 }
910 return (ULocaleDisplayNames *)LocaleDisplayNames::createInstance(Locale(locale), contexts, length);
911 }
912
913
914 U_CAPI void U_EXPORT2
915 uldn_close(ULocaleDisplayNames *ldn) {
916 delete (LocaleDisplayNames *)ldn;
917 }
918
919 U_CAPI const char * U_EXPORT2
920 uldn_getLocale(const ULocaleDisplayNames *ldn) {
921 if (ldn) {
922 return ((const LocaleDisplayNames *)ldn)->getLocale().getName();
923 }
924 return NULL;
925 }
926
927 U_CAPI UDialectHandling U_EXPORT2
928 uldn_getDialectHandling(const ULocaleDisplayNames *ldn) {
929 if (ldn) {
930 return ((const LocaleDisplayNames *)ldn)->getDialectHandling();
931 }
932 return ULDN_STANDARD_NAMES;
933 }
934
935 U_CAPI UDisplayContext U_EXPORT2
936 uldn_getContext(const ULocaleDisplayNames *ldn,
937 UDisplayContextType type,
938 UErrorCode *pErrorCode) {
939 if (U_FAILURE(*pErrorCode)) {
940 return (UDisplayContext)0;
941 }
942 return ((const LocaleDisplayNames *)ldn)->getContext(type);
943 }
944
945 U_CAPI int32_t U_EXPORT2
946 uldn_localeDisplayName(const ULocaleDisplayNames *ldn,
947 const char *locale,
948 UChar *result,
949 int32_t maxResultSize,
950 UErrorCode *pErrorCode) {
951 if (U_FAILURE(*pErrorCode)) {
952 return 0;
953 }
954 if (ldn == NULL || locale == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
955 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
956 return 0;
957 }
958 UnicodeString temp(result, 0, maxResultSize);
959 ((const LocaleDisplayNames *)ldn)->localeDisplayName(locale, temp);
960 if (temp.isBogus()) {
961 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
962 return 0;
963 }
964 return temp.extract(result, maxResultSize, *pErrorCode);
965 }
966
967 U_CAPI int32_t U_EXPORT2
968 uldn_languageDisplayName(const ULocaleDisplayNames *ldn,
969 const char *lang,
970 UChar *result,
971 int32_t maxResultSize,
972 UErrorCode *pErrorCode) {
973 if (U_FAILURE(*pErrorCode)) {
974 return 0;
975 }
976 if (ldn == NULL || lang == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
977 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
978 return 0;
979 }
980 UnicodeString temp(result, 0, maxResultSize);
981 ((const LocaleDisplayNames *)ldn)->languageDisplayName(lang, temp);
982 return temp.extract(result, maxResultSize, *pErrorCode);
983 }
984
985 U_CAPI int32_t U_EXPORT2
986 uldn_scriptDisplayName(const ULocaleDisplayNames *ldn,
987 const char *script,
988 UChar *result,
989 int32_t maxResultSize,
990 UErrorCode *pErrorCode) {
991 if (U_FAILURE(*pErrorCode)) {
992 return 0;
993 }
994 if (ldn == NULL || script == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
995 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
996 return 0;
997 }
998 UnicodeString temp(result, 0, maxResultSize);
999 ((const LocaleDisplayNames *)ldn)->scriptDisplayName(script, temp);
1000 return temp.extract(result, maxResultSize, *pErrorCode);
1001 }
1002
1003 U_CAPI int32_t U_EXPORT2
1004 uldn_scriptCodeDisplayName(const ULocaleDisplayNames *ldn,
1005 UScriptCode scriptCode,
1006 UChar *result,
1007 int32_t maxResultSize,
1008 UErrorCode *pErrorCode) {
1009 return uldn_scriptDisplayName(ldn, uscript_getName(scriptCode), result, maxResultSize, pErrorCode);
1010 }
1011
1012 U_CAPI int32_t U_EXPORT2
1013 uldn_regionDisplayName(const ULocaleDisplayNames *ldn,
1014 const char *region,
1015 UChar *result,
1016 int32_t maxResultSize,
1017 UErrorCode *pErrorCode) {
1018 if (U_FAILURE(*pErrorCode)) {
1019 return 0;
1020 }
1021 if (ldn == NULL || region == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1022 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1023 return 0;
1024 }
1025 UnicodeString temp(result, 0, maxResultSize);
1026 ((const LocaleDisplayNames *)ldn)->regionDisplayName(region, temp);
1027 return temp.extract(result, maxResultSize, *pErrorCode);
1028 }
1029
1030 U_CAPI int32_t U_EXPORT2
1031 uldn_variantDisplayName(const ULocaleDisplayNames *ldn,
1032 const char *variant,
1033 UChar *result,
1034 int32_t maxResultSize,
1035 UErrorCode *pErrorCode) {
1036 if (U_FAILURE(*pErrorCode)) {
1037 return 0;
1038 }
1039 if (ldn == NULL || variant == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1040 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1041 return 0;
1042 }
1043 UnicodeString temp(result, 0, maxResultSize);
1044 ((const LocaleDisplayNames *)ldn)->variantDisplayName(variant, temp);
1045 return temp.extract(result, maxResultSize, *pErrorCode);
1046 }
1047
1048 U_CAPI int32_t U_EXPORT2
1049 uldn_keyDisplayName(const ULocaleDisplayNames *ldn,
1050 const char *key,
1051 UChar *result,
1052 int32_t maxResultSize,
1053 UErrorCode *pErrorCode) {
1054 if (U_FAILURE(*pErrorCode)) {
1055 return 0;
1056 }
1057 if (ldn == NULL || key == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1058 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1059 return 0;
1060 }
1061 UnicodeString temp(result, 0, maxResultSize);
1062 ((const LocaleDisplayNames *)ldn)->keyDisplayName(key, temp);
1063 return temp.extract(result, maxResultSize, *pErrorCode);
1064 }
1065
1066 U_CAPI int32_t U_EXPORT2
1067 uldn_keyValueDisplayName(const ULocaleDisplayNames *ldn,
1068 const char *key,
1069 const char *value,
1070 UChar *result,
1071 int32_t maxResultSize,
1072 UErrorCode *pErrorCode) {
1073 if (U_FAILURE(*pErrorCode)) {
1074 return 0;
1075 }
1076 if (ldn == NULL || key == NULL || value == NULL || (result == NULL && maxResultSize > 0)
1077 || maxResultSize < 0) {
1078 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1079 return 0;
1080 }
1081 UnicodeString temp(result, 0, maxResultSize);
1082 ((const LocaleDisplayNames *)ldn)->keyValueDisplayName(key, value, temp);
1083 return temp.extract(result, maxResultSize, *pErrorCode);
1084 }
1085
1086 #endif