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  */
ncat(char * buffer,uint32_t buflen,...)37 static int32_t ncat(char *buffer, uint32_t buflen, ...) {
38   va_list args;
39   char *str;
40   char *p = buffer;
41   const char* e = buffer + buflen - 1;
42 
43   if (buffer == NULL || buflen < 1) {
44     return -1;
45   }
46 
47   va_start(args, buflen);
48   while ((str = va_arg(args, char *)) != 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 
60 U_NAMESPACE_BEGIN
61 
62 ////////////////////////////////////////////////////////////////////////////////////////////////////
63 
64 // Access resource data for locale components.
65 // Wrap code in uloc.c for now.
66 class ICUDataTable {
67     const char* path;
68     Locale locale;
69 
70 public:
71     ICUDataTable(const char* path, const Locale& locale);
72     ~ICUDataTable();
73 
74     const Locale& getLocale();
75 
76     UnicodeString& get(const char* tableKey, const char* itemKey,
77                         UnicodeString& result) const;
78     UnicodeString& get(const char* tableKey, const char* subTableKey, const char* itemKey,
79                         UnicodeString& result) const;
80 
81     UnicodeString& getNoFallback(const char* tableKey, const char* itemKey,
82                                 UnicodeString &result) const;
83     UnicodeString& getNoFallback(const char* tableKey, const char* subTableKey, const char* itemKey,
84                                 UnicodeString &result) const;
85 };
86 
87 inline UnicodeString &
get(const char * tableKey,const char * itemKey,UnicodeString & result) const88 ICUDataTable::get(const char* tableKey, const char* itemKey, UnicodeString& result) const {
89     return get(tableKey, NULL, itemKey, result);
90 }
91 
92 inline UnicodeString &
getNoFallback(const char * tableKey,const char * itemKey,UnicodeString & result) const93 ICUDataTable::getNoFallback(const char* tableKey, const char* itemKey, UnicodeString& result) const {
94     return getNoFallback(tableKey, NULL, itemKey, result);
95 }
96 
ICUDataTable(const char * path,const Locale & locale)97 ICUDataTable::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 
~ICUDataTable()110 ICUDataTable::~ICUDataTable() {
111   if (path) {
112     uprv_free((void*) path);
113     path = NULL;
114   }
115 }
116 
117 const Locale&
getLocale()118 ICUDataTable::getLocale() {
119   return locale;
120 }
121 
122 UnicodeString &
get(const char * tableKey,const char * subTableKey,const char * itemKey,UnicodeString & result) const123 ICUDataTable::get(const char* tableKey, const char* subTableKey, const char* itemKey,
124                   UnicodeString &result) const {
125   UErrorCode status = U_ZERO_ERROR;
126   int32_t len = 0;
127 
128   const UChar *s = uloc_getTableStringWithFallback(path, locale.getName(),
129                                                    tableKey, subTableKey, itemKey,
130                                                    &len, &status);
131   if (U_SUCCESS(status) && len > 0) {
132     return result.setTo(s, len);
133   }
134   return result.setTo(UnicodeString(itemKey, -1, US_INV));
135 }
136 
137 UnicodeString &
getNoFallback(const char * tableKey,const char * subTableKey,const char * itemKey,UnicodeString & result) const138 ICUDataTable::getNoFallback(const char* tableKey, const char* subTableKey, const char* itemKey,
139                             UnicodeString& result) const {
140   UErrorCode status = U_ZERO_ERROR;
141   int32_t len = 0;
142 
143   const UChar *s = uloc_getTableStringWithFallback(path, locale.getName(),
144                                                    tableKey, subTableKey, itemKey,
145                                                    &len, &status);
146   if (U_SUCCESS(status)) {
147     return result.setTo(s, len);
148   }
149 
150   result.setToBogus();
151   return result;
152 }
153 
154 ////////////////////////////////////////////////////////////////////////////////////////////////////
155 
~LocaleDisplayNames()156 LocaleDisplayNames::~LocaleDisplayNames() {}
157 
158 ////////////////////////////////////////////////////////////////////////////////////////////////////
159 
160 #if 0  // currently unused
161 
162 class DefaultLocaleDisplayNames : public LocaleDisplayNames {
163   UDialectHandling dialectHandling;
164 
165 public:
166   // constructor
167   DefaultLocaleDisplayNames(UDialectHandling dialectHandling);
168 
169   virtual ~DefaultLocaleDisplayNames();
170 
171   virtual const Locale& getLocale() const;
172   virtual UDialectHandling getDialectHandling() const;
173 
174   virtual UnicodeString& localeDisplayName(const Locale& locale,
175                                            UnicodeString& result) const;
176   virtual UnicodeString& localeDisplayName(const char* localeId,
177                                            UnicodeString& result) const;
178   virtual UnicodeString& languageDisplayName(const char* lang,
179                                              UnicodeString& result) const;
180   virtual UnicodeString& scriptDisplayName(const char* script,
181                                            UnicodeString& result) const;
182   virtual UnicodeString& scriptDisplayName(UScriptCode scriptCode,
183                                            UnicodeString& result) const;
184   virtual UnicodeString& regionDisplayName(const char* region,
185                                            UnicodeString& result) const;
186   virtual UnicodeString& variantDisplayName(const char* variant,
187                                             UnicodeString& result) const;
188   virtual UnicodeString& keyDisplayName(const char* key,
189                                         UnicodeString& result) const;
190   virtual UnicodeString& keyValueDisplayName(const char* key,
191                                              const char* value,
192                                              UnicodeString& result) const;
193 };
194 
195 DefaultLocaleDisplayNames::DefaultLocaleDisplayNames(UDialectHandling dialectHandling)
196     : dialectHandling(dialectHandling) {
197 }
198 
199 DefaultLocaleDisplayNames::~DefaultLocaleDisplayNames() {
200 }
201 
202 const Locale&
203 DefaultLocaleDisplayNames::getLocale() const {
204   return Locale::getRoot();
205 }
206 
207 UDialectHandling
208 DefaultLocaleDisplayNames::getDialectHandling() const {
209   return dialectHandling;
210 }
211 
212 UnicodeString&
213 DefaultLocaleDisplayNames::localeDisplayName(const Locale& locale,
214                                              UnicodeString& result) const {
215   return result = UnicodeString(locale.getName(), -1, US_INV);
216 }
217 
218 UnicodeString&
219 DefaultLocaleDisplayNames::localeDisplayName(const char* localeId,
220                                              UnicodeString& result) const {
221   return result = UnicodeString(localeId, -1, US_INV);
222 }
223 
224 UnicodeString&
225 DefaultLocaleDisplayNames::languageDisplayName(const char* lang,
226                                                UnicodeString& result) const {
227   return result = UnicodeString(lang, -1, US_INV);
228 }
229 
230 UnicodeString&
231 DefaultLocaleDisplayNames::scriptDisplayName(const char* script,
232                                              UnicodeString& result) const {
233   return result = UnicodeString(script, -1, US_INV);
234 }
235 
236 UnicodeString&
237 DefaultLocaleDisplayNames::scriptDisplayName(UScriptCode scriptCode,
238                                              UnicodeString& result) const {
239   const char* name = uscript_getName(scriptCode);
240   if (name) {
241     return result = UnicodeString(name, -1, US_INV);
242   }
243   return result.remove();
244 }
245 
246 UnicodeString&
247 DefaultLocaleDisplayNames::regionDisplayName(const char* region,
248                                              UnicodeString& result) const {
249   return result = UnicodeString(region, -1, US_INV);
250 }
251 
252 UnicodeString&
253 DefaultLocaleDisplayNames::variantDisplayName(const char* variant,
254                                               UnicodeString& result) const {
255   return result = UnicodeString(variant, -1, US_INV);
256 }
257 
258 UnicodeString&
259 DefaultLocaleDisplayNames::keyDisplayName(const char* key,
260                                           UnicodeString& result) const {
261   return result = UnicodeString(key, -1, US_INV);
262 }
263 
264 UnicodeString&
265 DefaultLocaleDisplayNames::keyValueDisplayName(const char* /* key */,
266                                                const char* value,
267                                                UnicodeString& result) const {
268   return result = UnicodeString(value, -1, US_INV);
269 }
270 
271 #endif  // currently unused class DefaultLocaleDisplayNames
272 
273 ////////////////////////////////////////////////////////////////////////////////////////////////////
274 
275 class LocaleDisplayNamesImpl : public LocaleDisplayNames {
276     Locale locale;
277     UDialectHandling dialectHandling;
278     ICUDataTable langData;
279     ICUDataTable regionData;
280     SimpleFormatter separatorFormat;
281     SimpleFormatter format;
282     SimpleFormatter keyTypeFormat;
283     UDisplayContext capitalizationContext;
284 #if !UCONFIG_NO_BREAK_ITERATION
285     BreakIterator* capitalizationBrkIter;
286 #else
287     UObject* capitalizationBrkIter;
288 #endif
289     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 
310 public:
311     // constructor
312     LocaleDisplayNamesImpl(const Locale& locale, UDialectHandling dialectHandling);
313     LocaleDisplayNamesImpl(const Locale& locale, UDisplayContext *contexts, int32_t length);
314     virtual ~LocaleDisplayNamesImpl();
315 
316     virtual const Locale& getLocale() const override;
317     virtual UDialectHandling getDialectHandling() const override;
318     virtual UDisplayContext getContext(UDisplayContextType type) const override;
319 
320     virtual UnicodeString& localeDisplayName(const Locale& locale,
321                                                 UnicodeString& result) const override;
322     virtual UnicodeString& localeDisplayName(const char* localeId,
323                                                 UnicodeString& result) const override;
324     virtual UnicodeString& languageDisplayName(const char* lang,
325                                                UnicodeString& result) const override;
326     virtual UnicodeString& scriptDisplayName(const char* script,
327                                                 UnicodeString& result) const override;
328     virtual UnicodeString& scriptDisplayName(UScriptCode scriptCode,
329                                                 UnicodeString& result) const override;
330     virtual UnicodeString& regionDisplayName(const char* region,
331                                                 UnicodeString& result) const override;
332     virtual UnicodeString& variantDisplayName(const char* variant,
333                                                 UnicodeString& result) const override;
334     virtual UnicodeString& keyDisplayName(const char* key,
335                                                 UnicodeString& result) const override;
336     virtual UnicodeString& keyValueDisplayName(const char* key,
337                                                 const char* value,
338                                                 UnicodeString& result) const override;
339 private:
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 
LocaleDisplayNamesImpl(const Locale & locale,UDialectHandling dialectHandling)355 LocaleDisplayNamesImpl::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 
LocaleDisplayNamesImpl(const Locale & locale,UDisplayContext * contexts,int32_t length)368 LocaleDisplayNamesImpl::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 
401 struct LocaleDisplayNamesImpl::CapitalizationContextSink : public ResourceSink {
402     UBool hasCapitalizationUsage;
403     LocaleDisplayNamesImpl& parent;
404 
CapitalizationContextSinkLocaleDisplayNamesImpl::CapitalizationContextSink405     CapitalizationContextSink(LocaleDisplayNamesImpl& _parent)
406       : hasCapitalizationUsage(FALSE), parent(_parent) {}
407     virtual ~CapitalizationContextSink();
408 
putLocaleDisplayNamesImpl::CapitalizationContextSink409     virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
410             UErrorCode &errorCode) override {
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.
~CapitalizationContextSink()447 LocaleDisplayNamesImpl::CapitalizationContextSink::~CapitalizationContextSink() {}
448 
449 void
initialize(void)450 LocaleDisplayNamesImpl::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 
~LocaleDisplayNamesImpl()519 LocaleDisplayNamesImpl::~LocaleDisplayNamesImpl() {
520 #if !UCONFIG_NO_BREAK_ITERATION
521     delete capitalizationBrkIter;
522 #endif
523 }
524 
525 const Locale&
getLocale() const526 LocaleDisplayNamesImpl::getLocale() const {
527     return locale;
528 }
529 
530 UDialectHandling
getDialectHandling() const531 LocaleDisplayNamesImpl::getDialectHandling() const {
532     return dialectHandling;
533 }
534 
535 UDisplayContext
getContext(UDisplayContextType type) const536 LocaleDisplayNamesImpl::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 
552 UnicodeString&
adjustForUsageAndContext(CapContextUsage usage,UnicodeString & result) const553 LocaleDisplayNamesImpl::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 
568 UnicodeString&
localeDisplayName(const Locale & loc,UnicodeString & result) const569 LocaleDisplayNamesImpl::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 
698 UnicodeString&
appendWithSep(UnicodeString & buffer,const UnicodeString & src) const699 LocaleDisplayNamesImpl::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 
710 UnicodeString&
localeDisplayName(const char * localeId,UnicodeString & result) const711 LocaleDisplayNamesImpl::localeDisplayName(const char* localeId,
712                                           UnicodeString& result) const {
713     return localeDisplayName(Locale(localeId), result);
714 }
715 
716 // private
717 UnicodeString&
localeIdName(const char * localeId,UnicodeString & result,bool substitute) const718 LocaleDisplayNamesImpl::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     langData.getNoFallback("Languages", localeId, result);
727     if (result.isBogus() && uprv_strchr(localeId, '_') == NULL) {
728         // Canonicalize lang and try again, ICU-20870
729         // (only for language codes without script or region)
730         Locale canonLocale = Locale::createCanonical(localeId);
731         const char* canonLocId = canonLocale.getName();
732         if (nameLength == UDISPCTX_LENGTH_SHORT) {
733             langData.getNoFallback("Languages%short", canonLocId, result);
734             if (!result.isBogus()) {
735                 return result;
736             }
737         }
738         langData.getNoFallback("Languages", canonLocId, result);
739     }
740     if (result.isBogus() && substitute) {
741         // use key, this is what langData.get (with fallback) falls back to.
742         result.setTo(UnicodeString(localeId, -1, US_INV)); // use key (
743     }
744     return result;
745 }
746 
747 UnicodeString&
languageDisplayName(const char * lang,UnicodeString & result) const748 LocaleDisplayNamesImpl::languageDisplayName(const char* lang,
749                                             UnicodeString& result) const {
750     if (uprv_strcmp("root", lang) == 0 || uprv_strchr(lang, '_') != NULL) {
751         return result = UnicodeString(lang, -1, US_INV);
752     }
753     if (nameLength == UDISPCTX_LENGTH_SHORT) {
754         langData.getNoFallback("Languages%short", lang, result);
755         if (!result.isBogus()) {
756             return adjustForUsageAndContext(kCapContextUsageLanguage, result);
757         }
758     }
759     langData.getNoFallback("Languages", lang, result);
760     if (result.isBogus()) {
761         // Canonicalize lang and try again, ICU-20870
762         Locale canonLocale = Locale::createCanonical(lang);
763         const char* canonLocId = canonLocale.getName();
764         if (nameLength == UDISPCTX_LENGTH_SHORT) {
765             langData.getNoFallback("Languages%short", canonLocId, result);
766             if (!result.isBogus()) {
767                 return adjustForUsageAndContext(kCapContextUsageLanguage, result);
768             }
769         }
770         langData.getNoFallback("Languages", canonLocId, result);
771     }
772     if (result.isBogus() && substitute == UDISPCTX_SUBSTITUTE) {
773         // use key, this is what langData.get (with fallback) falls back to.
774         result.setTo(UnicodeString(lang, -1, US_INV)); // use key (
775     }
776     return adjustForUsageAndContext(kCapContextUsageLanguage, result);
777 }
778 
779 UnicodeString&
scriptDisplayName(const char * script,UnicodeString & result,UBool skipAdjust) const780 LocaleDisplayNamesImpl::scriptDisplayName(const char* script,
781                                           UnicodeString& result,
782                                           UBool skipAdjust) const {
783     if (nameLength == UDISPCTX_LENGTH_SHORT) {
784         langData.getNoFallback("Scripts%short", script, result);
785         if (!result.isBogus()) {
786             return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageScript, result);
787         }
788     }
789     if (substitute == UDISPCTX_SUBSTITUTE) {
790         langData.get("Scripts", script, result);
791     } else {
792         langData.getNoFallback("Scripts", script, result);
793     }
794     return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageScript, result);
795 }
796 
797 UnicodeString&
scriptDisplayName(const char * script,UnicodeString & result) const798 LocaleDisplayNamesImpl::scriptDisplayName(const char* script,
799                                           UnicodeString& result) const {
800     return scriptDisplayName(script, result, FALSE);
801 }
802 
803 UnicodeString&
scriptDisplayName(UScriptCode scriptCode,UnicodeString & result) const804 LocaleDisplayNamesImpl::scriptDisplayName(UScriptCode scriptCode,
805                                           UnicodeString& result) const {
806     return scriptDisplayName(uscript_getName(scriptCode), result, FALSE);
807 }
808 
809 UnicodeString&
regionDisplayName(const char * region,UnicodeString & result,UBool skipAdjust) const810 LocaleDisplayNamesImpl::regionDisplayName(const char* region,
811                                           UnicodeString& result,
812                                           UBool skipAdjust) const {
813     if (nameLength == UDISPCTX_LENGTH_SHORT) {
814          regionData.getNoFallback("Countries%short", region, result);
815         if (!result.isBogus()) {
816             return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageTerritory, result);
817         }
818     }
819     if (substitute == UDISPCTX_SUBSTITUTE) {
820         regionData.get("Countries", region, result);
821     } else {
822         regionData.getNoFallback("Countries", region, result);
823     }
824     return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageTerritory, result);
825 }
826 
827 UnicodeString&
regionDisplayName(const char * region,UnicodeString & result) const828 LocaleDisplayNamesImpl::regionDisplayName(const char* region,
829                                           UnicodeString& result) const {
830     return regionDisplayName(region, result, FALSE);
831 }
832 
833 
834 UnicodeString&
variantDisplayName(const char * variant,UnicodeString & result,UBool skipAdjust) const835 LocaleDisplayNamesImpl::variantDisplayName(const char* variant,
836                                            UnicodeString& result,
837                                            UBool skipAdjust) const {
838     // don't have a resource for short variant names
839     if (substitute == UDISPCTX_SUBSTITUTE) {
840         langData.get("Variants", variant, result);
841     } else {
842         langData.getNoFallback("Variants", variant, result);
843     }
844     return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageVariant, result);
845 }
846 
847 UnicodeString&
variantDisplayName(const char * variant,UnicodeString & result) const848 LocaleDisplayNamesImpl::variantDisplayName(const char* variant,
849                                            UnicodeString& result) const {
850     return variantDisplayName(variant, result, FALSE);
851 }
852 
853 UnicodeString&
keyDisplayName(const char * key,UnicodeString & result,UBool skipAdjust) const854 LocaleDisplayNamesImpl::keyDisplayName(const char* key,
855                                        UnicodeString& result,
856                                        UBool skipAdjust) const {
857     // don't have a resource for short key names
858     if (substitute == UDISPCTX_SUBSTITUTE) {
859         langData.get("Keys", key, result);
860     } else {
861         langData.getNoFallback("Keys", key, result);
862     }
863     return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKey, result);
864 }
865 
866 UnicodeString&
keyDisplayName(const char * key,UnicodeString & result) const867 LocaleDisplayNamesImpl::keyDisplayName(const char* key,
868                                        UnicodeString& result) const {
869     return keyDisplayName(key, result, FALSE);
870 }
871 
872 UnicodeString&
keyValueDisplayName(const char * key,const char * value,UnicodeString & result,UBool skipAdjust) const873 LocaleDisplayNamesImpl::keyValueDisplayName(const char* key,
874                                             const char* value,
875                                             UnicodeString& result,
876                                             UBool skipAdjust) const {
877     if (uprv_strcmp(key, "currency") == 0) {
878         // ICU4C does not have ICU4J CurrencyDisplayInfo equivalent for now.
879         UErrorCode sts = U_ZERO_ERROR;
880         UnicodeString ustrValue(value, -1, US_INV);
881         int32_t len;
882         const UChar *currencyName = ucurr_getName(ustrValue.getTerminatedBuffer(),
883             locale.getBaseName(), UCURR_LONG_NAME, nullptr /* isChoiceFormat */, &len, &sts);
884         if (U_FAILURE(sts)) {
885             // Return the value as is on failure
886             result = ustrValue;
887             return result;
888         }
889         result.setTo(currencyName, len);
890         return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result);
891     }
892 
893     if (nameLength == UDISPCTX_LENGTH_SHORT) {
894         langData.getNoFallback("Types%short", key, value, result);
895         if (!result.isBogus()) {
896             return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result);
897         }
898     }
899     if (substitute == UDISPCTX_SUBSTITUTE) {
900         langData.get("Types", key, value, result);
901     } else {
902         langData.getNoFallback("Types", key, value, result);
903     }
904     return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result);
905 }
906 
907 UnicodeString&
keyValueDisplayName(const char * key,const char * value,UnicodeString & result) const908 LocaleDisplayNamesImpl::keyValueDisplayName(const char* key,
909                                             const char* value,
910                                             UnicodeString& result) const {
911     return keyValueDisplayName(key, value, result, FALSE);
912 }
913 
914 ////////////////////////////////////////////////////////////////////////////////////////////////////
915 
916 LocaleDisplayNames*
createInstance(const Locale & locale,UDialectHandling dialectHandling)917 LocaleDisplayNames::createInstance(const Locale& locale,
918                                    UDialectHandling dialectHandling) {
919     return new LocaleDisplayNamesImpl(locale, dialectHandling);
920 }
921 
922 LocaleDisplayNames*
createInstance(const Locale & locale,UDisplayContext * contexts,int32_t length)923 LocaleDisplayNames::createInstance(const Locale& locale,
924                                    UDisplayContext *contexts, int32_t length) {
925     if (contexts == NULL) {
926         length = 0;
927     }
928     return new LocaleDisplayNamesImpl(locale, contexts, length);
929 }
930 
931 U_NAMESPACE_END
932 
933 ////////////////////////////////////////////////////////////////////////////////////////////////////
934 
935 U_NAMESPACE_USE
936 
937 U_CAPI ULocaleDisplayNames * U_EXPORT2
uldn_open(const char * locale,UDialectHandling dialectHandling,UErrorCode * pErrorCode)938 uldn_open(const char * locale,
939           UDialectHandling dialectHandling,
940           UErrorCode *pErrorCode) {
941   if (U_FAILURE(*pErrorCode)) {
942     return 0;
943   }
944   if (locale == NULL) {
945     locale = uloc_getDefault();
946   }
947   return (ULocaleDisplayNames *)LocaleDisplayNames::createInstance(Locale(locale), dialectHandling);
948 }
949 
950 U_CAPI ULocaleDisplayNames * U_EXPORT2
uldn_openForContext(const char * locale,UDisplayContext * contexts,int32_t length,UErrorCode * pErrorCode)951 uldn_openForContext(const char * locale,
952                     UDisplayContext *contexts, int32_t length,
953                     UErrorCode *pErrorCode) {
954   if (U_FAILURE(*pErrorCode)) {
955     return 0;
956   }
957   if (locale == NULL) {
958     locale = uloc_getDefault();
959   }
960   return (ULocaleDisplayNames *)LocaleDisplayNames::createInstance(Locale(locale), contexts, length);
961 }
962 
963 
964 U_CAPI void U_EXPORT2
uldn_close(ULocaleDisplayNames * ldn)965 uldn_close(ULocaleDisplayNames *ldn) {
966   delete (LocaleDisplayNames *)ldn;
967 }
968 
969 U_CAPI const char * U_EXPORT2
uldn_getLocale(const ULocaleDisplayNames * ldn)970 uldn_getLocale(const ULocaleDisplayNames *ldn) {
971   if (ldn) {
972     return ((const LocaleDisplayNames *)ldn)->getLocale().getName();
973   }
974   return NULL;
975 }
976 
977 U_CAPI UDialectHandling U_EXPORT2
uldn_getDialectHandling(const ULocaleDisplayNames * ldn)978 uldn_getDialectHandling(const ULocaleDisplayNames *ldn) {
979   if (ldn) {
980     return ((const LocaleDisplayNames *)ldn)->getDialectHandling();
981   }
982   return ULDN_STANDARD_NAMES;
983 }
984 
985 U_CAPI UDisplayContext U_EXPORT2
uldn_getContext(const ULocaleDisplayNames * ldn,UDisplayContextType type,UErrorCode * pErrorCode)986 uldn_getContext(const ULocaleDisplayNames *ldn,
987               UDisplayContextType type,
988               UErrorCode *pErrorCode) {
989   if (U_FAILURE(*pErrorCode)) {
990     return (UDisplayContext)0;
991   }
992   return ((const LocaleDisplayNames *)ldn)->getContext(type);
993 }
994 
995 U_CAPI int32_t U_EXPORT2
uldn_localeDisplayName(const ULocaleDisplayNames * ldn,const char * locale,UChar * result,int32_t maxResultSize,UErrorCode * pErrorCode)996 uldn_localeDisplayName(const ULocaleDisplayNames *ldn,
997                        const char *locale,
998                        UChar *result,
999                        int32_t maxResultSize,
1000                        UErrorCode *pErrorCode) {
1001   if (U_FAILURE(*pErrorCode)) {
1002     return 0;
1003   }
1004   if (ldn == NULL || locale == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1005     *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1006     return 0;
1007   }
1008   UnicodeString temp(result, 0, maxResultSize);
1009   ((const LocaleDisplayNames *)ldn)->localeDisplayName(locale, temp);
1010   if (temp.isBogus()) {
1011     *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1012     return 0;
1013   }
1014   return temp.extract(result, maxResultSize, *pErrorCode);
1015 }
1016 
1017 U_CAPI int32_t U_EXPORT2
uldn_languageDisplayName(const ULocaleDisplayNames * ldn,const char * lang,UChar * result,int32_t maxResultSize,UErrorCode * pErrorCode)1018 uldn_languageDisplayName(const ULocaleDisplayNames *ldn,
1019                          const char *lang,
1020                          UChar *result,
1021                          int32_t maxResultSize,
1022                          UErrorCode *pErrorCode) {
1023   if (U_FAILURE(*pErrorCode)) {
1024     return 0;
1025   }
1026   if (ldn == NULL || lang == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1027     *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1028     return 0;
1029   }
1030   UnicodeString temp(result, 0, maxResultSize);
1031   ((const LocaleDisplayNames *)ldn)->languageDisplayName(lang, temp);
1032   return temp.extract(result, maxResultSize, *pErrorCode);
1033 }
1034 
1035 U_CAPI int32_t U_EXPORT2
uldn_scriptDisplayName(const ULocaleDisplayNames * ldn,const char * script,UChar * result,int32_t maxResultSize,UErrorCode * pErrorCode)1036 uldn_scriptDisplayName(const ULocaleDisplayNames *ldn,
1037                        const char *script,
1038                        UChar *result,
1039                        int32_t maxResultSize,
1040                        UErrorCode *pErrorCode) {
1041   if (U_FAILURE(*pErrorCode)) {
1042     return 0;
1043   }
1044   if (ldn == NULL || script == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1045     *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1046     return 0;
1047   }
1048   UnicodeString temp(result, 0, maxResultSize);
1049   ((const LocaleDisplayNames *)ldn)->scriptDisplayName(script, temp);
1050   return temp.extract(result, maxResultSize, *pErrorCode);
1051 }
1052 
1053 U_CAPI int32_t U_EXPORT2
uldn_scriptCodeDisplayName(const ULocaleDisplayNames * ldn,UScriptCode scriptCode,UChar * result,int32_t maxResultSize,UErrorCode * pErrorCode)1054 uldn_scriptCodeDisplayName(const ULocaleDisplayNames *ldn,
1055                            UScriptCode scriptCode,
1056                            UChar *result,
1057                            int32_t maxResultSize,
1058                            UErrorCode *pErrorCode) {
1059   return uldn_scriptDisplayName(ldn, uscript_getName(scriptCode), result, maxResultSize, pErrorCode);
1060 }
1061 
1062 U_CAPI int32_t U_EXPORT2
uldn_regionDisplayName(const ULocaleDisplayNames * ldn,const char * region,UChar * result,int32_t maxResultSize,UErrorCode * pErrorCode)1063 uldn_regionDisplayName(const ULocaleDisplayNames *ldn,
1064                        const char *region,
1065                        UChar *result,
1066                        int32_t maxResultSize,
1067                        UErrorCode *pErrorCode) {
1068   if (U_FAILURE(*pErrorCode)) {
1069     return 0;
1070   }
1071   if (ldn == NULL || region == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1072     *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1073     return 0;
1074   }
1075   UnicodeString temp(result, 0, maxResultSize);
1076   ((const LocaleDisplayNames *)ldn)->regionDisplayName(region, temp);
1077   return temp.extract(result, maxResultSize, *pErrorCode);
1078 }
1079 
1080 U_CAPI int32_t U_EXPORT2
uldn_variantDisplayName(const ULocaleDisplayNames * ldn,const char * variant,UChar * result,int32_t maxResultSize,UErrorCode * pErrorCode)1081 uldn_variantDisplayName(const ULocaleDisplayNames *ldn,
1082                         const char *variant,
1083                         UChar *result,
1084                         int32_t maxResultSize,
1085                         UErrorCode *pErrorCode) {
1086   if (U_FAILURE(*pErrorCode)) {
1087     return 0;
1088   }
1089   if (ldn == NULL || variant == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1090     *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1091     return 0;
1092   }
1093   UnicodeString temp(result, 0, maxResultSize);
1094   ((const LocaleDisplayNames *)ldn)->variantDisplayName(variant, temp);
1095   return temp.extract(result, maxResultSize, *pErrorCode);
1096 }
1097 
1098 U_CAPI int32_t U_EXPORT2
uldn_keyDisplayName(const ULocaleDisplayNames * ldn,const char * key,UChar * result,int32_t maxResultSize,UErrorCode * pErrorCode)1099 uldn_keyDisplayName(const ULocaleDisplayNames *ldn,
1100                     const char *key,
1101                     UChar *result,
1102                     int32_t maxResultSize,
1103                     UErrorCode *pErrorCode) {
1104   if (U_FAILURE(*pErrorCode)) {
1105     return 0;
1106   }
1107   if (ldn == NULL || key == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1108     *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1109     return 0;
1110   }
1111   UnicodeString temp(result, 0, maxResultSize);
1112   ((const LocaleDisplayNames *)ldn)->keyDisplayName(key, temp);
1113   return temp.extract(result, maxResultSize, *pErrorCode);
1114 }
1115 
1116 U_CAPI int32_t U_EXPORT2
uldn_keyValueDisplayName(const ULocaleDisplayNames * ldn,const char * key,const char * value,UChar * result,int32_t maxResultSize,UErrorCode * pErrorCode)1117 uldn_keyValueDisplayName(const ULocaleDisplayNames *ldn,
1118                          const char *key,
1119                          const char *value,
1120                          UChar *result,
1121                          int32_t maxResultSize,
1122                          UErrorCode *pErrorCode) {
1123   if (U_FAILURE(*pErrorCode)) {
1124     return 0;
1125   }
1126   if (ldn == NULL || key == NULL || value == NULL || (result == NULL && maxResultSize > 0)
1127       || maxResultSize < 0) {
1128     *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1129     return 0;
1130   }
1131   UnicodeString temp(result, 0, maxResultSize);
1132   ((const LocaleDisplayNames *)ldn)->keyValueDisplayName(key, value, temp);
1133   return temp.extract(result, maxResultSize, *pErrorCode);
1134 }
1135 
1136 #endif
1137