]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/locdspnm.cpp
ICU-59117.0.1.tar.gz
[apple/icu.git] / icuSources / common / locdspnm.cpp
CommitLineData
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 */
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 *))) {
49 char c;
50 while (p != e && (c = *str++)) {
51 *p++ = c;
52 }
53 }
54 *p = 0;
55 va_end(args);
56
57 return 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 {
51004dcb
A
67 const char* path;
68 Locale locale;
729e4ab9
A
69
70public:
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
87inline UnicodeString &
88ICUDataTable::get(const char* tableKey, const char* itemKey, UnicodeString& result) const {
51004dcb 89 return get(tableKey, NULL, itemKey, result);
729e4ab9
A
90}
91
92inline UnicodeString &
93ICUDataTable::getNoFallback(const char* tableKey, const char* itemKey, UnicodeString& result) const {
51004dcb 94 return getNoFallback(tableKey, NULL, itemKey, result);
729e4ab9
A
95}
96
97ICUDataTable::ICUDataTable(const char* path, const Locale& locale)
51004dcb 98 : path(NULL), locale(Locale::getRoot())
729e4ab9
A
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
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);
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
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
4388f060
A
156LocaleDisplayNames::~LocaleDisplayNames() {}
157
729e4ab9
A
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;
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
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 {
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
b331163b 289 static UMutex capitalizationBrkIterLock;
57a6839d
A
290 UnicodeString formatOpenParen;
291 UnicodeString formatReplaceOpenParen;
292 UnicodeString formatCloseParen;
293 UnicodeString formatReplaceCloseParen;
294 UDisplayContext nameLength;
51004dcb
A
295
296 // Constants for capitalization context usage types.
297 enum CapContextUsage {
298 kCapContextUsageLanguage,
299 kCapContextUsageScript,
300 kCapContextUsageTerritory,
301 kCapContextUsageVariant,
302 kCapContextUsageKey,
57a6839d 303 kCapContextUsageKeyValue,
51004dcb
A
304 kCapContextUsageCount
305 };
57a6839d
A
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];
729e4ab9
A
309
310public:
51004dcb
A
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;
729e4ab9 339private:
51004dcb
A
340 UnicodeString& localeIdName(const char* localeId,
341 UnicodeString& result) const;
2ca993e8
A
342 UnicodeString& regionShortDisplayName(const char* region, // Apple-specific
343 UnicodeString& result) const;
51004dcb
A
344 UnicodeString& appendWithSep(UnicodeString& buffer, const UnicodeString& src) const;
345 UnicodeString& adjustForUsageAndContext(CapContextUsage usage, UnicodeString& result) const;
2ca993e8
A
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;
51004dcb 352 void initialize(void);
f3c0d7a5
A
353
354 struct CapitalizationContextSink;
729e4ab9
A
355};
356
b331163b
A
357UMutex LocaleDisplayNamesImpl::capitalizationBrkIterLock = U_MUTEX_INITIALIZER;
358
729e4ab9
A
359LocaleDisplayNamesImpl::LocaleDisplayNamesImpl(const Locale& locale,
360 UDialectHandling dialectHandling)
51004dcb
A
361 : dialectHandling(dialectHandling)
362 , langData(U_ICUDATA_LANG, locale)
363 , regionData(U_ICUDATA_REGION, locale)
51004dcb 364 , capitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
57a6839d 365 , capitalizationBrkIter(NULL)
b331163b 366 , nameLength(UDISPCTX_LENGTH_FULL)
729e4ab9 367{
51004dcb
A
368 initialize();
369}
729e4ab9 370
51004dcb
A
371LocaleDisplayNamesImpl::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)
51004dcb
A
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;
b331163b 390 case UDISPCTX_TYPE_DISPLAY_LENGTH:
57a6839d
A
391 nameLength = value;
392 break;
b331163b
A
393 case UADISPCTX_TYPE_LENGTH: // Apple-specific
394 nameLength = (value == UADISPCTX_LENGTH_SHORT)? UDISPCTX_LENGTH_SHORT: UDISPCTX_LENGTH_FULL;
395 break;
51004dcb
A
396 default:
397 break;
398 }
399 }
400 initialize();
401}
729e4ab9 402
f3c0d7a5
A
403struct 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.
449LocaleDisplayNamesImpl::CapitalizationContextSink::~CapitalizationContextSink() {}
450
51004dcb
A
451void
452LocaleDisplayNamesImpl::initialize(void) {
453 LocaleDisplayNamesImpl *nonConstThis = (LocaleDisplayNamesImpl *)this;
454 nonConstThis->locale = langData.getLocale() == Locale::getRoot()
455 ? regionData.getLocale()
456 : langData.getLocale();
4388f060 457
57a6839d 458 UnicodeString sep;
51004dcb
A
459 langData.getNoFallback("localeDisplayPattern", "separator", sep);
460 if (sep.isBogus()) {
57a6839d 461 sep = UnicodeString("{0}, {1}", -1, US_INV);
51004dcb 462 }
57a6839d 463 UErrorCode status = U_ZERO_ERROR;
2ca993e8 464 separatorFormat.applyPatternMinMaxArguments(sep, 2, 2, status);
51004dcb
A
465
466 UnicodeString pattern;
467 langData.getNoFallback("localeDisplayPattern", "pattern", pattern);
468 if (pattern.isBogus()) {
469 pattern = UnicodeString("{0} ({1})", -1, US_INV);
470 }
2ca993e8 471 format.applyPatternMinMaxArguments(pattern, 2, 2, status);
57a6839d
A
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 }
51004dcb
A
483
484 UnicodeString ktPattern;
485 langData.get("localeDisplayPattern", "keyTypePattern", ktPattern);
486 if (ktPattern.isBogus()) {
487 ktPattern = UnicodeString("{0}={1}", -1, US_INV);
488 }
2ca993e8 489 keyTypeFormat.applyPatternMinMaxArguments(ktPattern, 2, 2, status);
51004dcb
A
490
491 uprv_memset(fCapitalization, 0, sizeof(fCapitalization));
492#if !UCONFIG_NO_BREAK_ITERATION
57a6839d
A
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) {
f3c0d7a5
A
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;
57a6839d 506 }
f3c0d7a5 507 needBrkIter = sink.hasCapitalizationUsage;
57a6839d
A
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;
51004dcb 516 }
51004dcb
A
517 }
518#endif
729e4ab9
A
519}
520
521LocaleDisplayNamesImpl::~LocaleDisplayNamesImpl() {
f3c0d7a5 522#if !UCONFIG_NO_BREAK_ITERATION
57a6839d 523 delete capitalizationBrkIter;
f3c0d7a5
A
524#endif
525}
729e4ab9
A
526
527const Locale&
528LocaleDisplayNamesImpl::getLocale() const {
51004dcb 529 return locale;
729e4ab9
A
530}
531
532UDialectHandling
533LocaleDisplayNamesImpl::getDialectHandling() const {
51004dcb
A
534 return dialectHandling;
535}
536
537UDisplayContext
538LocaleDisplayNamesImpl::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;
b331163b 544 case UDISPCTX_TYPE_DISPLAY_LENGTH:
57a6839d 545 return nameLength;
b331163b
A
546 case UADISPCTX_TYPE_LENGTH: // Apple-specific
547 return (nameLength == UDISPCTX_LENGTH_SHORT)? UADISPCTX_LENGTH_SHORT: UADISPCTX_LENGTH_STANDARD;
51004dcb
A
548 default:
549 break;
550 }
551 return (UDisplayContext)0;
552}
553
554UnicodeString&
555LocaleDisplayNamesImpl::adjustForUsageAndContext(CapContextUsage usage,
556 UnicodeString& result) const {
557#if !UCONFIG_NO_BREAK_ITERATION
558 // check to see whether we need to titlecase result
57a6839d
A
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
b331163b 562 Mutex lock(&capitalizationBrkIterLock);
57a6839d 563 result.toTitle(capitalizationBrkIter, locale, U_TITLECASE_NO_LOWERCASE | U_TITLECASE_NO_BREAK_ADJUSTMENT);
51004dcb
A
564 }
565#endif
566 return result;
729e4ab9
A
567}
568
569UnicodeString&
570LocaleDisplayNamesImpl::localeDisplayName(const Locale& locale,
571 UnicodeString& result) const {
2ca993e8
A
572 if (locale.isBogus()) {
573 result.setToBogus();
574 return result;
575 }
729e4ab9
A
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;
729e4ab9
A
626 UErrorCode status = U_ZERO_ERROR;
627
628 if (hasScript) {
2ca993e8 629 resultRemainder.append(scriptDisplayName(script, temp, TRUE));
729e4ab9
A
630 }
631 if (hasCountry) {
b331163b 632 appendWithSep(resultRemainder, regionShortDisplayName(country, temp)); // Apple modification
729e4ab9
A
633 }
634 if (hasVariant) {
2ca993e8 635 appendWithSep(resultRemainder, variantDisplayName(variant, temp, TRUE));
729e4ab9 636 }
57a6839d
A
637 resultRemainder.findAndReplace(formatOpenParen, formatReplaceOpenParen);
638 resultRemainder.findAndReplace(formatCloseParen, formatReplaceCloseParen);
729e4ab9 639
2ca993e8
A
640 LocalPointer<StringEnumeration> e(locale.createKeywords(status));
641 if (e.isValid() && U_SUCCESS(status)) {
729e4ab9
A
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 locale.getKeywordValue(key, value, ULOC_KEYWORD_AND_VALUES_CAPACITY, status);
2ca993e8
A
647 if (U_FAILURE(status)) {
648 return result;
649 }
650 keyDisplayName(key, temp, TRUE);
57a6839d
A
651 temp.findAndReplace(formatOpenParen, formatReplaceOpenParen);
652 temp.findAndReplace(formatCloseParen, formatReplaceCloseParen);
2ca993e8 653 keyValueDisplayName(key, value, temp2, TRUE);
57a6839d
A
654 temp2.findAndReplace(formatOpenParen, formatReplaceOpenParen);
655 temp2.findAndReplace(formatCloseParen, formatReplaceCloseParen);
4388f060
A
656 if (temp2 != UnicodeString(value, -1, US_INV)) {
657 appendWithSep(resultRemainder, temp2);
658 } else if (temp != UnicodeString(key, -1, US_INV)) {
659 UnicodeString temp3;
2ca993e8 660 keyTypeFormat.format(temp, temp2, temp3, status);
4388f060
A
661 appendWithSep(resultRemainder, temp3);
662 } else {
663 appendWithSep(resultRemainder, temp)
664 .append((UChar)0x3d /* = */)
665 .append(temp2);
666 }
729e4ab9 667 }
729e4ab9
A
668 }
669
670 if (!resultRemainder.isEmpty()) {
2ca993e8 671 format.format(resultName, resultRemainder, result.remove(), status);
51004dcb 672 return adjustForUsageAndContext(kCapContextUsageLanguage, result);
729e4ab9
A
673 }
674
51004dcb
A
675 result = resultName;
676 return adjustForUsageAndContext(kCapContextUsageLanguage, result);
729e4ab9
A
677}
678
679UnicodeString&
680LocaleDisplayNamesImpl::appendWithSep(UnicodeString& buffer, const UnicodeString& src) const {
57a6839d
A
681 if (buffer.isEmpty()) {
682 buffer.setTo(src);
683 } else {
2ca993e8 684 const UnicodeString *values[2] = { &buffer, &src };
57a6839d 685 UErrorCode status = U_ZERO_ERROR;
2ca993e8 686 separatorFormat.formatAndReplace(values, 2, buffer, NULL, 0, status);
51004dcb 687 }
51004dcb 688 return buffer;
729e4ab9
A
689}
690
691UnicodeString&
692LocaleDisplayNamesImpl::localeDisplayName(const char* localeId,
693 UnicodeString& result) const {
51004dcb 694 return localeDisplayName(Locale(localeId), result);
729e4ab9
A
695}
696
51004dcb 697// private
729e4ab9
A
698UnicodeString&
699LocaleDisplayNamesImpl::localeIdName(const char* localeId,
700 UnicodeString& result) const {
b331163b
A
701 if (nameLength == UDISPCTX_LENGTH_SHORT) {
702 langData.getNoFallback("Languages%short", localeId, result);
57a6839d
A
703 if (!result.isBogus()) {
704 return result;
705 }
706 }
51004dcb 707 return langData.getNoFallback("Languages", localeId, result);
729e4ab9
A
708}
709
710UnicodeString&
711LocaleDisplayNamesImpl::languageDisplayName(const char* lang,
712 UnicodeString& result) const {
51004dcb
A
713 if (uprv_strcmp("root", lang) == 0 || uprv_strchr(lang, '_') != NULL) {
714 return result = UnicodeString(lang, -1, US_INV);
715 }
b331163b 716 if (nameLength == UDISPCTX_LENGTH_SHORT) {
2ca993e8 717 langData.getNoFallback("Languages%short", lang, result);
57a6839d
A
718 if (!result.isBogus()) {
719 return adjustForUsageAndContext(kCapContextUsageLanguage, result);
720 }
721 }
51004dcb
A
722 langData.get("Languages", lang, result);
723 return adjustForUsageAndContext(kCapContextUsageLanguage, result);
729e4ab9
A
724}
725
726UnicodeString&
727LocaleDisplayNamesImpl::scriptDisplayName(const char* script,
2ca993e8
A
728 UnicodeString& result,
729 UBool skipAdjust) const {
730 if (!skipAdjust) { // => prefer standalone
731 langData.getNoFallback("Scripts%stand-alone", script, result);
b331163b
A
732 if (!result.isBogus()) {
733 return adjustForUsageAndContext(kCapContextUsageScript, result);
734 }
735 }
2ca993e8
A
736 if (nameLength == UDISPCTX_LENGTH_SHORT) {
737 langData.getNoFallback("Scripts%short", script, result);
738 if (!result.isBogus()) {
739 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageScript, result);
740 }
741 }
51004dcb 742 langData.get("Scripts", script, result);
2ca993e8
A
743 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageScript, result);
744}
745
746UnicodeString&
747LocaleDisplayNamesImpl::scriptDisplayName(const char* script,
748 UnicodeString& result) const {
749 return scriptDisplayName(script, result, FALSE);
729e4ab9
A
750}
751
752UnicodeString&
753LocaleDisplayNamesImpl::scriptDisplayName(UScriptCode scriptCode,
754 UnicodeString& result) const {
2ca993e8 755 return scriptDisplayName(uscript_getName(scriptCode), result, FALSE);
729e4ab9
A
756}
757
758UnicodeString&
759LocaleDisplayNamesImpl::regionDisplayName(const char* region,
2ca993e8
A
760 UnicodeString& result,
761 UBool skipAdjust) const {
b331163b 762 if (nameLength == UDISPCTX_LENGTH_SHORT) {
2ca993e8 763 regionData.getNoFallback("Countries%short", region, result);
57a6839d 764 if (!result.isBogus()) {
2ca993e8 765 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageTerritory, result);
57a6839d
A
766 }
767 }
768 regionData.get("Countries", region, result);
2ca993e8 769 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageTerritory, result);
57a6839d
A
770}
771
2ca993e8
A
772// private Apple, only for building localeDisplayName
773// (use short region, don't adjust for context)
57a6839d
A
774UnicodeString&
775LocaleDisplayNamesImpl::regionShortDisplayName(const char* region,
776 UnicodeString& result) const {
777 if (uprv_strcmp(region, "PS") != 0) {
b331163b 778 regionData.getNoFallback("Countries%short", region, result);
57a6839d 779 if (!result.isBogus()) {
2ca993e8 780 return result;
57a6839d
A
781 }
782 }
51004dcb 783 regionData.get("Countries", region, result);
2ca993e8 784 return result;
729e4ab9
A
785}
786
2ca993e8
A
787UnicodeString&
788LocaleDisplayNamesImpl::regionDisplayName(const char* region,
789 UnicodeString& result) const {
790 return regionDisplayName(region, result, FALSE);
791}
792
793
729e4ab9
A
794UnicodeString&
795LocaleDisplayNamesImpl::variantDisplayName(const char* variant,
2ca993e8
A
796 UnicodeString& result,
797 UBool skipAdjust) const {
b331163b 798 // don't have a resource for short variant names
51004dcb 799 langData.get("Variants", variant, result);
2ca993e8
A
800 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageVariant, result);
801}
802
803UnicodeString&
804LocaleDisplayNamesImpl::variantDisplayName(const char* variant,
805 UnicodeString& result) const {
806 return variantDisplayName(variant, result, FALSE);
729e4ab9
A
807}
808
809UnicodeString&
810LocaleDisplayNamesImpl::keyDisplayName(const char* key,
2ca993e8
A
811 UnicodeString& result,
812 UBool skipAdjust) const {
b331163b 813 // don't have a resource for short key names
51004dcb 814 langData.get("Keys", key, result);
2ca993e8
A
815 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKey, result);
816}
817
818UnicodeString&
819LocaleDisplayNamesImpl::keyDisplayName(const char* key,
820 UnicodeString& result) const {
821 return keyDisplayName(key, result, FALSE);
729e4ab9
A
822}
823
824UnicodeString&
825LocaleDisplayNamesImpl::keyValueDisplayName(const char* key,
826 const char* value,
2ca993e8
A
827 UnicodeString& result,
828 UBool skipAdjust) const {
b331163b
A
829 if (uprv_strcmp(key, "currency") == 0) {
830 // ICU4C does not have ICU4J CurrencyDisplayInfo equivalent for now.
831 UErrorCode sts = U_ZERO_ERROR;
832 UnicodeString ustrValue(value, -1, US_INV);
833 int32_t len;
834 UBool isChoice = FALSE;
835 const UChar *currencyName = ucurr_getName(ustrValue.getTerminatedBuffer(),
836 locale.getBaseName(), UCURR_LONG_NAME, &isChoice, &len, &sts);
837 if (U_FAILURE(sts)) {
838 // Return the value as is on failure
839 result = ustrValue;
840 return result;
841 }
842 result.setTo(currencyName, len);
2ca993e8 843 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result);
b331163b
A
844 }
845
846 if (nameLength == UDISPCTX_LENGTH_SHORT) {
847 langData.get("Types%short", key, value, result);
848 if (!result.isBogus()) {
2ca993e8 849 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result);
b331163b
A
850 }
851 }
51004dcb 852 langData.get("Types", key, value, result);
2ca993e8
A
853 return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result);
854}
855
856UnicodeString&
857LocaleDisplayNamesImpl::keyValueDisplayName(const char* key,
858 const char* value,
859 UnicodeString& result) const {
860 return keyValueDisplayName(key, value, result, FALSE);
729e4ab9
A
861}
862
863////////////////////////////////////////////////////////////////////////////////////////////////////
864
865LocaleDisplayNames*
866LocaleDisplayNames::createInstance(const Locale& locale,
867 UDialectHandling dialectHandling) {
51004dcb
A
868 return new LocaleDisplayNamesImpl(locale, dialectHandling);
869}
870
871LocaleDisplayNames*
872LocaleDisplayNames::createInstance(const Locale& locale,
873 UDisplayContext *contexts, int32_t length) {
874 if (contexts == NULL) {
875 length = 0;
876 }
877 return new LocaleDisplayNamesImpl(locale, contexts, length);
729e4ab9
A
878}
879
880U_NAMESPACE_END
881
882////////////////////////////////////////////////////////////////////////////////////////////////////
883
884U_NAMESPACE_USE
885
51004dcb 886U_CAPI ULocaleDisplayNames * U_EXPORT2
729e4ab9
A
887uldn_open(const char * locale,
888 UDialectHandling dialectHandling,
889 UErrorCode *pErrorCode) {
890 if (U_FAILURE(*pErrorCode)) {
891 return 0;
892 }
893 if (locale == NULL) {
894 locale = uloc_getDefault();
895 }
896 return (ULocaleDisplayNames *)LocaleDisplayNames::createInstance(Locale(locale), dialectHandling);
897}
898
51004dcb
A
899U_CAPI ULocaleDisplayNames * U_EXPORT2
900uldn_openForContext(const char * locale,
901 UDisplayContext *contexts, int32_t length,
902 UErrorCode *pErrorCode) {
903 if (U_FAILURE(*pErrorCode)) {
904 return 0;
905 }
906 if (locale == NULL) {
907 locale = uloc_getDefault();
908 }
909 return (ULocaleDisplayNames *)LocaleDisplayNames::createInstance(Locale(locale), contexts, length);
910}
911
912
913U_CAPI void U_EXPORT2
729e4ab9
A
914uldn_close(ULocaleDisplayNames *ldn) {
915 delete (LocaleDisplayNames *)ldn;
916}
917
51004dcb 918U_CAPI const char * U_EXPORT2
729e4ab9
A
919uldn_getLocale(const ULocaleDisplayNames *ldn) {
920 if (ldn) {
921 return ((const LocaleDisplayNames *)ldn)->getLocale().getName();
922 }
923 return NULL;
924}
925
51004dcb 926U_CAPI UDialectHandling U_EXPORT2
729e4ab9
A
927uldn_getDialectHandling(const ULocaleDisplayNames *ldn) {
928 if (ldn) {
929 return ((const LocaleDisplayNames *)ldn)->getDialectHandling();
930 }
931 return ULDN_STANDARD_NAMES;
932}
933
51004dcb
A
934U_CAPI UDisplayContext U_EXPORT2
935uldn_getContext(const ULocaleDisplayNames *ldn,
936 UDisplayContextType type,
937 UErrorCode *pErrorCode) {
938 if (U_FAILURE(*pErrorCode)) {
939 return (UDisplayContext)0;
940 }
941 return ((const LocaleDisplayNames *)ldn)->getContext(type);
942}
943
944U_CAPI int32_t U_EXPORT2
729e4ab9
A
945uldn_localeDisplayName(const ULocaleDisplayNames *ldn,
946 const char *locale,
947 UChar *result,
948 int32_t maxResultSize,
949 UErrorCode *pErrorCode) {
950 if (U_FAILURE(*pErrorCode)) {
951 return 0;
952 }
953 if (ldn == NULL || locale == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
954 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
955 return 0;
956 }
957 UnicodeString temp(result, 0, maxResultSize);
958 ((const LocaleDisplayNames *)ldn)->localeDisplayName(locale, temp);
2ca993e8
A
959 if (temp.isBogus()) {
960 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
961 return 0;
962 }
729e4ab9
A
963 return temp.extract(result, maxResultSize, *pErrorCode);
964}
965
51004dcb 966U_CAPI int32_t U_EXPORT2
729e4ab9
A
967uldn_languageDisplayName(const ULocaleDisplayNames *ldn,
968 const char *lang,
969 UChar *result,
970 int32_t maxResultSize,
971 UErrorCode *pErrorCode) {
972 if (U_FAILURE(*pErrorCode)) {
973 return 0;
974 }
975 if (ldn == NULL || lang == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
976 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
977 return 0;
978 }
979 UnicodeString temp(result, 0, maxResultSize);
980 ((const LocaleDisplayNames *)ldn)->languageDisplayName(lang, temp);
981 return temp.extract(result, maxResultSize, *pErrorCode);
982}
983
51004dcb 984U_CAPI int32_t U_EXPORT2
729e4ab9
A
985uldn_scriptDisplayName(const ULocaleDisplayNames *ldn,
986 const char *script,
987 UChar *result,
988 int32_t maxResultSize,
989 UErrorCode *pErrorCode) {
990 if (U_FAILURE(*pErrorCode)) {
991 return 0;
992 }
993 if (ldn == NULL || script == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
994 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
995 return 0;
996 }
997 UnicodeString temp(result, 0, maxResultSize);
998 ((const LocaleDisplayNames *)ldn)->scriptDisplayName(script, temp);
999 return temp.extract(result, maxResultSize, *pErrorCode);
1000}
1001
51004dcb 1002U_CAPI int32_t U_EXPORT2
729e4ab9
A
1003uldn_scriptCodeDisplayName(const ULocaleDisplayNames *ldn,
1004 UScriptCode scriptCode,
1005 UChar *result,
1006 int32_t maxResultSize,
1007 UErrorCode *pErrorCode) {
1008 return uldn_scriptDisplayName(ldn, uscript_getName(scriptCode), result, maxResultSize, pErrorCode);
1009}
1010
51004dcb 1011U_CAPI int32_t U_EXPORT2
729e4ab9
A
1012uldn_regionDisplayName(const ULocaleDisplayNames *ldn,
1013 const char *region,
1014 UChar *result,
1015 int32_t maxResultSize,
1016 UErrorCode *pErrorCode) {
1017 if (U_FAILURE(*pErrorCode)) {
1018 return 0;
1019 }
1020 if (ldn == NULL || region == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1021 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1022 return 0;
1023 }
1024 UnicodeString temp(result, 0, maxResultSize);
1025 ((const LocaleDisplayNames *)ldn)->regionDisplayName(region, temp);
1026 return temp.extract(result, maxResultSize, *pErrorCode);
1027}
1028
51004dcb 1029U_CAPI int32_t U_EXPORT2
729e4ab9
A
1030uldn_variantDisplayName(const ULocaleDisplayNames *ldn,
1031 const char *variant,
1032 UChar *result,
1033 int32_t maxResultSize,
1034 UErrorCode *pErrorCode) {
1035 if (U_FAILURE(*pErrorCode)) {
1036 return 0;
1037 }
1038 if (ldn == NULL || variant == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1039 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1040 return 0;
1041 }
1042 UnicodeString temp(result, 0, maxResultSize);
1043 ((const LocaleDisplayNames *)ldn)->variantDisplayName(variant, temp);
1044 return temp.extract(result, maxResultSize, *pErrorCode);
1045}
1046
51004dcb 1047U_CAPI int32_t U_EXPORT2
729e4ab9
A
1048uldn_keyDisplayName(const ULocaleDisplayNames *ldn,
1049 const char *key,
1050 UChar *result,
1051 int32_t maxResultSize,
1052 UErrorCode *pErrorCode) {
1053 if (U_FAILURE(*pErrorCode)) {
1054 return 0;
1055 }
1056 if (ldn == NULL || key == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1057 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1058 return 0;
1059 }
1060 UnicodeString temp(result, 0, maxResultSize);
1061 ((const LocaleDisplayNames *)ldn)->keyDisplayName(key, temp);
1062 return temp.extract(result, maxResultSize, *pErrorCode);
1063}
1064
51004dcb 1065U_CAPI int32_t U_EXPORT2
729e4ab9
A
1066uldn_keyValueDisplayName(const ULocaleDisplayNames *ldn,
1067 const char *key,
1068 const char *value,
1069 UChar *result,
1070 int32_t maxResultSize,
1071 UErrorCode *pErrorCode) {
1072 if (U_FAILURE(*pErrorCode)) {
1073 return 0;
1074 }
1075 if (ldn == NULL || key == NULL || value == NULL || (result == NULL && maxResultSize > 0)
1076 || maxResultSize < 0) {
1077 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1078 return 0;
1079 }
1080 UnicodeString temp(result, 0, maxResultSize);
1081 ((const LocaleDisplayNames *)ldn)->keyValueDisplayName(key, value, temp);
1082 return temp.extract(result, maxResultSize, *pErrorCode);
1083}
1084
1085#endif