Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 1 | // © 2016 and later: Unicode, Inc. and others. |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 2 | // License & terms of use: http://www.unicode.org/copyright.html |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 3 | /* |
| 4 | ******************************************************************************* |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 5 | * Copyright (C) 2010-2016, International Business Machines Corporation and |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 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" |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 15 | #include "unicode/simpleformatter.h" |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 16 | #include "unicode/ucasemap.h" |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 17 | #include "unicode/ures.h" |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 18 | #include "unicode/udisplaycontext.h" |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 19 | #include "unicode/brkiter.h" |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 20 | #include "unicode/ucurr.h" |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 21 | #include "cmemory.h" |
| 22 | #include "cstring.h" |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 23 | #include "mutex.h" |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 24 | #include "ulocimp.h" |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 25 | #include "umutex.h" |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 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 | 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); |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 48 | while ((str = va_arg(args, char *)) != 0) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 49 | char c; |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 50 | while (p != e && (c = *str++) != 0) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 51 | *p++ = c; |
| 52 | } |
| 53 | } |
| 54 | *p = 0; |
| 55 | va_end(args); |
| 56 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 57 | return static_cast<int32_t>(p - buffer); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 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 & |
| 88 | ICUDataTable::get(const char* tableKey, const char* itemKey, UnicodeString& result) const { |
| 89 | return get(tableKey, NULL, itemKey, result); |
| 90 | } |
| 91 | |
| 92 | inline UnicodeString & |
| 93 | ICUDataTable::getNoFallback(const char* tableKey, const char* itemKey, UnicodeString& result) const { |
| 94 | return getNoFallback(tableKey, NULL, itemKey, result); |
| 95 | } |
| 96 | |
| 97 | ICUDataTable::ICUDataTable(const char* path, const Locale& locale) |
| 98 | : path(NULL), locale(Locale::getRoot()) |
| 99 | { |
| 100 | if (path) { |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 101 | int32_t len = static_cast<int32_t>(uprv_strlen(path)); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 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 | |
| 110 | ICUDataTable::~ICUDataTable() { |
| 111 | if (path) { |
| 112 | uprv_free((void*) path); |
| 113 | path = NULL; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | const Locale& |
| 118 | ICUDataTable::getLocale() { |
| 119 | return locale; |
| 120 | } |
| 121 | |
| 122 | UnicodeString & |
| 123 | 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 & |
| 138 | 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 | |
| 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; |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 280 | SimpleFormatter separatorFormat; |
| 281 | SimpleFormatter format; |
| 282 | SimpleFormatter keyTypeFormat; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 283 | UDisplayContext capitalizationContext; |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 284 | #if !UCONFIG_NO_BREAK_ITERATION |
| 285 | BreakIterator* capitalizationBrkIter; |
| 286 | #else |
| 287 | UObject* capitalizationBrkIter; |
| 288 | #endif |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 289 | UnicodeString formatOpenParen; |
| 290 | UnicodeString formatReplaceOpenParen; |
| 291 | UnicodeString formatCloseParen; |
| 292 | UnicodeString formatReplaceCloseParen; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 293 | UDisplayContext nameLength; |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 294 | UDisplayContext substitute; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 295 | |
| 296 | // Constants for capitalization context usage types. |
| 297 | enum CapContextUsage { |
| 298 | kCapContextUsageLanguage, |
| 299 | kCapContextUsageScript, |
| 300 | kCapContextUsageTerritory, |
| 301 | kCapContextUsageVariant, |
| 302 | kCapContextUsageKey, |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 303 | kCapContextUsageKeyValue, |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 304 | kCapContextUsageCount |
| 305 | }; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 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]; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 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 | |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 316 | virtual const Locale& getLocale() const override; |
| 317 | virtual UDialectHandling getDialectHandling() const override; |
| 318 | virtual UDisplayContext getContext(UDisplayContextType type) const override; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 319 | |
| 320 | virtual UnicodeString& localeDisplayName(const Locale& locale, |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 321 | UnicodeString& result) const override; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 322 | virtual UnicodeString& localeDisplayName(const char* localeId, |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 323 | UnicodeString& result) const override; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 324 | virtual UnicodeString& languageDisplayName(const char* lang, |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 325 | UnicodeString& result) const override; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 326 | virtual UnicodeString& scriptDisplayName(const char* script, |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 327 | UnicodeString& result) const override; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 328 | virtual UnicodeString& scriptDisplayName(UScriptCode scriptCode, |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 329 | UnicodeString& result) const override; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 330 | virtual UnicodeString& regionDisplayName(const char* region, |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 331 | UnicodeString& result) const override; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 332 | virtual UnicodeString& variantDisplayName(const char* variant, |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 333 | UnicodeString& result) const override; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 334 | virtual UnicodeString& keyDisplayName(const char* key, |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 335 | UnicodeString& result) const override; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 336 | virtual UnicodeString& keyValueDisplayName(const char* key, |
| 337 | const char* value, |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 338 | UnicodeString& result) const override; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 339 | private: |
| 340 | UnicodeString& localeIdName(const char* localeId, |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 341 | UnicodeString& result, bool substitute) const; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 342 | UnicodeString& appendWithSep(UnicodeString& buffer, const UnicodeString& src) const; |
| 343 | UnicodeString& adjustForUsageAndContext(CapContextUsage usage, UnicodeString& result) const; |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 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; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 350 | void initialize(void); |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 351 | |
| 352 | struct CapitalizationContextSink; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | LocaleDisplayNamesImpl::LocaleDisplayNamesImpl(const Locale& locale, |
| 356 | UDialectHandling dialectHandling) |
| 357 | : dialectHandling(dialectHandling) |
| 358 | , langData(U_ICUDATA_LANG, locale) |
| 359 | , regionData(U_ICUDATA_REGION, locale) |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 360 | , capitalizationContext(UDISPCTX_CAPITALIZATION_NONE) |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 361 | , capitalizationBrkIter(NULL) |
| 362 | , nameLength(UDISPCTX_LENGTH_FULL) |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 363 | , substitute(UDISPCTX_SUBSTITUTE) |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 364 | { |
| 365 | initialize(); |
| 366 | } |
| 367 | |
| 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) |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 373 | , capitalizationContext(UDISPCTX_CAPITALIZATION_NONE) |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 374 | , capitalizationBrkIter(NULL) |
| 375 | , nameLength(UDISPCTX_LENGTH_FULL) |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 376 | , substitute(UDISPCTX_SUBSTITUTE) |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 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; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 388 | case UDISPCTX_TYPE_DISPLAY_LENGTH: |
| 389 | nameLength = value; |
| 390 | break; |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 391 | case UDISPCTX_TYPE_SUBSTITUTE_HANDLING: |
| 392 | substitute = value; |
| 393 | break; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 394 | default: |
| 395 | break; |
| 396 | } |
| 397 | } |
| 398 | initialize(); |
| 399 | } |
| 400 | |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 401 | struct LocaleDisplayNamesImpl::CapitalizationContextSink : public ResourceSink { |
| 402 | UBool hasCapitalizationUsage; |
| 403 | LocaleDisplayNamesImpl& parent; |
| 404 | |
| 405 | CapitalizationContextSink(LocaleDisplayNamesImpl& _parent) |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 406 | : hasCapitalizationUsage(false), parent(_parent) {} |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 407 | virtual ~CapitalizationContextSink(); |
| 408 | |
| 409 | virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/, |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 410 | UErrorCode &errorCode) override { |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 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 | |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 440 | parent.fCapitalization[usageEnum] = true; |
| 441 | hasCapitalizationUsage = true; |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | }; |
| 445 | |
| 446 | // Virtual destructors must be defined out of line. |
| 447 | LocaleDisplayNamesImpl::CapitalizationContextSink::~CapitalizationContextSink() {} |
| 448 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 449 | 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; |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 462 | separatorFormat.applyPatternMinMaxArguments(sep, 2, 2, status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 463 | |
| 464 | UnicodeString pattern; |
| 465 | langData.getNoFallback("localeDisplayPattern", "pattern", pattern); |
| 466 | if (pattern.isBogus()) { |
| 467 | pattern = UnicodeString("{0} ({1})", -1, US_INV); |
| 468 | } |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 469 | format.applyPatternMinMaxArguments(pattern, 2, 2, status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 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 | } |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 487 | keyTypeFormat.applyPatternMinMaxArguments(ktPattern, 2, 2, status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 488 | |
| 489 | uprv_memset(fCapitalization, 0, sizeof(fCapitalization)); |
| 490 | #if !UCONFIG_NO_BREAK_ITERATION |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 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) |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 493 | UBool needBrkIter = false; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 494 | if (capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU || capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_STANDALONE) { |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 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; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 504 | } |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 505 | needBrkIter = sink.hasCapitalizationUsage; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 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 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 515 | } |
| 516 | #endif |
| 517 | } |
| 518 | |
| 519 | LocaleDisplayNamesImpl::~LocaleDisplayNamesImpl() { |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 520 | #if !UCONFIG_NO_BREAK_ITERATION |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 521 | delete capitalizationBrkIter; |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 522 | #endif |
| 523 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 524 | |
| 525 | const Locale& |
| 526 | LocaleDisplayNamesImpl::getLocale() const { |
| 527 | return locale; |
| 528 | } |
| 529 | |
| 530 | UDialectHandling |
| 531 | LocaleDisplayNamesImpl::getDialectHandling() const { |
| 532 | return dialectHandling; |
| 533 | } |
| 534 | |
| 535 | UDisplayContext |
| 536 | 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; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 542 | case UDISPCTX_TYPE_DISPLAY_LENGTH: |
| 543 | return nameLength; |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 544 | case UDISPCTX_TYPE_SUBSTITUTE_HANDLING: |
| 545 | return substitute; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 546 | default: |
| 547 | break; |
| 548 | } |
| 549 | return (UDisplayContext)0; |
| 550 | } |
| 551 | |
| 552 | UnicodeString& |
| 553 | LocaleDisplayNamesImpl::adjustForUsageAndContext(CapContextUsage usage, |
| 554 | UnicodeString& result) const { |
| 555 | #if !UCONFIG_NO_BREAK_ITERATION |
| 556 | // check to see whether we need to titlecase result |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 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 |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 560 | static UMutex capitalizationBrkIterLock; |
| 561 | Mutex lock(&capitalizationBrkIterLock); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 562 | result.toTitle(capitalizationBrkIter, locale, U_TITLECASE_NO_LOWERCASE | U_TITLECASE_NO_BREAK_ADJUSTMENT); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 563 | } |
| 564 | #endif |
| 565 | return result; |
| 566 | } |
| 567 | |
| 568 | UnicodeString& |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 569 | LocaleDisplayNamesImpl::localeDisplayName(const Locale& loc, |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 570 | UnicodeString& result) const { |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 571 | if (loc.isBogus()) { |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 572 | result.setToBogus(); |
| 573 | return result; |
| 574 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 575 | UnicodeString resultName; |
| 576 | |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 577 | const char* lang = loc.getLanguage(); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 578 | if (uprv_strlen(lang) == 0) { |
| 579 | lang = "root"; |
| 580 | } |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 581 | const char* script = loc.getScript(); |
| 582 | const char* country = loc.getCountry(); |
| 583 | const char* variant = loc.getVariant(); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 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); |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 594 | localeIdName(buffer, resultName, false); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 595 | if (!resultName.isBogus()) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 596 | hasScript = false; |
| 597 | hasCountry = false; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 598 | break; |
| 599 | } |
| 600 | } |
| 601 | if (hasScript) { |
| 602 | ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", script, (char *)0); |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 603 | localeIdName(buffer, resultName, false); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 604 | if (!resultName.isBogus()) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 605 | hasScript = false; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 606 | break; |
| 607 | } |
| 608 | } |
| 609 | if (hasCountry) { |
| 610 | ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", country, (char*)0); |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 611 | localeIdName(buffer, resultName, false); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 612 | if (!resultName.isBogus()) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 613 | hasCountry = false; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 614 | break; |
| 615 | } |
| 616 | } |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 617 | } while (false); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 618 | } |
| 619 | if (resultName.isBogus() || resultName.isEmpty()) { |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 620 | localeIdName(lang, resultName, substitute == UDISPCTX_SUBSTITUTE); |
| 621 | if (resultName.isBogus()) { |
| 622 | result.setToBogus(); |
| 623 | return result; |
| 624 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | UnicodeString resultRemainder; |
| 628 | UnicodeString temp; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 629 | UErrorCode status = U_ZERO_ERROR; |
| 630 | |
| 631 | if (hasScript) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 632 | UnicodeString script_str = scriptDisplayName(script, temp, true); |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 633 | if (script_str.isBogus()) { |
| 634 | result.setToBogus(); |
| 635 | return result; |
| 636 | } |
| 637 | resultRemainder.append(script_str); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 638 | } |
| 639 | if (hasCountry) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 640 | UnicodeString region_str = regionDisplayName(country, temp, true); |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 641 | if (region_str.isBogus()) { |
| 642 | result.setToBogus(); |
| 643 | return result; |
| 644 | } |
| 645 | appendWithSep(resultRemainder, region_str); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 646 | } |
| 647 | if (hasVariant) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 648 | UnicodeString variant_str = variantDisplayName(variant, temp, true); |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 649 | if (variant_str.isBogus()) { |
| 650 | result.setToBogus(); |
| 651 | return result; |
| 652 | } |
| 653 | appendWithSep(resultRemainder, variant_str); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 654 | } |
| 655 | resultRemainder.findAndReplace(formatOpenParen, formatReplaceOpenParen); |
| 656 | resultRemainder.findAndReplace(formatCloseParen, formatReplaceCloseParen); |
| 657 | |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 658 | LocalPointer<StringEnumeration> e(loc.createKeywords(status)); |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 659 | if (e.isValid() && U_SUCCESS(status)) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 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) { |
Jungshik Shin | 7f873c4 | 2017-10-10 09:45:48 -0700 | [diff] [blame] | 664 | value[0] = 0; |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 665 | loc.getKeywordValue(key, value, ULOC_KEYWORD_AND_VALUES_CAPACITY, status); |
Jungshik Shin | 7f873c4 | 2017-10-10 09:45:48 -0700 | [diff] [blame] | 666 | if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING) { |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 667 | return result; |
| 668 | } |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 669 | keyDisplayName(key, temp, true); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 670 | temp.findAndReplace(formatOpenParen, formatReplaceOpenParen); |
| 671 | temp.findAndReplace(formatCloseParen, formatReplaceCloseParen); |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 672 | keyValueDisplayName(key, value, temp2, true); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 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; |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 679 | keyTypeFormat.format(temp, temp2, temp3, status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 680 | appendWithSep(resultRemainder, temp3); |
| 681 | } else { |
| 682 | appendWithSep(resultRemainder, temp) |
| 683 | .append((UChar)0x3d /* = */) |
| 684 | .append(temp2); |
| 685 | } |
| 686 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | if (!resultRemainder.isEmpty()) { |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 690 | format.format(resultName, resultRemainder, result.remove(), status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 691 | return adjustForUsageAndContext(kCapContextUsageLanguage, result); |
| 692 | } |
| 693 | |
| 694 | result = resultName; |
| 695 | return adjustForUsageAndContext(kCapContextUsageLanguage, result); |
| 696 | } |
| 697 | |
| 698 | UnicodeString& |
| 699 | LocaleDisplayNamesImpl::appendWithSep(UnicodeString& buffer, const UnicodeString& src) const { |
| 700 | if (buffer.isEmpty()) { |
| 701 | buffer.setTo(src); |
| 702 | } else { |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 703 | const UnicodeString *values[2] = { &buffer, &src }; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 704 | UErrorCode status = U_ZERO_ERROR; |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 705 | separatorFormat.formatAndReplace(values, 2, buffer, NULL, 0, status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 706 | } |
| 707 | return buffer; |
| 708 | } |
| 709 | |
| 710 | UnicodeString& |
| 711 | LocaleDisplayNamesImpl::localeDisplayName(const char* localeId, |
| 712 | UnicodeString& result) const { |
| 713 | return localeDisplayName(Locale(localeId), result); |
| 714 | } |
| 715 | |
| 716 | // private |
| 717 | UnicodeString& |
| 718 | LocaleDisplayNamesImpl::localeIdName(const char* localeId, |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 719 | UnicodeString& result, bool substitute) const { |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 720 | if (nameLength == UDISPCTX_LENGTH_SHORT) { |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 721 | langData.getNoFallback("Languages%short", localeId, result); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 722 | if (!result.isBogus()) { |
| 723 | return result; |
| 724 | } |
| 725 | } |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 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); |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 739 | } |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 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; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | UnicodeString& |
| 748 | 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 | } |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 753 | if (nameLength == UDISPCTX_LENGTH_SHORT) { |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 754 | langData.getNoFallback("Languages%short", lang, result); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 755 | if (!result.isBogus()) { |
| 756 | return adjustForUsageAndContext(kCapContextUsageLanguage, result); |
| 757 | } |
| 758 | } |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 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 ( |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 775 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 776 | return adjustForUsageAndContext(kCapContextUsageLanguage, result); |
| 777 | } |
| 778 | |
| 779 | UnicodeString& |
| 780 | LocaleDisplayNamesImpl::scriptDisplayName(const char* script, |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 781 | UnicodeString& result, |
| 782 | UBool skipAdjust) const { |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 783 | if (nameLength == UDISPCTX_LENGTH_SHORT) { |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 784 | langData.getNoFallback("Scripts%short", script, result); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 785 | if (!result.isBogus()) { |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 786 | return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageScript, result); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 787 | } |
| 788 | } |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 789 | if (substitute == UDISPCTX_SUBSTITUTE) { |
| 790 | langData.get("Scripts", script, result); |
| 791 | } else { |
| 792 | langData.getNoFallback("Scripts", script, result); |
| 793 | } |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 794 | return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageScript, result); |
| 795 | } |
| 796 | |
| 797 | UnicodeString& |
| 798 | LocaleDisplayNamesImpl::scriptDisplayName(const char* script, |
| 799 | UnicodeString& result) const { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 800 | return scriptDisplayName(script, result, false); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | UnicodeString& |
| 804 | LocaleDisplayNamesImpl::scriptDisplayName(UScriptCode scriptCode, |
| 805 | UnicodeString& result) const { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 806 | return scriptDisplayName(uscript_getName(scriptCode), result, false); |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | UnicodeString& |
| 810 | LocaleDisplayNamesImpl::regionDisplayName(const char* region, |
| 811 | UnicodeString& result, |
| 812 | UBool skipAdjust) const { |
| 813 | if (nameLength == UDISPCTX_LENGTH_SHORT) { |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 814 | regionData.getNoFallback("Countries%short", region, result); |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 815 | if (!result.isBogus()) { |
| 816 | return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageTerritory, result); |
| 817 | } |
| 818 | } |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 819 | if (substitute == UDISPCTX_SUBSTITUTE) { |
| 820 | regionData.get("Countries", region, result); |
| 821 | } else { |
| 822 | regionData.getNoFallback("Countries", region, result); |
| 823 | } |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 824 | return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageTerritory, result); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | UnicodeString& |
| 828 | LocaleDisplayNamesImpl::regionDisplayName(const char* region, |
| 829 | UnicodeString& result) const { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 830 | return regionDisplayName(region, result, false); |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | |
| 834 | UnicodeString& |
| 835 | LocaleDisplayNamesImpl::variantDisplayName(const char* variant, |
| 836 | UnicodeString& result, |
| 837 | UBool skipAdjust) const { |
| 838 | // don't have a resource for short variant names |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 839 | if (substitute == UDISPCTX_SUBSTITUTE) { |
| 840 | langData.get("Variants", variant, result); |
| 841 | } else { |
| 842 | langData.getNoFallback("Variants", variant, result); |
| 843 | } |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 844 | return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageVariant, result); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | UnicodeString& |
| 848 | LocaleDisplayNamesImpl::variantDisplayName(const char* variant, |
| 849 | UnicodeString& result) const { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 850 | return variantDisplayName(variant, result, false); |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | UnicodeString& |
| 854 | LocaleDisplayNamesImpl::keyDisplayName(const char* key, |
| 855 | UnicodeString& result, |
| 856 | UBool skipAdjust) const { |
| 857 | // don't have a resource for short key names |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 858 | if (substitute == UDISPCTX_SUBSTITUTE) { |
| 859 | langData.get("Keys", key, result); |
| 860 | } else { |
| 861 | langData.getNoFallback("Keys", key, result); |
| 862 | } |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 863 | return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKey, result); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | UnicodeString& |
| 867 | LocaleDisplayNamesImpl::keyDisplayName(const char* key, |
| 868 | UnicodeString& result) const { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 869 | return keyDisplayName(key, result, false); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | UnicodeString& |
| 873 | LocaleDisplayNamesImpl::keyValueDisplayName(const char* key, |
| 874 | const char* value, |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 875 | UnicodeString& result, |
| 876 | UBool skipAdjust) const { |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 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; |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 882 | const UChar *currencyName = ucurr_getName(ustrValue.getTerminatedBuffer(), |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 883 | locale.getBaseName(), UCURR_LONG_NAME, nullptr /* isChoiceFormat */, &len, &sts); |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 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); |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 890 | return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result); |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 891 | } |
| 892 | |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 893 | if (nameLength == UDISPCTX_LENGTH_SHORT) { |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 894 | langData.getNoFallback("Types%short", key, value, result); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 895 | if (!result.isBogus()) { |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 896 | return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 897 | } |
| 898 | } |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 899 | if (substitute == UDISPCTX_SUBSTITUTE) { |
| 900 | langData.get("Types", key, value, result); |
| 901 | } else { |
| 902 | langData.getNoFallback("Types", key, value, result); |
| 903 | } |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 904 | return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result); |
| 905 | } |
| 906 | |
| 907 | UnicodeString& |
| 908 | LocaleDisplayNamesImpl::keyValueDisplayName(const char* key, |
| 909 | const char* value, |
| 910 | UnicodeString& result) const { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 911 | return keyValueDisplayName(key, value, result, false); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 915 | |
| 916 | LocaleDisplayNames* |
| 917 | LocaleDisplayNames::createInstance(const Locale& locale, |
| 918 | UDialectHandling dialectHandling) { |
| 919 | return new LocaleDisplayNamesImpl(locale, dialectHandling); |
| 920 | } |
| 921 | |
| 922 | LocaleDisplayNames* |
| 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 |
| 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 |
| 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 |
| 965 | uldn_close(ULocaleDisplayNames *ldn) { |
| 966 | delete (LocaleDisplayNames *)ldn; |
| 967 | } |
| 968 | |
| 969 | U_CAPI const char * U_EXPORT2 |
| 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 |
| 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 |
| 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 |
| 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); |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 1010 | if (temp.isBogus()) { |
| 1011 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; |
| 1012 | return 0; |
| 1013 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1014 | return temp.extract(result, maxResultSize, *pErrorCode); |
| 1015 | } |
| 1016 | |
| 1017 | U_CAPI int32_t U_EXPORT2 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |