2 *******************************************************************************
3 * Copyright (C) 2010-2012, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_FORMATTING
12 #include "unicode/locdspnm.h"
13 #include "unicode/msgfmt.h"
14 #include "unicode/ures.h"
15 #include "unicode/brkiter.h"
26 * Concatenate a number of null-terminated strings to buffer, leaving a
27 * null-terminated string. The last argument should be the null pointer.
28 * Return the length of the string in the buffer, not counting the trailing
29 * null. Return -1 if there is an error (buffer is null, or buflen < 1).
31 static int32_t ncat(char *buffer
, uint32_t buflen
, ...) {
35 const char* e
= buffer
+ buflen
- 1;
37 if (buffer
== NULL
|| buflen
< 1) {
41 va_start(args
, buflen
);
42 while ((str
= va_arg(args
, char *))) {
44 while (p
!= e
&& (c
= *str
++)) {
56 ////////////////////////////////////////////////////////////////////////////////////////////////////
58 // Access resource data for locale components.
59 // Wrap code in uloc.c for now.
65 ICUDataTable(const char* path
, const Locale
& locale
);
68 const Locale
& getLocale();
70 UnicodeString
& get(const char* tableKey
, const char* itemKey
,
71 UnicodeString
& result
) const;
72 UnicodeString
& get(const char* tableKey
, const char* subTableKey
, const char* itemKey
,
73 UnicodeString
& result
) const;
75 UnicodeString
& getNoFallback(const char* tableKey
, const char* itemKey
,
76 UnicodeString
&result
) const;
77 UnicodeString
& getNoFallback(const char* tableKey
, const char* subTableKey
, const char* itemKey
,
78 UnicodeString
&result
) const;
81 inline UnicodeString
&
82 ICUDataTable::get(const char* tableKey
, const char* itemKey
, UnicodeString
& result
) const {
83 return get(tableKey
, NULL
, itemKey
, result
);
86 inline UnicodeString
&
87 ICUDataTable::getNoFallback(const char* tableKey
, const char* itemKey
, UnicodeString
& result
) const {
88 return getNoFallback(tableKey
, NULL
, itemKey
, result
);
91 ICUDataTable::ICUDataTable(const char* path
, const Locale
& locale
)
92 : path(NULL
), locale(Locale::getRoot())
95 int32_t len
= uprv_strlen(path
);
96 this->path
= (const char*) uprv_malloc(len
+ 1);
98 uprv_strcpy((char *)this->path
, path
);
99 this->locale
= locale
;
104 ICUDataTable::~ICUDataTable() {
106 uprv_free((void*) path
);
112 ICUDataTable::getLocale() {
117 ICUDataTable::get(const char* tableKey
, const char* subTableKey
, const char* itemKey
,
118 UnicodeString
&result
) const {
119 UErrorCode status
= U_ZERO_ERROR
;
122 const UChar
*s
= uloc_getTableStringWithFallback(path
, locale
.getName(),
123 tableKey
, subTableKey
, itemKey
,
125 if (U_SUCCESS(status
) && len
> 0) {
126 return result
.setTo(s
, len
);
128 return result
.setTo(UnicodeString(itemKey
, -1, US_INV
));
132 ICUDataTable::getNoFallback(const char* tableKey
, const char* subTableKey
, const char* itemKey
,
133 UnicodeString
& result
) const {
134 UErrorCode status
= U_ZERO_ERROR
;
137 const UChar
*s
= uloc_getTableStringWithFallback(path
, locale
.getName(),
138 tableKey
, subTableKey
, itemKey
,
140 if (U_SUCCESS(status
)) {
141 return result
.setTo(s
, len
);
148 ////////////////////////////////////////////////////////////////////////////////////////////////////
150 LocaleDisplayNames::~LocaleDisplayNames() {}
152 ////////////////////////////////////////////////////////////////////////////////////////////////////
154 #if 0 // currently unused
156 class DefaultLocaleDisplayNames
: public LocaleDisplayNames
{
157 UDialectHandling dialectHandling
;
161 DefaultLocaleDisplayNames(UDialectHandling dialectHandling
);
163 virtual ~DefaultLocaleDisplayNames();
165 virtual const Locale
& getLocale() const;
166 virtual UDialectHandling
getDialectHandling() const;
168 virtual UnicodeString
& localeDisplayName(const Locale
& locale
,
169 UnicodeString
& result
) const;
170 virtual UnicodeString
& localeDisplayName(const char* localeId
,
171 UnicodeString
& result
) const;
172 virtual UnicodeString
& languageDisplayName(const char* lang
,
173 UnicodeString
& result
) const;
174 virtual UnicodeString
& scriptDisplayName(const char* script
,
175 UnicodeString
& result
) const;
176 virtual UnicodeString
& scriptDisplayName(UScriptCode scriptCode
,
177 UnicodeString
& result
) const;
178 virtual UnicodeString
& regionDisplayName(const char* region
,
179 UnicodeString
& result
) const;
180 virtual UnicodeString
& variantDisplayName(const char* variant
,
181 UnicodeString
& result
) const;
182 virtual UnicodeString
& keyDisplayName(const char* key
,
183 UnicodeString
& result
) const;
184 virtual UnicodeString
& keyValueDisplayName(const char* key
,
186 UnicodeString
& result
) const;
189 DefaultLocaleDisplayNames::DefaultLocaleDisplayNames(UDialectHandling dialectHandling
)
190 : dialectHandling(dialectHandling
) {
193 DefaultLocaleDisplayNames::~DefaultLocaleDisplayNames() {
197 DefaultLocaleDisplayNames::getLocale() const {
198 return Locale::getRoot();
202 DefaultLocaleDisplayNames::getDialectHandling() const {
203 return dialectHandling
;
207 DefaultLocaleDisplayNames::localeDisplayName(const Locale
& locale
,
208 UnicodeString
& result
) const {
209 return result
= UnicodeString(locale
.getName(), -1, US_INV
);
213 DefaultLocaleDisplayNames::localeDisplayName(const char* localeId
,
214 UnicodeString
& result
) const {
215 return result
= UnicodeString(localeId
, -1, US_INV
);
219 DefaultLocaleDisplayNames::languageDisplayName(const char* lang
,
220 UnicodeString
& result
) const {
221 return result
= UnicodeString(lang
, -1, US_INV
);
225 DefaultLocaleDisplayNames::scriptDisplayName(const char* script
,
226 UnicodeString
& result
) const {
227 return result
= UnicodeString(script
, -1, US_INV
);
231 DefaultLocaleDisplayNames::scriptDisplayName(UScriptCode scriptCode
,
232 UnicodeString
& result
) const {
233 const char* name
= uscript_getName(scriptCode
);
235 return result
= UnicodeString(name
, -1, US_INV
);
237 return result
.remove();
241 DefaultLocaleDisplayNames::regionDisplayName(const char* region
,
242 UnicodeString
& result
) const {
243 return result
= UnicodeString(region
, -1, US_INV
);
247 DefaultLocaleDisplayNames::variantDisplayName(const char* variant
,
248 UnicodeString
& result
) const {
249 return result
= UnicodeString(variant
, -1, US_INV
);
253 DefaultLocaleDisplayNames::keyDisplayName(const char* key
,
254 UnicodeString
& result
) const {
255 return result
= UnicodeString(key
, -1, US_INV
);
259 DefaultLocaleDisplayNames::keyValueDisplayName(const char* /* key */,
261 UnicodeString
& result
) const {
262 return result
= UnicodeString(value
, -1, US_INV
);
265 #endif // currently unused class DefaultLocaleDisplayNames
267 ////////////////////////////////////////////////////////////////////////////////////////////////////
269 class LocaleDisplayNamesImpl
: public LocaleDisplayNames
{
271 UDialectHandling dialectHandling
;
272 ICUDataTable langData
;
273 ICUDataTable regionData
;
275 MessageFormat
*format
;
276 MessageFormat
*keyTypeFormat
;
277 UDisplayContext capitalizationContext
;
279 // Constants for capitalization context usage types.
280 enum CapContextUsage
{
281 kCapContextUsageLanguage
,
282 kCapContextUsageScript
,
283 kCapContextUsageTerritory
,
284 kCapContextUsageVariant
,
286 kCapContextUsageType
,
287 kCapContextUsageCount
289 // Capitalization transforms. For each usage type, the first array element indicates
290 // whether to titlecase for uiListOrMenu context, the second indicates whether to
291 // titlecase for stand-alone context.
292 UBool fCapitalization
[kCapContextUsageCount
][2];
296 LocaleDisplayNamesImpl(const Locale
& locale
, UDialectHandling dialectHandling
);
297 LocaleDisplayNamesImpl(const Locale
& locale
, UDisplayContext
*contexts
, int32_t length
);
298 virtual ~LocaleDisplayNamesImpl();
300 virtual const Locale
& getLocale() const;
301 virtual UDialectHandling
getDialectHandling() const;
302 virtual UDisplayContext
getContext(UDisplayContextType type
) const;
304 virtual UnicodeString
& localeDisplayName(const Locale
& locale
,
305 UnicodeString
& result
) const;
306 virtual UnicodeString
& localeDisplayName(const char* localeId
,
307 UnicodeString
& result
) const;
308 virtual UnicodeString
& languageDisplayName(const char* lang
,
309 UnicodeString
& result
) const;
310 virtual UnicodeString
& scriptDisplayName(const char* script
,
311 UnicodeString
& result
) const;
312 virtual UnicodeString
& scriptDisplayName(UScriptCode scriptCode
,
313 UnicodeString
& result
) const;
314 virtual UnicodeString
& regionDisplayName(const char* region
,
315 UnicodeString
& result
) const;
316 virtual UnicodeString
& variantDisplayName(const char* variant
,
317 UnicodeString
& result
) const;
318 virtual UnicodeString
& keyDisplayName(const char* key
,
319 UnicodeString
& result
) const;
320 virtual UnicodeString
& keyValueDisplayName(const char* key
,
322 UnicodeString
& result
) const;
324 UnicodeString
& localeIdName(const char* localeId
,
325 UnicodeString
& result
) const;
326 UnicodeString
& appendWithSep(UnicodeString
& buffer
, const UnicodeString
& src
) const;
327 UnicodeString
& adjustForUsageAndContext(CapContextUsage usage
, UnicodeString
& result
) const;
328 void initialize(void);
331 LocaleDisplayNamesImpl::LocaleDisplayNamesImpl(const Locale
& locale
,
332 UDialectHandling dialectHandling
)
333 : dialectHandling(dialectHandling
)
334 , langData(U_ICUDATA_LANG
, locale
)
335 , regionData(U_ICUDATA_REGION
, locale
)
337 , keyTypeFormat(NULL
)
338 , capitalizationContext(UDISPCTX_CAPITALIZATION_NONE
)
343 LocaleDisplayNamesImpl::LocaleDisplayNamesImpl(const Locale
& locale
,
344 UDisplayContext
*contexts
, int32_t length
)
345 : dialectHandling(ULDN_STANDARD_NAMES
)
346 , langData(U_ICUDATA_LANG
, locale
)
347 , regionData(U_ICUDATA_REGION
, locale
)
349 , keyTypeFormat(NULL
)
350 , capitalizationContext(UDISPCTX_CAPITALIZATION_NONE
)
352 while (length
-- > 0) {
353 UDisplayContext value
= *contexts
++;
354 UDisplayContextType selector
= (UDisplayContextType
)((uint32_t)value
>> 8);
356 case UDISPCTX_TYPE_DIALECT_HANDLING
:
357 dialectHandling
= (UDialectHandling
)value
;
359 case UDISPCTX_TYPE_CAPITALIZATION
:
360 capitalizationContext
= value
;
370 LocaleDisplayNamesImpl::initialize(void) {
371 LocaleDisplayNamesImpl
*nonConstThis
= (LocaleDisplayNamesImpl
*)this;
372 nonConstThis
->locale
= langData
.getLocale() == Locale::getRoot()
373 ? regionData
.getLocale()
374 : langData
.getLocale();
376 langData
.getNoFallback("localeDisplayPattern", "separator", sep
);
378 sep
= UnicodeString(", ", -1, US_INV
);
381 UnicodeString pattern
;
382 langData
.getNoFallback("localeDisplayPattern", "pattern", pattern
);
383 if (pattern
.isBogus()) {
384 pattern
= UnicodeString("{0} ({1})", -1, US_INV
);
386 UErrorCode status
= U_ZERO_ERROR
;
387 format
= new MessageFormat(pattern
, status
);
389 UnicodeString ktPattern
;
390 langData
.get("localeDisplayPattern", "keyTypePattern", ktPattern
);
391 if (ktPattern
.isBogus()) {
392 ktPattern
= UnicodeString("{0}={1}", -1, US_INV
);
394 keyTypeFormat
= new MessageFormat(ktPattern
, status
);
396 uprv_memset(fCapitalization
, 0, sizeof(fCapitalization
));
397 #if !UCONFIG_NO_BREAK_ITERATION
398 // The following is basically copied from DateFormatSymbols::initializeData
400 const char * usageName
;
401 LocaleDisplayNamesImpl::CapContextUsage usageEnum
;
402 } ContextUsageNameToEnum
;
403 const ContextUsageNameToEnum contextUsageTypeMap
[] = {
404 // Entries must be sorted by usageTypeName; entry with NULL name terminates list.
405 { "key", kCapContextUsageKey
},
406 { "languages", kCapContextUsageLanguage
},
407 { "script", kCapContextUsageScript
},
408 { "territory", kCapContextUsageTerritory
},
409 { "type", kCapContextUsageType
},
410 { "variant", kCapContextUsageVariant
},
411 { NULL
, (CapContextUsage
)0 },
414 UResourceBundle
*localeBundle
= ures_open(NULL
, locale
.getName(), &status
);
415 if (U_SUCCESS(status
)) {
416 UResourceBundle
*contextTransforms
= ures_getByKeyWithFallback(localeBundle
, "contextTransforms", NULL
, &status
);
417 if (U_SUCCESS(status
)) {
418 UResourceBundle
*contextTransformUsage
;
419 while ( (contextTransformUsage
= ures_getNextResource(contextTransforms
, NULL
, &status
)) != NULL
) {
420 const int32_t * intVector
= ures_getIntVector(contextTransformUsage
, &len
, &status
);
421 if (U_SUCCESS(status
) && intVector
!= NULL
&& len
>= 2) {
422 const char* usageKey
= ures_getKey(contextTransformUsage
);
423 if (usageKey
!= NULL
) {
424 const ContextUsageNameToEnum
* typeMapPtr
= contextUsageTypeMap
;
425 int32_t compResult
= 0;
426 // linear search; list is short and we cannot be sure that bsearch is available
427 while ( typeMapPtr
->usageName
!= NULL
&& (compResult
= uprv_strcmp(usageKey
, typeMapPtr
->usageName
)) > 0 ) {
430 if (typeMapPtr
->usageName
!= NULL
&& compResult
== 0) {
431 fCapitalization
[typeMapPtr
->usageEnum
][0] = intVector
[0];
432 fCapitalization
[typeMapPtr
->usageEnum
][1] = intVector
[1];
436 status
= U_ZERO_ERROR
;
437 ures_close(contextTransformUsage
);
439 ures_close(contextTransforms
);
441 ures_close(localeBundle
);
446 LocaleDisplayNamesImpl::~LocaleDisplayNamesImpl() {
448 delete keyTypeFormat
;
452 LocaleDisplayNamesImpl::getLocale() const {
457 LocaleDisplayNamesImpl::getDialectHandling() const {
458 return dialectHandling
;
462 LocaleDisplayNamesImpl::getContext(UDisplayContextType type
) const {
464 case UDISPCTX_TYPE_DIALECT_HANDLING
:
465 return (UDisplayContext
)dialectHandling
;
466 case UDISPCTX_TYPE_CAPITALIZATION
:
467 return capitalizationContext
;
471 return (UDisplayContext
)0;
475 LocaleDisplayNamesImpl::adjustForUsageAndContext(CapContextUsage usage
,
476 UnicodeString
& result
) const {
477 #if !UCONFIG_NO_BREAK_ITERATION
478 // check to see whether we need to titlecase result
479 UBool titlecase
= FALSE
;
480 switch (capitalizationContext
) {
481 case UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE
:
484 case UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU
:
485 titlecase
= fCapitalization
[usage
][0];
487 case UDISPCTX_CAPITALIZATION_FOR_STANDALONE
:
488 titlecase
= fCapitalization
[usage
][1];
491 // titlecase = FALSE;
495 // TODO: Fix this titlecase hack when we figure out something better to do.
496 // We don't want to titlecase the whole text, only something like the first word,
497 // of the first segment long enough to have a complete cluster, whichever is
498 // shorter. We could have keep a word break iterator around, but I am not sure
499 // that will do the ight thing for the purposes here. For now we assume that in
500 // languages for which titlecasing makes a difference, we can stop at non-letter
501 // characters in 0x0000-0x00FF and only titlecase up to the first occurrence of
502 // any of those, or to a small number of chars, whichever comes first.
503 int32_t stopPos
, stopPosLimit
= 8, len
= result
.length();
504 if ( stopPosLimit
> len
) {
507 for ( stopPos
= 0; stopPos
< stopPosLimit
; stopPos
++ ) {
508 UChar32 ch
= result
.char32At(stopPos
);
509 if ( (ch
< 0x41) || (ch
> 0x5A && ch
< 0x61) || (ch
> 0x7A && ch
< 0xC0) ) {
516 if ( stopPos
> 0 && stopPos
< len
) {
517 UnicodeString
firstWord(result
, 0, stopPos
);
518 firstWord
.toTitle(NULL
, locale
, U_TITLECASE_NO_LOWERCASE
| U_TITLECASE_NO_BREAK_ADJUSTMENT
);
519 result
.replaceBetween(0, stopPos
, firstWord
);
521 // no stopPos, titlecase the whole text
522 result
.toTitle(NULL
, locale
, U_TITLECASE_NO_LOWERCASE
| U_TITLECASE_NO_BREAK_ADJUSTMENT
);
530 LocaleDisplayNamesImpl::localeDisplayName(const Locale
& locale
,
531 UnicodeString
& result
) const {
532 UnicodeString resultName
;
534 const char* lang
= locale
.getLanguage();
535 if (uprv_strlen(lang
) == 0) {
538 const char* script
= locale
.getScript();
539 const char* country
= locale
.getCountry();
540 const char* variant
= locale
.getVariant();
542 UBool hasScript
= uprv_strlen(script
) > 0;
543 UBool hasCountry
= uprv_strlen(country
) > 0;
544 UBool hasVariant
= uprv_strlen(variant
) > 0;
546 if (dialectHandling
== ULDN_DIALECT_NAMES
) {
547 char buffer
[ULOC_FULLNAME_CAPACITY
];
548 do { // loop construct is so we can break early out of search
549 if (hasScript
&& hasCountry
) {
550 ncat(buffer
, ULOC_FULLNAME_CAPACITY
, lang
, "_", script
, "_", country
, (char *)0);
551 localeIdName(buffer
, resultName
);
552 if (!resultName
.isBogus()) {
559 ncat(buffer
, ULOC_FULLNAME_CAPACITY
, lang
, "_", script
, (char *)0);
560 localeIdName(buffer
, resultName
);
561 if (!resultName
.isBogus()) {
567 ncat(buffer
, ULOC_FULLNAME_CAPACITY
, lang
, "_", country
, (char*)0);
568 localeIdName(buffer
, resultName
);
569 if (!resultName
.isBogus()) {
576 if (resultName
.isBogus() || resultName
.isEmpty()) {
577 localeIdName(lang
, resultName
);
580 UnicodeString resultRemainder
;
582 StringEnumeration
*e
= NULL
;
583 UErrorCode status
= U_ZERO_ERROR
;
586 resultRemainder
.append(scriptDisplayName(script
, temp
));
589 appendWithSep(resultRemainder
, regionDisplayName(country
, temp
));
592 appendWithSep(resultRemainder
, variantDisplayName(variant
, temp
));
595 e
= locale
.createKeywords(status
);
596 if (e
&& U_SUCCESS(status
)) {
598 char value
[ULOC_KEYWORD_AND_VALUES_CAPACITY
]; // sigh, no ULOC_VALUE_CAPACITY
600 while ((key
= e
->next((int32_t *)0, status
)) != NULL
) {
601 locale
.getKeywordValue(key
, value
, ULOC_KEYWORD_AND_VALUES_CAPACITY
, status
);
602 keyDisplayName(key
, temp
);
603 keyValueDisplayName(key
, value
, temp2
);
604 if (temp2
!= UnicodeString(value
, -1, US_INV
)) {
605 appendWithSep(resultRemainder
, temp2
);
606 } else if (temp
!= UnicodeString(key
, -1, US_INV
)) {
608 Formattable data
[] = {
613 status
= U_ZERO_ERROR
;
614 keyTypeFormat
->format(data
, 2, temp3
, fpos
, status
);
615 appendWithSep(resultRemainder
, temp3
);
617 appendWithSep(resultRemainder
, temp
)
618 .append((UChar
)0x3d /* = */)
625 if (!resultRemainder
.isEmpty()) {
626 Formattable data
[] = {
631 status
= U_ZERO_ERROR
;
632 format
->format(data
, 2, result
, fpos
, status
);
633 return adjustForUsageAndContext(kCapContextUsageLanguage
, result
);
637 return adjustForUsageAndContext(kCapContextUsageLanguage
, result
);
641 LocaleDisplayNamesImpl::appendWithSep(UnicodeString
& buffer
, const UnicodeString
& src
) const {
642 if (!buffer
.isEmpty()) {
650 LocaleDisplayNamesImpl::localeDisplayName(const char* localeId
,
651 UnicodeString
& result
) const {
652 return localeDisplayName(Locale(localeId
), result
);
657 LocaleDisplayNamesImpl::localeIdName(const char* localeId
,
658 UnicodeString
& result
) const {
659 return langData
.getNoFallback("Languages", localeId
, result
);
663 LocaleDisplayNamesImpl::languageDisplayName(const char* lang
,
664 UnicodeString
& result
) const {
665 if (uprv_strcmp("root", lang
) == 0 || uprv_strchr(lang
, '_') != NULL
) {
666 return result
= UnicodeString(lang
, -1, US_INV
);
668 langData
.get("Languages", lang
, result
);
669 return adjustForUsageAndContext(kCapContextUsageLanguage
, result
);
673 LocaleDisplayNamesImpl::scriptDisplayName(const char* script
,
674 UnicodeString
& result
) const {
675 langData
.get("Scripts", script
, result
);
676 return adjustForUsageAndContext(kCapContextUsageScript
, result
);
680 LocaleDisplayNamesImpl::scriptDisplayName(UScriptCode scriptCode
,
681 UnicodeString
& result
) const {
682 const char* name
= uscript_getName(scriptCode
);
683 langData
.get("Scripts", name
, result
);
684 return adjustForUsageAndContext(kCapContextUsageScript
, result
);
688 LocaleDisplayNamesImpl::regionDisplayName(const char* region
,
689 UnicodeString
& result
) const {
690 regionData
.get("Countries", region
, result
);
691 return adjustForUsageAndContext(kCapContextUsageTerritory
, result
);
695 LocaleDisplayNamesImpl::variantDisplayName(const char* variant
,
696 UnicodeString
& result
) const {
697 langData
.get("Variants", variant
, result
);
698 return adjustForUsageAndContext(kCapContextUsageVariant
, result
);
702 LocaleDisplayNamesImpl::keyDisplayName(const char* key
,
703 UnicodeString
& result
) const {
704 langData
.get("Keys", key
, result
);
705 return adjustForUsageAndContext(kCapContextUsageKey
, result
);
709 LocaleDisplayNamesImpl::keyValueDisplayName(const char* key
,
711 UnicodeString
& result
) const {
712 langData
.get("Types", key
, value
, result
);
713 return adjustForUsageAndContext(kCapContextUsageType
, result
);
716 ////////////////////////////////////////////////////////////////////////////////////////////////////
719 LocaleDisplayNames::createInstance(const Locale
& locale
,
720 UDialectHandling dialectHandling
) {
721 return new LocaleDisplayNamesImpl(locale
, dialectHandling
);
725 LocaleDisplayNames::createInstance(const Locale
& locale
,
726 UDisplayContext
*contexts
, int32_t length
) {
727 if (contexts
== NULL
) {
730 return new LocaleDisplayNamesImpl(locale
, contexts
, length
);
735 ////////////////////////////////////////////////////////////////////////////////////////////////////
739 U_CAPI ULocaleDisplayNames
* U_EXPORT2
740 uldn_open(const char * locale
,
741 UDialectHandling dialectHandling
,
742 UErrorCode
*pErrorCode
) {
743 if (U_FAILURE(*pErrorCode
)) {
746 if (locale
== NULL
) {
747 locale
= uloc_getDefault();
749 return (ULocaleDisplayNames
*)LocaleDisplayNames::createInstance(Locale(locale
), dialectHandling
);
752 U_CAPI ULocaleDisplayNames
* U_EXPORT2
753 uldn_openForContext(const char * locale
,
754 UDisplayContext
*contexts
, int32_t length
,
755 UErrorCode
*pErrorCode
) {
756 if (U_FAILURE(*pErrorCode
)) {
759 if (locale
== NULL
) {
760 locale
= uloc_getDefault();
762 return (ULocaleDisplayNames
*)LocaleDisplayNames::createInstance(Locale(locale
), contexts
, length
);
766 U_CAPI
void U_EXPORT2
767 uldn_close(ULocaleDisplayNames
*ldn
) {
768 delete (LocaleDisplayNames
*)ldn
;
771 U_CAPI
const char * U_EXPORT2
772 uldn_getLocale(const ULocaleDisplayNames
*ldn
) {
774 return ((const LocaleDisplayNames
*)ldn
)->getLocale().getName();
779 U_CAPI UDialectHandling U_EXPORT2
780 uldn_getDialectHandling(const ULocaleDisplayNames
*ldn
) {
782 return ((const LocaleDisplayNames
*)ldn
)->getDialectHandling();
784 return ULDN_STANDARD_NAMES
;
787 U_CAPI UDisplayContext U_EXPORT2
788 uldn_getContext(const ULocaleDisplayNames
*ldn
,
789 UDisplayContextType type
,
790 UErrorCode
*pErrorCode
) {
791 if (U_FAILURE(*pErrorCode
)) {
792 return (UDisplayContext
)0;
794 return ((const LocaleDisplayNames
*)ldn
)->getContext(type
);
797 U_CAPI
int32_t U_EXPORT2
798 uldn_localeDisplayName(const ULocaleDisplayNames
*ldn
,
801 int32_t maxResultSize
,
802 UErrorCode
*pErrorCode
) {
803 if (U_FAILURE(*pErrorCode
)) {
806 if (ldn
== NULL
|| locale
== NULL
|| (result
== NULL
&& maxResultSize
> 0) || maxResultSize
< 0) {
807 *pErrorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
810 UnicodeString
temp(result
, 0, maxResultSize
);
811 ((const LocaleDisplayNames
*)ldn
)->localeDisplayName(locale
, temp
);
812 return temp
.extract(result
, maxResultSize
, *pErrorCode
);
815 U_CAPI
int32_t U_EXPORT2
816 uldn_languageDisplayName(const ULocaleDisplayNames
*ldn
,
819 int32_t maxResultSize
,
820 UErrorCode
*pErrorCode
) {
821 if (U_FAILURE(*pErrorCode
)) {
824 if (ldn
== NULL
|| lang
== NULL
|| (result
== NULL
&& maxResultSize
> 0) || maxResultSize
< 0) {
825 *pErrorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
828 UnicodeString
temp(result
, 0, maxResultSize
);
829 ((const LocaleDisplayNames
*)ldn
)->languageDisplayName(lang
, temp
);
830 return temp
.extract(result
, maxResultSize
, *pErrorCode
);
833 U_CAPI
int32_t U_EXPORT2
834 uldn_scriptDisplayName(const ULocaleDisplayNames
*ldn
,
837 int32_t maxResultSize
,
838 UErrorCode
*pErrorCode
) {
839 if (U_FAILURE(*pErrorCode
)) {
842 if (ldn
== NULL
|| script
== NULL
|| (result
== NULL
&& maxResultSize
> 0) || maxResultSize
< 0) {
843 *pErrorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
846 UnicodeString
temp(result
, 0, maxResultSize
);
847 ((const LocaleDisplayNames
*)ldn
)->scriptDisplayName(script
, temp
);
848 return temp
.extract(result
, maxResultSize
, *pErrorCode
);
851 U_CAPI
int32_t U_EXPORT2
852 uldn_scriptCodeDisplayName(const ULocaleDisplayNames
*ldn
,
853 UScriptCode scriptCode
,
855 int32_t maxResultSize
,
856 UErrorCode
*pErrorCode
) {
857 return uldn_scriptDisplayName(ldn
, uscript_getName(scriptCode
), result
, maxResultSize
, pErrorCode
);
860 U_CAPI
int32_t U_EXPORT2
861 uldn_regionDisplayName(const ULocaleDisplayNames
*ldn
,
864 int32_t maxResultSize
,
865 UErrorCode
*pErrorCode
) {
866 if (U_FAILURE(*pErrorCode
)) {
869 if (ldn
== NULL
|| region
== NULL
|| (result
== NULL
&& maxResultSize
> 0) || maxResultSize
< 0) {
870 *pErrorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
873 UnicodeString
temp(result
, 0, maxResultSize
);
874 ((const LocaleDisplayNames
*)ldn
)->regionDisplayName(region
, temp
);
875 return temp
.extract(result
, maxResultSize
, *pErrorCode
);
878 U_CAPI
int32_t U_EXPORT2
879 uldn_variantDisplayName(const ULocaleDisplayNames
*ldn
,
882 int32_t maxResultSize
,
883 UErrorCode
*pErrorCode
) {
884 if (U_FAILURE(*pErrorCode
)) {
887 if (ldn
== NULL
|| variant
== NULL
|| (result
== NULL
&& maxResultSize
> 0) || maxResultSize
< 0) {
888 *pErrorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
891 UnicodeString
temp(result
, 0, maxResultSize
);
892 ((const LocaleDisplayNames
*)ldn
)->variantDisplayName(variant
, temp
);
893 return temp
.extract(result
, maxResultSize
, *pErrorCode
);
896 U_CAPI
int32_t U_EXPORT2
897 uldn_keyDisplayName(const ULocaleDisplayNames
*ldn
,
900 int32_t maxResultSize
,
901 UErrorCode
*pErrorCode
) {
902 if (U_FAILURE(*pErrorCode
)) {
905 if (ldn
== NULL
|| key
== NULL
|| (result
== NULL
&& maxResultSize
> 0) || maxResultSize
< 0) {
906 *pErrorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
909 UnicodeString
temp(result
, 0, maxResultSize
);
910 ((const LocaleDisplayNames
*)ldn
)->keyDisplayName(key
, temp
);
911 return temp
.extract(result
, maxResultSize
, *pErrorCode
);
914 U_CAPI
int32_t U_EXPORT2
915 uldn_keyValueDisplayName(const ULocaleDisplayNames
*ldn
,
919 int32_t maxResultSize
,
920 UErrorCode
*pErrorCode
) {
921 if (U_FAILURE(*pErrorCode
)) {
924 if (ldn
== NULL
|| key
== NULL
|| value
== NULL
|| (result
== NULL
&& maxResultSize
> 0)
925 || maxResultSize
< 0) {
926 *pErrorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
929 UnicodeString
temp(result
, 0, maxResultSize
);
930 ((const LocaleDisplayNames
*)ldn
)->keyValueDisplayName(key
, value
, temp
);
931 return temp
.extract(result
, maxResultSize
, *pErrorCode
);