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