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 */
37static 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 *)) != 0) {
49 char c;
50 while (p != e && (c = *str++) != 0) {
51 *p++ = c;
52 }
53 }
54 *p = 0;
55 va_end(args);
56
57 return static_cast<int32_t>(p - buffer);
58}
59
60U_NAMESPACE_BEGIN
61
62////////////////////////////////////////////////////////////////////////////////////////////////////
63
64// Access resource data for locale components.
65// Wrap code in uloc.c for now.
66class ICUDataTable {
67 const char* path;
68 Locale locale;
69
70public:
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
87inline UnicodeString &
88ICUDataTable::get(const char* tableKey, const char* itemKey, UnicodeString& result) const {
89 return get(tableKey, NULL, itemKey, result);
90}
91
92inline UnicodeString &
93ICUDataTable::getNoFallback(const char* tableKey, const char* itemKey, UnicodeString& result) const {
94 return getNoFallback(tableKey, NULL, itemKey, result);
95}
96
97ICUDataTable::ICUDataTable(const char* path, const Locale& locale)
98 : path(NULL), locale(Locale::getRoot())
99{
100 if (path) {
101 int32_t len = static_cast<int32_t>(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
110ICUDataTable::~ICUDataTable() {
111 if (path) {
112 uprv_free((void*) path);
113 path = NULL;
114 }
115}
116
117const Locale&
118ICUDataTable::getLocale() {
119 return locale;
120}
121
122UnicodeString &
123ICUDataTable::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
137UnicodeString &
138ICUDataTable::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
156LocaleDisplayNames::~LocaleDisplayNames() {}
157
158////////////////////////////////////////////////////////////////////////////////////////////////////
159
160#if 0 // currently unused
161
162class DefaultLocaleDisplayNames : public LocaleDisplayNames {
163 UDialectHandling dialectHandling;
164
165public:
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
195DefaultLocaleDisplayNames::DefaultLocaleDisplayNames(UDialectHandling dialectHandling)
196 : dialectHandling(dialectHandling) {
197}
198
199DefaultLocaleDisplayNames::~DefaultLocaleDisplayNames() {
200}
201
202const Locale&
203DefaultLocaleDisplayNames::getLocale() const {
204 return Locale::getRoot();
205}
206
207UDialectHandling
208DefaultLocaleDisplayNames::getDialectHandling() const {
209 return dialectHandling;
210}
211
212UnicodeString&
213DefaultLocaleDisplayNames::localeDisplayName(const Locale& locale,
214 UnicodeString& result) const {
215 return result = UnicodeString(locale.getName(), -1, US_INV);
216}
217
218UnicodeString&
219DefaultLocaleDisplayNames::localeDisplayName(const char* localeId,
220 UnicodeString& result) const {
221 return result = UnicodeString(localeId, -1, US_INV);
222}
223
224UnicodeString&
225DefaultLocaleDisplayNames::languageDisplayName(const char* lang,
226 UnicodeString& result) const {
227 return result = UnicodeString(lang, -1, US_INV);
228}
229
230UnicodeString&
231DefaultLocaleDisplayNames::scriptDisplayName(const char* script,
232 UnicodeString& result) const {
233 return result = UnicodeString(script, -1, US_INV);
234}
235
236UnicodeString&
237DefaultLocaleDisplayNames::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
246UnicodeString&
247DefaultLocaleDisplayNames::regionDisplayName(const char* region,
248 UnicodeString& result) const {
249 return result = UnicodeString(region, -1, US_INV);
250}
251
252UnicodeString&
253DefaultLocaleDisplayNames::variantDisplayName(const char* variant,
254 UnicodeString& result) const {
255 return result = UnicodeString(variant, -1, US_INV);
256}
257
258UnicodeString&
259DefaultLocaleDisplayNames::keyDisplayName(const char* key,
260 UnicodeString& result) const {
261 return result = UnicodeString(key, -1, US_INV);
262}
263
264UnicodeString&
265DefaultLocaleDisplayNames::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
275class 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 UnicodeString formatOpenParen;
290 UnicodeString formatReplaceOpenParen;
291 UnicodeString formatCloseParen;
292 UnicodeString formatReplaceCloseParen;
293 UDisplayContext nameLength;
294 UDisplayContext substitute;
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
310public:
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;
339private:
340 UnicodeString& localeIdName(const char* localeId,
341 UnicodeString& result, bool substitute) const;
342 UnicodeString& appendWithSep(UnicodeString& buffer, const UnicodeString& src) const;
343 UnicodeString& adjustForUsageAndContext(CapContextUsage usage, UnicodeString& result) const;
344 UnicodeString& scriptDisplayName(const char* script, UnicodeString& result, UBool skipAdjust) const;
345 UnicodeString& regionDisplayName(const char* region, UnicodeString& result, UBool skipAdjust) const;
346 UnicodeString& variantDisplayName(const char* variant, UnicodeString& result, UBool skipAdjust) const;
347 UnicodeString& keyDisplayName(const char* key, UnicodeString& result, UBool skipAdjust) const;
348 UnicodeString& keyValueDisplayName(const char* key, const char* value,
349 UnicodeString& result, UBool skipAdjust) const;
350 void initialize(void);
351
352 struct CapitalizationContextSink;
353};
354
355LocaleDisplayNamesImpl::LocaleDisplayNamesImpl(const Locale& locale,
356 UDialectHandling dialectHandling)
357 : dialectHandling(dialectHandling)
358 , langData(U_ICUDATA_LANG, locale)
359 , regionData(U_ICUDATA_REGION, locale)
360 , capitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
361 , capitalizationBrkIter(NULL)
362 , nameLength(UDISPCTX_LENGTH_FULL)
363 , substitute(UDISPCTX_SUBSTITUTE)
364{
365 initialize();
366}
367
368LocaleDisplayNamesImpl::LocaleDisplayNamesImpl(const Locale& locale,
369 UDisplayContext *contexts, int32_t length)
370 : dialectHandling(ULDN_STANDARD_NAMES)
371 , langData(U_ICUDATA_LANG, locale)
372 , regionData(U_ICUDATA_REGION, locale)
373 , capitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
374 , capitalizationBrkIter(NULL)
375 , nameLength(UDISPCTX_LENGTH_FULL)
376 , substitute(UDISPCTX_SUBSTITUTE)
377{
378 while (length-- > 0) {
379 UDisplayContext value = *contexts++;
380 UDisplayContextType selector = (UDisplayContextType)((uint32_t)value >> 8);
381 switch (selector) {
382 case UDISPCTX_TYPE_DIALECT_HANDLING:
383 dialectHandling = (UDialectHandling)value;
384 break;
385 case UDISPCTX_TYPE_CAPITALIZATION:
386 capitalizationContext = value;
387 break;
388 case UDISPCTX_TYPE_DISPLAY_LENGTH:
389 nameLength = value;
390 break;
391 case UDISPCTX_TYPE_SUBSTITUTE_HANDLING:
392 substitute = value;
393 break;
394 default:
395 break;
396 }
397 }
398 initialize();
399}
400
401struct LocaleDisplayNamesImpl::CapitalizationContextSink : public ResourceSink {
402 UBool hasCapitalizationUsage;
403 LocaleDisplayNamesImpl& parent;
404
405 CapitalizationContextSink(LocaleDisplayNamesImpl& _parent)
406 : hasCapitalizationUsage(FALSE), parent(_parent) {}
407 virtual ~CapitalizationContextSink();
408
409 virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
410 UErrorCode &errorCode) {
411 ResourceTable contexts = value.getTable(errorCode);
412 if (U_FAILURE(errorCode)) { return; }
413 for (int i = 0; contexts.getKeyAndValue(i, key, value); ++i) {
414
415 CapContextUsage usageEnum;
416 if (uprv_strcmp(key, "key") == 0) {
417 usageEnum = kCapContextUsageKey;
418 } else if (uprv_strcmp(key, "keyValue") == 0) {
419 usageEnum = kCapContextUsageKeyValue;
420 } else if (uprv_strcmp(key, "languages") == 0) {
421 usageEnum = kCapContextUsageLanguage;
422 } else if (uprv_strcmp(key, "script") == 0) {
423 usageEnum = kCapContextUsageScript;
424 } else if (uprv_strcmp(key, "territory") == 0) {
425 usageEnum = kCapContextUsageTerritory;
426 } else if (uprv_strcmp(key, "variant") == 0) {
427 usageEnum = kCapContextUsageVariant;
428 } else {
429 continue;
430 }
431
432 int32_t len = 0;
433 const int32_t* intVector = value.getIntVector(len, errorCode);
434 if (U_FAILURE(errorCode)) { return; }
435 if (len < 2) { continue; }
436
437 int32_t titlecaseInt = (parent.capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU) ? intVector[0] : intVector[1];
438 if (titlecaseInt == 0) { continue; }
439
440 parent.fCapitalization[usageEnum] = TRUE;
441 hasCapitalizationUsage = TRUE;
442 }
443 }
444};
445
446// Virtual destructors must be defined out of line.
447LocaleDisplayNamesImpl::CapitalizationContextSink::~CapitalizationContextSink() {}
448
449void
450LocaleDisplayNamesImpl::initialize(void) {
451 LocaleDisplayNamesImpl *nonConstThis = (LocaleDisplayNamesImpl *)this;
452 nonConstThis->locale = langData.getLocale() == Locale::getRoot()
453 ? regionData.getLocale()
454 : langData.getLocale();
455
456 UnicodeString sep;
457 langData.getNoFallback("localeDisplayPattern", "separator", sep);
458 if (sep.isBogus()) {
459 sep = UnicodeString("{0}, {1}", -1, US_INV);
460 }
461 UErrorCode status = U_ZERO_ERROR;
462 separatorFormat.applyPatternMinMaxArguments(sep, 2, 2, status);
463
464 UnicodeString pattern;
465 langData.getNoFallback("localeDisplayPattern", "pattern", pattern);
466 if (pattern.isBogus()) {
467 pattern = UnicodeString("{0} ({1})", -1, US_INV);
468 }
469 format.applyPatternMinMaxArguments(pattern, 2, 2, status);
470 if (pattern.indexOf((UChar)0xFF08) >= 0) {
471 formatOpenParen.setTo((UChar)0xFF08); // fullwidth (
472 formatReplaceOpenParen.setTo((UChar)0xFF3B); // fullwidth [
473 formatCloseParen.setTo((UChar)0xFF09); // fullwidth )
474 formatReplaceCloseParen.setTo((UChar)0xFF3D); // fullwidth ]
475 } else {
476 formatOpenParen.setTo((UChar)0x0028); // (
477 formatReplaceOpenParen.setTo((UChar)0x005B); // [
478 formatCloseParen.setTo((UChar)0x0029); // )
479 formatReplaceCloseParen.setTo((UChar)0x005D); // ]
480 }
481
482 UnicodeString ktPattern;
483 langData.get("localeDisplayPattern", "keyTypePattern", ktPattern);
484 if (ktPattern.isBogus()) {
485 ktPattern = UnicodeString("{0}={1}", -1, US_INV);
486 }
487 keyTypeFormat.applyPatternMinMaxArguments(ktPattern, 2, 2, status);
488
489 uprv_memset(fCapitalization, 0, sizeof(fCapitalization));
490#if !UCONFIG_NO_BREAK_ITERATION
491 // Only get the context data if we need it! This is a const object so we know now...
492 // Also check whether we will need a break iterator (depends on the data)
493 UBool needBrkIter = FALSE;
494 if (capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU || capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_STANDALONE) {
495 LocalUResourceBundlePointer resource(ures_open(NULL, locale.getName(), &status));
496 if (U_FAILURE(status)) { return; }
497 CapitalizationContextSink sink(*this);
498 ures_getAllItemsWithFallback(resource.getAlias(), "contextTransforms", sink, status);
499 if (status == U_MISSING_RESOURCE_ERROR) {
500 // Silently ignore. Not every locale has contextTransforms.
501 status = U_ZERO_ERROR;
502 } else if (U_FAILURE(status)) {
503 return;
504 }
505 needBrkIter = sink.hasCapitalizationUsage;
506 }
507 // Get a sentence break iterator if we will need it
508 if (needBrkIter || capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE) {
509 status = U_ZERO_ERROR;
510 capitalizationBrkIter = BreakIterator::createSentenceInstance(locale, status);
511 if (U_FAILURE(status)) {
512 delete capitalizationBrkIter;
513 capitalizationBrkIter = NULL;
514 }
515 }
516#endif
517}
518
519LocaleDisplayNamesImpl::~LocaleDisplayNamesImpl() {
520#if !UCONFIG_NO_BREAK_ITERATION
521 delete capitalizationBrkIter;
522#endif
523}
524
525const Locale&
526LocaleDisplayNamesImpl::getLocale() const {
527 return locale;
528}
529
530UDialectHandling
531LocaleDisplayNamesImpl::getDialectHandling() const {
532 return dialectHandling;
533}
534
535UDisplayContext
536LocaleDisplayNamesImpl::getContext(UDisplayContextType type) const {
537 switch (type) {
538 case UDISPCTX_TYPE_DIALECT_HANDLING:
539 return (UDisplayContext)dialectHandling;
540 case UDISPCTX_TYPE_CAPITALIZATION:
541 return capitalizationContext;
542 case UDISPCTX_TYPE_DISPLAY_LENGTH:
543 return nameLength;
544 case UDISPCTX_TYPE_SUBSTITUTE_HANDLING:
545 return substitute;
546 default:
547 break;
548 }
549 return (UDisplayContext)0;
550}
551
552UnicodeString&
553LocaleDisplayNamesImpl::adjustForUsageAndContext(CapContextUsage usage,
554 UnicodeString& result) const {
555#if !UCONFIG_NO_BREAK_ITERATION
556 // check to see whether we need to titlecase result
557 if ( result.length() > 0 && u_islower(result.char32At(0)) && capitalizationBrkIter!= NULL &&
558 ( capitalizationContext==UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE || fCapitalization[usage] ) ) {
559 // note fCapitalization[usage] won't be set unless capitalizationContext is UI_LIST_OR_MENU or STANDALONE
560 static UMutex capitalizationBrkIterLock;
561 Mutex lock(&capitalizationBrkIterLock);
562 result.toTitle(capitalizationBrkIter, locale, U_TITLECASE_NO_LOWERCASE | U_TITLECASE_NO_BREAK_ADJUSTMENT);
563 }
564#endif
565 return result;
566}
567
568UnicodeString&
569LocaleDisplayNamesImpl::localeDisplayName(const Locale& loc,
570 UnicodeString& result) const {
571 if (loc.isBogus()) {
572 result.setToBogus();
573 return result;
574 }
575 UnicodeString resultName;
576
577 const char* lang = loc.getLanguage();
578 if (uprv_strlen(lang) == 0) {
579 lang = "root";
580 }
581 const char* script = loc.getScript();
582 const char* country = loc.getCountry();
583 const char* variant = loc.getVariant();
584
585 UBool hasScript = uprv_strlen(script) > 0;
586 UBool hasCountry = uprv_strlen(country) > 0;
587 UBool hasVariant = uprv_strlen(variant) > 0;
588
589 if (dialectHandling == ULDN_DIALECT_NAMES) {
590 char buffer[ULOC_FULLNAME_CAPACITY];
591 do { // loop construct is so we can break early out of search
592 if (hasScript && hasCountry) {
593 ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", script, "_", country, (char *)0);
594 localeIdName(buffer, resultName, false);
595 if (!resultName.isBogus()) {
596 hasScript = FALSE;
597 hasCountry = FALSE;
598 break;
599 }
600 }
601 if (hasScript) {
602 ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", script, (char *)0);
603 localeIdName(buffer, resultName, false);
604 if (!resultName.isBogus()) {
605 hasScript = FALSE;
606 break;
607 }
608 }
609 if (hasCountry) {
610 ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", country, (char*)0);
611 localeIdName(buffer, resultName, false);
612 if (!resultName.isBogus()) {
613 hasCountry = FALSE;
614 break;
615 }
616 }
617 } while (FALSE);
618 }
619 if (resultName.isBogus() || resultName.isEmpty()) {
620 localeIdName(lang, resultName, substitute == UDISPCTX_SUBSTITUTE);
621 if (resultName.isBogus()) {
622 result.setToBogus();
623 return result;
624 }
625 }
626
627 UnicodeString resultRemainder;
628 UnicodeString temp;
629 UErrorCode status = U_ZERO_ERROR;
630
631 if (hasScript) {
632 UnicodeString script_str = scriptDisplayName(script, temp, TRUE);
633 if (script_str.isBogus()) {
634 result.setToBogus();
635 return result;
636 }
637 resultRemainder.append(script_str);
638 }
639 if (hasCountry) {
640 UnicodeString region_str = regionDisplayName(country, temp, TRUE);
641 if (region_str.isBogus()) {
642 result.setToBogus();
643 return result;
644 }
645 appendWithSep(resultRemainder, region_str);
646 }
647 if (hasVariant) {
648 UnicodeString variant_str = variantDisplayName(variant, temp, TRUE);
649 if (variant_str.isBogus()) {
650 result.setToBogus();
651 return result;
652 }
653 appendWithSep(resultRemainder, variant_str);
654 }
655 resultRemainder.findAndReplace(formatOpenParen, formatReplaceOpenParen);
656 resultRemainder.findAndReplace(formatCloseParen, formatReplaceCloseParen);
657
658 LocalPointer<StringEnumeration> e(loc.createKeywords(status));
659 if (e.isValid() && U_SUCCESS(status)) {
660 UnicodeString temp2;
661 char value[ULOC_KEYWORD_AND_VALUES_CAPACITY]; // sigh, no ULOC_VALUE_CAPACITY
662 const char* key;
663 while ((key = e->next((int32_t *)0, status)) != NULL) {
664 value[0] = 0;
665 loc.getKeywordValue(key, value, ULOC_KEYWORD_AND_VALUES_CAPACITY, status);
666 if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING) {
667 return result;
668 }
669 keyDisplayName(key, temp, TRUE);
670 temp.findAndReplace(formatOpenParen, formatReplaceOpenParen);
671 temp.findAndReplace(formatCloseParen, formatReplaceCloseParen);
672 keyValueDisplayName(key, value, temp2, TRUE);
673 temp2.findAndReplace(formatOpenParen, formatReplaceOpenParen);
674 temp2.findAndReplace(formatCloseParen, formatReplaceCloseParen);
675 if (temp2 != UnicodeString(value, -1, US_INV)) {
676 appendWithSep(resultRemainder, temp2);
677 } else if (temp != UnicodeString(key, -1, US_INV)) {
678 UnicodeString temp3;
679 keyTypeFormat.format(temp, temp2, temp3, status);
680 appendWithSep(resultRemainder, temp3);
681 } else {
682 appendWithSep(resultRemainder, temp)
683 .append((UChar)0x3d /* = */)
684 .append(temp2);
685 }
686 }
687 }
688
689 if (!resultRemainder.isEmpty()) {
690 format.format(resultName, resultRemainder, result.remove(), status);
691 return adjustForUsageAndContext(kCapContextUsageLanguage, result);
692 }
693
694 result = resultName;
695 return adjustForUsageAndContext(kCapContextUsageLanguage, result);
696}
697
698UnicodeString&
699LocaleDisplayNamesImpl::appendWithSep(UnicodeString& buffer, const UnicodeString& src) const {
700 if (buffer.isEmpty()) {
701 buffer.setTo(src);
702 } else {
703 const UnicodeString *values[2] = { &buffer, &src };
704 UErrorCode status = U_ZERO_ERROR;
705 separatorFormat.formatAndReplace(values, 2, buffer, NULL, 0, status);
706 }
707 return buffer;
708}
709
710UnicodeString&
711LocaleDisplayNamesImpl::localeDisplayName(const char* localeId,
712 UnicodeString& result) const {
713 return localeDisplayName(Locale(localeId), result);
714}
715
716// private
717UnicodeString&
718LocaleDisplayNamesImpl::localeIdName(const char* localeId,
719 UnicodeString& result, bool substitute) const {
720 if (nameLength == UDISPCTX_LENGTH_SHORT) {
721 langData.getNoFallback("Languages%short", localeId, result);
722 if (!result.isBogus()) {
723 return result;
724 }
725 }
726 if (substitute) {
727 return langData.get("Languages", localeId, result);
728 } else {
729 return langData.getNoFallback("Languages", localeId, result);
730 }
731}
732
733UnicodeString&
734LocaleDisplayNamesImpl::languageDisplayName(const char* lang,
735 UnicodeString& result) const {
736 if (uprv_strcmp("root", lang) == 0 || uprv_strchr(lang, '_') != NULL) {
737 return result = UnicodeString(lang, -1, US_INV);
738 }
739 if (nameLength == UDISPCTX_LENGTH_SHORT) {
740 langData.getNoFallback("Languages%short", lang, result);
741 if (!result.isBogus()) {
742 return adjustForUsageAndContext(kCapContextUsageLanguage, result);
743 }
744 }
745 if (substitute == UDISPCTX_SUBSTITUTE) {
746 langData.get("Languages", lang, result);
747 } else {
748 langData.getNoFallback("Languages", lang, result);
749 }
750 return adjustForUsageAndContext(kCapContextUsageLanguage, result);
751}
752
753UnicodeString&
754LocaleDisplayNamesImpl::scriptDisplayName(const char* script,
755 UnicodeString& result,
756 UBool skipAdjust) const {
757 if (nameLength == UDISPCTX_LENGTH_SHORT) {
758 langData.getNoFallback("Scripts%short", script, result);
759 if (!result.isBogus()) {
760 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageScript, result);
761 }
762 }
763 if (substitute == UDISPCTX_SUBSTITUTE) {
764 langData.get("Scripts", script, result);
765 } else {
766 langData.getNoFallback("Scripts", script, result);
767 }
768 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageScript, result);
769}
770
771UnicodeString&
772LocaleDisplayNamesImpl::scriptDisplayName(const char* script,
773 UnicodeString& result) const {
774 return scriptDisplayName(script, result, FALSE);
775}
776
777UnicodeString&
778LocaleDisplayNamesImpl::scriptDisplayName(UScriptCode scriptCode,
779 UnicodeString& result) const {
780 return scriptDisplayName(uscript_getName(scriptCode), result, FALSE);
781}
782
783UnicodeString&
784LocaleDisplayNamesImpl::regionDisplayName(const char* region,
785 UnicodeString& result,
786 UBool skipAdjust) const {
787 if (nameLength == UDISPCTX_LENGTH_SHORT) {
788 regionData.getNoFallback("Countries%short", region, result);
789 if (!result.isBogus()) {
790 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageTerritory, result);
791 }
792 }
793 if (substitute == UDISPCTX_SUBSTITUTE) {
794 regionData.get("Countries", region, result);
795 } else {
796 regionData.getNoFallback("Countries", region, result);
797 }
798 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageTerritory, result);
799}
800
801UnicodeString&
802LocaleDisplayNamesImpl::regionDisplayName(const char* region,
803 UnicodeString& result) const {
804 return regionDisplayName(region, result, FALSE);
805}
806
807
808UnicodeString&
809LocaleDisplayNamesImpl::variantDisplayName(const char* variant,
810 UnicodeString& result,
811 UBool skipAdjust) const {
812 // don't have a resource for short variant names
813 if (substitute == UDISPCTX_SUBSTITUTE) {
814 langData.get("Variants", variant, result);
815 } else {
816 langData.getNoFallback("Variants", variant, result);
817 }
818 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageVariant, result);
819}
820
821UnicodeString&
822LocaleDisplayNamesImpl::variantDisplayName(const char* variant,
823 UnicodeString& result) const {
824 return variantDisplayName(variant, result, FALSE);
825}
826
827UnicodeString&
828LocaleDisplayNamesImpl::keyDisplayName(const char* key,
829 UnicodeString& result,
830 UBool skipAdjust) const {
831 // don't have a resource for short key names
832 if (substitute == UDISPCTX_SUBSTITUTE) {
833 langData.get("Keys", key, result);
834 } else {
835 langData.getNoFallback("Keys", key, result);
836 }
837 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKey, result);
838}
839
840UnicodeString&
841LocaleDisplayNamesImpl::keyDisplayName(const char* key,
842 UnicodeString& result) const {
843 return keyDisplayName(key, result, FALSE);
844}
845
846UnicodeString&
847LocaleDisplayNamesImpl::keyValueDisplayName(const char* key,
848 const char* value,
849 UnicodeString& result,
850 UBool skipAdjust) const {
851 if (uprv_strcmp(key, "currency") == 0) {
852 // ICU4C does not have ICU4J CurrencyDisplayInfo equivalent for now.
853 UErrorCode sts = U_ZERO_ERROR;
854 UnicodeString ustrValue(value, -1, US_INV);
855 int32_t len;
856 const UChar *currencyName = ucurr_getName(ustrValue.getTerminatedBuffer(),
857 locale.getBaseName(), UCURR_LONG_NAME, nullptr /* isChoiceFormat */, &len, &sts);
858 if (U_FAILURE(sts)) {
859 // Return the value as is on failure
860 result = ustrValue;
861 return result;
862 }
863 result.setTo(currencyName, len);
864 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result);
865 }
866
867 if (nameLength == UDISPCTX_LENGTH_SHORT) {
868 langData.getNoFallback("Types%short", key, value, result);
869 if (!result.isBogus()) {
870 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result);
871 }
872 }
873 if (substitute == UDISPCTX_SUBSTITUTE) {
874 langData.get("Types", key, value, result);
875 } else {
876 langData.getNoFallback("Types", key, value, result);
877 }
878 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result);
879}
880
881UnicodeString&
882LocaleDisplayNamesImpl::keyValueDisplayName(const char* key,
883 const char* value,
884 UnicodeString& result) const {
885 return keyValueDisplayName(key, value, result, FALSE);
886}
887
888////////////////////////////////////////////////////////////////////////////////////////////////////
889
890LocaleDisplayNames*
891LocaleDisplayNames::createInstance(const Locale& locale,
892 UDialectHandling dialectHandling) {
893 return new LocaleDisplayNamesImpl(locale, dialectHandling);
894}
895
896LocaleDisplayNames*
897LocaleDisplayNames::createInstance(const Locale& locale,
898 UDisplayContext *contexts, int32_t length) {
899 if (contexts == NULL) {
900 length = 0;
901 }
902 return new LocaleDisplayNamesImpl(locale, contexts, length);
903}
904
905U_NAMESPACE_END
906
907////////////////////////////////////////////////////////////////////////////////////////////////////
908
909U_NAMESPACE_USE
910
911U_CAPI ULocaleDisplayNames * U_EXPORT2
912uldn_open(const char * locale,
913 UDialectHandling dialectHandling,
914 UErrorCode *pErrorCode) {
915 if (U_FAILURE(*pErrorCode)) {
916 return 0;
917 }
918 if (locale == NULL) {
919 locale = uloc_getDefault();
920 }
921 return (ULocaleDisplayNames *)LocaleDisplayNames::createInstance(Locale(locale), dialectHandling);
922}
923
924U_CAPI ULocaleDisplayNames * U_EXPORT2
925uldn_openForContext(const char * locale,
926 UDisplayContext *contexts, int32_t length,
927 UErrorCode *pErrorCode) {
928 if (U_FAILURE(*pErrorCode)) {
929 return 0;
930 }
931 if (locale == NULL) {
932 locale = uloc_getDefault();
933 }
934 return (ULocaleDisplayNames *)LocaleDisplayNames::createInstance(Locale(locale), contexts, length);
935}
936
937
938U_CAPI void U_EXPORT2
939uldn_close(ULocaleDisplayNames *ldn) {
940 delete (LocaleDisplayNames *)ldn;
941}
942
943U_CAPI const char * U_EXPORT2
944uldn_getLocale(const ULocaleDisplayNames *ldn) {
945 if (ldn) {
946 return ((const LocaleDisplayNames *)ldn)->getLocale().getName();
947 }
948 return NULL;
949}
950
951U_CAPI UDialectHandling U_EXPORT2
952uldn_getDialectHandling(const ULocaleDisplayNames *ldn) {
953 if (ldn) {
954 return ((const LocaleDisplayNames *)ldn)->getDialectHandling();
955 }
956 return ULDN_STANDARD_NAMES;
957}
958
959U_CAPI UDisplayContext U_EXPORT2
960uldn_getContext(const ULocaleDisplayNames *ldn,
961 UDisplayContextType type,
962 UErrorCode *pErrorCode) {
963 if (U_FAILURE(*pErrorCode)) {
964 return (UDisplayContext)0;
965 }
966 return ((const LocaleDisplayNames *)ldn)->getContext(type);
967}
968
969U_CAPI int32_t U_EXPORT2
970uldn_localeDisplayName(const ULocaleDisplayNames *ldn,
971 const char *locale,
972 UChar *result,
973 int32_t maxResultSize,
974 UErrorCode *pErrorCode) {
975 if (U_FAILURE(*pErrorCode)) {
976 return 0;
977 }
978 if (ldn == NULL || locale == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
979 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
980 return 0;
981 }
982 UnicodeString temp(result, 0, maxResultSize);
983 ((const LocaleDisplayNames *)ldn)->localeDisplayName(locale, temp);
984 if (temp.isBogus()) {
985 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
986 return 0;
987 }
988 return temp.extract(result, maxResultSize, *pErrorCode);
989}
990
991U_CAPI int32_t U_EXPORT2
992uldn_languageDisplayName(const ULocaleDisplayNames *ldn,
993 const char *lang,
994 UChar *result,
995 int32_t maxResultSize,
996 UErrorCode *pErrorCode) {
997 if (U_FAILURE(*pErrorCode)) {
998 return 0;
999 }
1000 if (ldn == NULL || lang == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1001 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1002 return 0;
1003 }
1004 UnicodeString temp(result, 0, maxResultSize);
1005 ((const LocaleDisplayNames *)ldn)->languageDisplayName(lang, temp);
1006 return temp.extract(result, maxResultSize, *pErrorCode);
1007}
1008
1009U_CAPI int32_t U_EXPORT2
1010uldn_scriptDisplayName(const ULocaleDisplayNames *ldn,
1011 const char *script,
1012 UChar *result,
1013 int32_t maxResultSize,
1014 UErrorCode *pErrorCode) {
1015 if (U_FAILURE(*pErrorCode)) {
1016 return 0;
1017 }
1018 if (ldn == NULL || script == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1019 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1020 return 0;
1021 }
1022 UnicodeString temp(result, 0, maxResultSize);
1023 ((const LocaleDisplayNames *)ldn)->scriptDisplayName(script, temp);
1024 return temp.extract(result, maxResultSize, *pErrorCode);
1025}
1026
1027U_CAPI int32_t U_EXPORT2
1028uldn_scriptCodeDisplayName(const ULocaleDisplayNames *ldn,
1029 UScriptCode scriptCode,
1030 UChar *result,
1031 int32_t maxResultSize,
1032 UErrorCode *pErrorCode) {
1033 return uldn_scriptDisplayName(ldn, uscript_getName(scriptCode), result, maxResultSize, pErrorCode);
1034}
1035
1036U_CAPI int32_t U_EXPORT2
1037uldn_regionDisplayName(const ULocaleDisplayNames *ldn,
1038 const char *region,
1039 UChar *result,
1040 int32_t maxResultSize,
1041 UErrorCode *pErrorCode) {
1042 if (U_FAILURE(*pErrorCode)) {
1043 return 0;
1044 }
1045 if (ldn == NULL || region == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1046 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1047 return 0;
1048 }
1049 UnicodeString temp(result, 0, maxResultSize);
1050 ((const LocaleDisplayNames *)ldn)->regionDisplayName(region, temp);
1051 return temp.extract(result, maxResultSize, *pErrorCode);
1052}
1053
1054U_CAPI int32_t U_EXPORT2
1055uldn_variantDisplayName(const ULocaleDisplayNames *ldn,
1056 const char *variant,
1057 UChar *result,
1058 int32_t maxResultSize,
1059 UErrorCode *pErrorCode) {
1060 if (U_FAILURE(*pErrorCode)) {
1061 return 0;
1062 }
1063 if (ldn == NULL || variant == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1064 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1065 return 0;
1066 }
1067 UnicodeString temp(result, 0, maxResultSize);
1068 ((const LocaleDisplayNames *)ldn)->variantDisplayName(variant, temp);
1069 return temp.extract(result, maxResultSize, *pErrorCode);
1070}
1071
1072U_CAPI int32_t U_EXPORT2
1073uldn_keyDisplayName(const ULocaleDisplayNames *ldn,
1074 const char *key,
1075 UChar *result,
1076 int32_t maxResultSize,
1077 UErrorCode *pErrorCode) {
1078 if (U_FAILURE(*pErrorCode)) {
1079 return 0;
1080 }
1081 if (ldn == NULL || key == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1082 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1083 return 0;
1084 }
1085 UnicodeString temp(result, 0, maxResultSize);
1086 ((const LocaleDisplayNames *)ldn)->keyDisplayName(key, temp);
1087 return temp.extract(result, maxResultSize, *pErrorCode);
1088}
1089
1090U_CAPI int32_t U_EXPORT2
1091uldn_keyValueDisplayName(const ULocaleDisplayNames *ldn,
1092 const char *key,
1093 const char *value,
1094 UChar *result,
1095 int32_t maxResultSize,
1096 UErrorCode *pErrorCode) {
1097 if (U_FAILURE(*pErrorCode)) {
1098 return 0;
1099 }
1100 if (ldn == NULL || key == NULL || value == NULL || (result == NULL && maxResultSize > 0)
1101 || maxResultSize < 0) {
1102 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1103 return 0;
1104 }
1105 UnicodeString temp(result, 0, maxResultSize);
1106 ((const LocaleDisplayNames *)ldn)->keyValueDisplayName(key, value, temp);
1107 return temp.extract(result, maxResultSize, *pErrorCode);
1108}
1109
1110#endif
1111