Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * Copyright (C) 2010-2013, International Business Machines Corporation and |
michael@0 | 4 | * others. All Rights Reserved. |
michael@0 | 5 | ******************************************************************************* |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #include "unicode/utypes.h" |
michael@0 | 9 | |
michael@0 | 10 | #if !UCONFIG_NO_FORMATTING |
michael@0 | 11 | |
michael@0 | 12 | #include "unicode/locdspnm.h" |
michael@0 | 13 | #include "unicode/msgfmt.h" |
michael@0 | 14 | #include "unicode/ures.h" |
michael@0 | 15 | #include "unicode/brkiter.h" |
michael@0 | 16 | |
michael@0 | 17 | #include "cmemory.h" |
michael@0 | 18 | #include "cstring.h" |
michael@0 | 19 | #include "ulocimp.h" |
michael@0 | 20 | #include "ureslocs.h" |
michael@0 | 21 | #include "uresimp.h" |
michael@0 | 22 | |
michael@0 | 23 | #include <stdarg.h> |
michael@0 | 24 | |
michael@0 | 25 | /** |
michael@0 | 26 | * Concatenate a number of null-terminated strings to buffer, leaving a |
michael@0 | 27 | * null-terminated string. The last argument should be the null pointer. |
michael@0 | 28 | * Return the length of the string in the buffer, not counting the trailing |
michael@0 | 29 | * null. Return -1 if there is an error (buffer is null, or buflen < 1). |
michael@0 | 30 | */ |
michael@0 | 31 | static int32_t ncat(char *buffer, uint32_t buflen, ...) { |
michael@0 | 32 | va_list args; |
michael@0 | 33 | char *str; |
michael@0 | 34 | char *p = buffer; |
michael@0 | 35 | const char* e = buffer + buflen - 1; |
michael@0 | 36 | |
michael@0 | 37 | if (buffer == NULL || buflen < 1) { |
michael@0 | 38 | return -1; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | va_start(args, buflen); |
michael@0 | 42 | while ((str = va_arg(args, char *))) { |
michael@0 | 43 | char c; |
michael@0 | 44 | while (p != e && (c = *str++)) { |
michael@0 | 45 | *p++ = c; |
michael@0 | 46 | } |
michael@0 | 47 | } |
michael@0 | 48 | *p = 0; |
michael@0 | 49 | va_end(args); |
michael@0 | 50 | |
michael@0 | 51 | return p - buffer; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | U_NAMESPACE_BEGIN |
michael@0 | 55 | |
michael@0 | 56 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
michael@0 | 57 | |
michael@0 | 58 | // Access resource data for locale components. |
michael@0 | 59 | // Wrap code in uloc.c for now. |
michael@0 | 60 | class ICUDataTable { |
michael@0 | 61 | const char* path; |
michael@0 | 62 | Locale locale; |
michael@0 | 63 | |
michael@0 | 64 | public: |
michael@0 | 65 | ICUDataTable(const char* path, const Locale& locale); |
michael@0 | 66 | ~ICUDataTable(); |
michael@0 | 67 | |
michael@0 | 68 | const Locale& getLocale(); |
michael@0 | 69 | |
michael@0 | 70 | UnicodeString& get(const char* tableKey, const char* itemKey, |
michael@0 | 71 | UnicodeString& result) const; |
michael@0 | 72 | UnicodeString& get(const char* tableKey, const char* subTableKey, const char* itemKey, |
michael@0 | 73 | UnicodeString& result) const; |
michael@0 | 74 | |
michael@0 | 75 | UnicodeString& getNoFallback(const char* tableKey, const char* itemKey, |
michael@0 | 76 | UnicodeString &result) const; |
michael@0 | 77 | UnicodeString& getNoFallback(const char* tableKey, const char* subTableKey, const char* itemKey, |
michael@0 | 78 | UnicodeString &result) const; |
michael@0 | 79 | }; |
michael@0 | 80 | |
michael@0 | 81 | inline UnicodeString & |
michael@0 | 82 | ICUDataTable::get(const char* tableKey, const char* itemKey, UnicodeString& result) const { |
michael@0 | 83 | return get(tableKey, NULL, itemKey, result); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | inline UnicodeString & |
michael@0 | 87 | ICUDataTable::getNoFallback(const char* tableKey, const char* itemKey, UnicodeString& result) const { |
michael@0 | 88 | return getNoFallback(tableKey, NULL, itemKey, result); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | ICUDataTable::ICUDataTable(const char* path, const Locale& locale) |
michael@0 | 92 | : path(NULL), locale(Locale::getRoot()) |
michael@0 | 93 | { |
michael@0 | 94 | if (path) { |
michael@0 | 95 | int32_t len = uprv_strlen(path); |
michael@0 | 96 | this->path = (const char*) uprv_malloc(len + 1); |
michael@0 | 97 | if (this->path) { |
michael@0 | 98 | uprv_strcpy((char *)this->path, path); |
michael@0 | 99 | this->locale = locale; |
michael@0 | 100 | } |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | ICUDataTable::~ICUDataTable() { |
michael@0 | 105 | if (path) { |
michael@0 | 106 | uprv_free((void*) path); |
michael@0 | 107 | path = NULL; |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | const Locale& |
michael@0 | 112 | ICUDataTable::getLocale() { |
michael@0 | 113 | return locale; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | UnicodeString & |
michael@0 | 117 | ICUDataTable::get(const char* tableKey, const char* subTableKey, const char* itemKey, |
michael@0 | 118 | UnicodeString &result) const { |
michael@0 | 119 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 120 | int32_t len = 0; |
michael@0 | 121 | |
michael@0 | 122 | const UChar *s = uloc_getTableStringWithFallback(path, locale.getName(), |
michael@0 | 123 | tableKey, subTableKey, itemKey, |
michael@0 | 124 | &len, &status); |
michael@0 | 125 | if (U_SUCCESS(status) && len > 0) { |
michael@0 | 126 | return result.setTo(s, len); |
michael@0 | 127 | } |
michael@0 | 128 | return result.setTo(UnicodeString(itemKey, -1, US_INV)); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | UnicodeString & |
michael@0 | 132 | ICUDataTable::getNoFallback(const char* tableKey, const char* subTableKey, const char* itemKey, |
michael@0 | 133 | UnicodeString& result) const { |
michael@0 | 134 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 135 | int32_t len = 0; |
michael@0 | 136 | |
michael@0 | 137 | const UChar *s = uloc_getTableStringWithFallback(path, locale.getName(), |
michael@0 | 138 | tableKey, subTableKey, itemKey, |
michael@0 | 139 | &len, &status); |
michael@0 | 140 | if (U_SUCCESS(status)) { |
michael@0 | 141 | return result.setTo(s, len); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | result.setToBogus(); |
michael@0 | 145 | return result; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
michael@0 | 149 | |
michael@0 | 150 | LocaleDisplayNames::~LocaleDisplayNames() {} |
michael@0 | 151 | |
michael@0 | 152 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
michael@0 | 153 | |
michael@0 | 154 | #if 0 // currently unused |
michael@0 | 155 | |
michael@0 | 156 | class DefaultLocaleDisplayNames : public LocaleDisplayNames { |
michael@0 | 157 | UDialectHandling dialectHandling; |
michael@0 | 158 | |
michael@0 | 159 | public: |
michael@0 | 160 | // constructor |
michael@0 | 161 | DefaultLocaleDisplayNames(UDialectHandling dialectHandling); |
michael@0 | 162 | |
michael@0 | 163 | virtual ~DefaultLocaleDisplayNames(); |
michael@0 | 164 | |
michael@0 | 165 | virtual const Locale& getLocale() const; |
michael@0 | 166 | virtual UDialectHandling getDialectHandling() const; |
michael@0 | 167 | |
michael@0 | 168 | virtual UnicodeString& localeDisplayName(const Locale& locale, |
michael@0 | 169 | UnicodeString& result) const; |
michael@0 | 170 | virtual UnicodeString& localeDisplayName(const char* localeId, |
michael@0 | 171 | UnicodeString& result) const; |
michael@0 | 172 | virtual UnicodeString& languageDisplayName(const char* lang, |
michael@0 | 173 | UnicodeString& result) const; |
michael@0 | 174 | virtual UnicodeString& scriptDisplayName(const char* script, |
michael@0 | 175 | UnicodeString& result) const; |
michael@0 | 176 | virtual UnicodeString& scriptDisplayName(UScriptCode scriptCode, |
michael@0 | 177 | UnicodeString& result) const; |
michael@0 | 178 | virtual UnicodeString& regionDisplayName(const char* region, |
michael@0 | 179 | UnicodeString& result) const; |
michael@0 | 180 | virtual UnicodeString& variantDisplayName(const char* variant, |
michael@0 | 181 | UnicodeString& result) const; |
michael@0 | 182 | virtual UnicodeString& keyDisplayName(const char* key, |
michael@0 | 183 | UnicodeString& result) const; |
michael@0 | 184 | virtual UnicodeString& keyValueDisplayName(const char* key, |
michael@0 | 185 | const char* value, |
michael@0 | 186 | UnicodeString& result) const; |
michael@0 | 187 | }; |
michael@0 | 188 | |
michael@0 | 189 | DefaultLocaleDisplayNames::DefaultLocaleDisplayNames(UDialectHandling dialectHandling) |
michael@0 | 190 | : dialectHandling(dialectHandling) { |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | DefaultLocaleDisplayNames::~DefaultLocaleDisplayNames() { |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | const Locale& |
michael@0 | 197 | DefaultLocaleDisplayNames::getLocale() const { |
michael@0 | 198 | return Locale::getRoot(); |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | UDialectHandling |
michael@0 | 202 | DefaultLocaleDisplayNames::getDialectHandling() const { |
michael@0 | 203 | return dialectHandling; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | UnicodeString& |
michael@0 | 207 | DefaultLocaleDisplayNames::localeDisplayName(const Locale& locale, |
michael@0 | 208 | UnicodeString& result) const { |
michael@0 | 209 | return result = UnicodeString(locale.getName(), -1, US_INV); |
michael@0 | 210 | } |
michael@0 | 211 | |
michael@0 | 212 | UnicodeString& |
michael@0 | 213 | DefaultLocaleDisplayNames::localeDisplayName(const char* localeId, |
michael@0 | 214 | UnicodeString& result) const { |
michael@0 | 215 | return result = UnicodeString(localeId, -1, US_INV); |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | UnicodeString& |
michael@0 | 219 | DefaultLocaleDisplayNames::languageDisplayName(const char* lang, |
michael@0 | 220 | UnicodeString& result) const { |
michael@0 | 221 | return result = UnicodeString(lang, -1, US_INV); |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | UnicodeString& |
michael@0 | 225 | DefaultLocaleDisplayNames::scriptDisplayName(const char* script, |
michael@0 | 226 | UnicodeString& result) const { |
michael@0 | 227 | return result = UnicodeString(script, -1, US_INV); |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | UnicodeString& |
michael@0 | 231 | DefaultLocaleDisplayNames::scriptDisplayName(UScriptCode scriptCode, |
michael@0 | 232 | UnicodeString& result) const { |
michael@0 | 233 | const char* name = uscript_getName(scriptCode); |
michael@0 | 234 | if (name) { |
michael@0 | 235 | return result = UnicodeString(name, -1, US_INV); |
michael@0 | 236 | } |
michael@0 | 237 | return result.remove(); |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | UnicodeString& |
michael@0 | 241 | DefaultLocaleDisplayNames::regionDisplayName(const char* region, |
michael@0 | 242 | UnicodeString& result) const { |
michael@0 | 243 | return result = UnicodeString(region, -1, US_INV); |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | UnicodeString& |
michael@0 | 247 | DefaultLocaleDisplayNames::variantDisplayName(const char* variant, |
michael@0 | 248 | UnicodeString& result) const { |
michael@0 | 249 | return result = UnicodeString(variant, -1, US_INV); |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | UnicodeString& |
michael@0 | 253 | DefaultLocaleDisplayNames::keyDisplayName(const char* key, |
michael@0 | 254 | UnicodeString& result) const { |
michael@0 | 255 | return result = UnicodeString(key, -1, US_INV); |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | UnicodeString& |
michael@0 | 259 | DefaultLocaleDisplayNames::keyValueDisplayName(const char* /* key */, |
michael@0 | 260 | const char* value, |
michael@0 | 261 | UnicodeString& result) const { |
michael@0 | 262 | return result = UnicodeString(value, -1, US_INV); |
michael@0 | 263 | } |
michael@0 | 264 | |
michael@0 | 265 | #endif // currently unused class DefaultLocaleDisplayNames |
michael@0 | 266 | |
michael@0 | 267 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
michael@0 | 268 | |
michael@0 | 269 | class LocaleDisplayNamesImpl : public LocaleDisplayNames { |
michael@0 | 270 | Locale locale; |
michael@0 | 271 | UDialectHandling dialectHandling; |
michael@0 | 272 | ICUDataTable langData; |
michael@0 | 273 | ICUDataTable regionData; |
michael@0 | 274 | MessageFormat *separatorFormat; |
michael@0 | 275 | MessageFormat *format; |
michael@0 | 276 | MessageFormat *keyTypeFormat; |
michael@0 | 277 | UDisplayContext capitalizationContext; |
michael@0 | 278 | UnicodeString formatOpenParen; |
michael@0 | 279 | UnicodeString formatReplaceOpenParen; |
michael@0 | 280 | UnicodeString formatCloseParen; |
michael@0 | 281 | UnicodeString formatReplaceCloseParen; |
michael@0 | 282 | |
michael@0 | 283 | // Constants for capitalization context usage types. |
michael@0 | 284 | enum CapContextUsage { |
michael@0 | 285 | kCapContextUsageLanguage, |
michael@0 | 286 | kCapContextUsageScript, |
michael@0 | 287 | kCapContextUsageTerritory, |
michael@0 | 288 | kCapContextUsageVariant, |
michael@0 | 289 | kCapContextUsageKey, |
michael@0 | 290 | kCapContextUsageType, |
michael@0 | 291 | kCapContextUsageCount |
michael@0 | 292 | }; |
michael@0 | 293 | // Capitalization transforms. For each usage type, the first array element indicates |
michael@0 | 294 | // whether to titlecase for uiListOrMenu context, the second indicates whether to |
michael@0 | 295 | // titlecase for stand-alone context. |
michael@0 | 296 | UBool fCapitalization[kCapContextUsageCount][2]; |
michael@0 | 297 | |
michael@0 | 298 | public: |
michael@0 | 299 | // constructor |
michael@0 | 300 | LocaleDisplayNamesImpl(const Locale& locale, UDialectHandling dialectHandling); |
michael@0 | 301 | LocaleDisplayNamesImpl(const Locale& locale, UDisplayContext *contexts, int32_t length); |
michael@0 | 302 | virtual ~LocaleDisplayNamesImpl(); |
michael@0 | 303 | |
michael@0 | 304 | virtual const Locale& getLocale() const; |
michael@0 | 305 | virtual UDialectHandling getDialectHandling() const; |
michael@0 | 306 | virtual UDisplayContext getContext(UDisplayContextType type) const; |
michael@0 | 307 | |
michael@0 | 308 | virtual UnicodeString& localeDisplayName(const Locale& locale, |
michael@0 | 309 | UnicodeString& result) const; |
michael@0 | 310 | virtual UnicodeString& localeDisplayName(const char* localeId, |
michael@0 | 311 | UnicodeString& result) const; |
michael@0 | 312 | virtual UnicodeString& languageDisplayName(const char* lang, |
michael@0 | 313 | UnicodeString& result) const; |
michael@0 | 314 | virtual UnicodeString& scriptDisplayName(const char* script, |
michael@0 | 315 | UnicodeString& result) const; |
michael@0 | 316 | virtual UnicodeString& scriptDisplayName(UScriptCode scriptCode, |
michael@0 | 317 | UnicodeString& result) const; |
michael@0 | 318 | virtual UnicodeString& regionDisplayName(const char* region, |
michael@0 | 319 | UnicodeString& result) const; |
michael@0 | 320 | virtual UnicodeString& variantDisplayName(const char* variant, |
michael@0 | 321 | UnicodeString& result) const; |
michael@0 | 322 | virtual UnicodeString& keyDisplayName(const char* key, |
michael@0 | 323 | UnicodeString& result) const; |
michael@0 | 324 | virtual UnicodeString& keyValueDisplayName(const char* key, |
michael@0 | 325 | const char* value, |
michael@0 | 326 | UnicodeString& result) const; |
michael@0 | 327 | private: |
michael@0 | 328 | UnicodeString& localeIdName(const char* localeId, |
michael@0 | 329 | UnicodeString& result) const; |
michael@0 | 330 | UnicodeString& appendWithSep(UnicodeString& buffer, const UnicodeString& src) const; |
michael@0 | 331 | UnicodeString& adjustForUsageAndContext(CapContextUsage usage, UnicodeString& result) const; |
michael@0 | 332 | void initialize(void); |
michael@0 | 333 | }; |
michael@0 | 334 | |
michael@0 | 335 | LocaleDisplayNamesImpl::LocaleDisplayNamesImpl(const Locale& locale, |
michael@0 | 336 | UDialectHandling dialectHandling) |
michael@0 | 337 | : dialectHandling(dialectHandling) |
michael@0 | 338 | , langData(U_ICUDATA_LANG, locale) |
michael@0 | 339 | , regionData(U_ICUDATA_REGION, locale) |
michael@0 | 340 | , separatorFormat(NULL) |
michael@0 | 341 | , format(NULL) |
michael@0 | 342 | , keyTypeFormat(NULL) |
michael@0 | 343 | , capitalizationContext(UDISPCTX_CAPITALIZATION_NONE) |
michael@0 | 344 | { |
michael@0 | 345 | initialize(); |
michael@0 | 346 | } |
michael@0 | 347 | |
michael@0 | 348 | LocaleDisplayNamesImpl::LocaleDisplayNamesImpl(const Locale& locale, |
michael@0 | 349 | UDisplayContext *contexts, int32_t length) |
michael@0 | 350 | : dialectHandling(ULDN_STANDARD_NAMES) |
michael@0 | 351 | , langData(U_ICUDATA_LANG, locale) |
michael@0 | 352 | , regionData(U_ICUDATA_REGION, locale) |
michael@0 | 353 | , separatorFormat(NULL) |
michael@0 | 354 | , format(NULL) |
michael@0 | 355 | , keyTypeFormat(NULL) |
michael@0 | 356 | , capitalizationContext(UDISPCTX_CAPITALIZATION_NONE) |
michael@0 | 357 | { |
michael@0 | 358 | while (length-- > 0) { |
michael@0 | 359 | UDisplayContext value = *contexts++; |
michael@0 | 360 | UDisplayContextType selector = (UDisplayContextType)((uint32_t)value >> 8); |
michael@0 | 361 | switch (selector) { |
michael@0 | 362 | case UDISPCTX_TYPE_DIALECT_HANDLING: |
michael@0 | 363 | dialectHandling = (UDialectHandling)value; |
michael@0 | 364 | break; |
michael@0 | 365 | case UDISPCTX_TYPE_CAPITALIZATION: |
michael@0 | 366 | capitalizationContext = value; |
michael@0 | 367 | break; |
michael@0 | 368 | default: |
michael@0 | 369 | break; |
michael@0 | 370 | } |
michael@0 | 371 | } |
michael@0 | 372 | initialize(); |
michael@0 | 373 | } |
michael@0 | 374 | |
michael@0 | 375 | void |
michael@0 | 376 | LocaleDisplayNamesImpl::initialize(void) { |
michael@0 | 377 | LocaleDisplayNamesImpl *nonConstThis = (LocaleDisplayNamesImpl *)this; |
michael@0 | 378 | nonConstThis->locale = langData.getLocale() == Locale::getRoot() |
michael@0 | 379 | ? regionData.getLocale() |
michael@0 | 380 | : langData.getLocale(); |
michael@0 | 381 | |
michael@0 | 382 | UnicodeString sep; |
michael@0 | 383 | langData.getNoFallback("localeDisplayPattern", "separator", sep); |
michael@0 | 384 | if (sep.isBogus()) { |
michael@0 | 385 | sep = UnicodeString("{0}, {1}", -1, US_INV); |
michael@0 | 386 | } |
michael@0 | 387 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 388 | separatorFormat = new MessageFormat(sep, status); |
michael@0 | 389 | |
michael@0 | 390 | UnicodeString pattern; |
michael@0 | 391 | langData.getNoFallback("localeDisplayPattern", "pattern", pattern); |
michael@0 | 392 | if (pattern.isBogus()) { |
michael@0 | 393 | pattern = UnicodeString("{0} ({1})", -1, US_INV); |
michael@0 | 394 | } |
michael@0 | 395 | format = new MessageFormat(pattern, status); |
michael@0 | 396 | if (pattern.indexOf((UChar)0xFF08) >= 0) { |
michael@0 | 397 | formatOpenParen.setTo((UChar)0xFF08); // fullwidth ( |
michael@0 | 398 | formatReplaceOpenParen.setTo((UChar)0xFF3B); // fullwidth [ |
michael@0 | 399 | formatCloseParen.setTo((UChar)0xFF09); // fullwidth ) |
michael@0 | 400 | formatReplaceCloseParen.setTo((UChar)0xFF3D); // fullwidth ] |
michael@0 | 401 | } else { |
michael@0 | 402 | formatOpenParen.setTo((UChar)0x0028); // ( |
michael@0 | 403 | formatReplaceOpenParen.setTo((UChar)0x005B); // [ |
michael@0 | 404 | formatCloseParen.setTo((UChar)0x0029); // ) |
michael@0 | 405 | formatReplaceCloseParen.setTo((UChar)0x005D); // ] |
michael@0 | 406 | } |
michael@0 | 407 | |
michael@0 | 408 | UnicodeString ktPattern; |
michael@0 | 409 | langData.get("localeDisplayPattern", "keyTypePattern", ktPattern); |
michael@0 | 410 | if (ktPattern.isBogus()) { |
michael@0 | 411 | ktPattern = UnicodeString("{0}={1}", -1, US_INV); |
michael@0 | 412 | } |
michael@0 | 413 | keyTypeFormat = new MessageFormat(ktPattern, status); |
michael@0 | 414 | |
michael@0 | 415 | uprv_memset(fCapitalization, 0, sizeof(fCapitalization)); |
michael@0 | 416 | #if !UCONFIG_NO_BREAK_ITERATION |
michael@0 | 417 | // The following is basically copied from DateFormatSymbols::initializeData |
michael@0 | 418 | typedef struct { |
michael@0 | 419 | const char * usageName; |
michael@0 | 420 | LocaleDisplayNamesImpl::CapContextUsage usageEnum; |
michael@0 | 421 | } ContextUsageNameToEnum; |
michael@0 | 422 | const ContextUsageNameToEnum contextUsageTypeMap[] = { |
michael@0 | 423 | // Entries must be sorted by usageTypeName; entry with NULL name terminates list. |
michael@0 | 424 | { "key", kCapContextUsageKey }, |
michael@0 | 425 | { "languages", kCapContextUsageLanguage }, |
michael@0 | 426 | { "script", kCapContextUsageScript }, |
michael@0 | 427 | { "territory", kCapContextUsageTerritory }, |
michael@0 | 428 | { "type", kCapContextUsageType }, |
michael@0 | 429 | { "variant", kCapContextUsageVariant }, |
michael@0 | 430 | { NULL, (CapContextUsage)0 }, |
michael@0 | 431 | }; |
michael@0 | 432 | int32_t len = 0; |
michael@0 | 433 | UResourceBundle *localeBundle = ures_open(NULL, locale.getName(), &status); |
michael@0 | 434 | if (U_SUCCESS(status)) { |
michael@0 | 435 | UResourceBundle *contextTransforms = ures_getByKeyWithFallback(localeBundle, "contextTransforms", NULL, &status); |
michael@0 | 436 | if (U_SUCCESS(status)) { |
michael@0 | 437 | UResourceBundle *contextTransformUsage; |
michael@0 | 438 | while ( (contextTransformUsage = ures_getNextResource(contextTransforms, NULL, &status)) != NULL ) { |
michael@0 | 439 | const int32_t * intVector = ures_getIntVector(contextTransformUsage, &len, &status); |
michael@0 | 440 | if (U_SUCCESS(status) && intVector != NULL && len >= 2) { |
michael@0 | 441 | const char* usageKey = ures_getKey(contextTransformUsage); |
michael@0 | 442 | if (usageKey != NULL) { |
michael@0 | 443 | const ContextUsageNameToEnum * typeMapPtr = contextUsageTypeMap; |
michael@0 | 444 | int32_t compResult = 0; |
michael@0 | 445 | // linear search; list is short and we cannot be sure that bsearch is available |
michael@0 | 446 | while ( typeMapPtr->usageName != NULL && (compResult = uprv_strcmp(usageKey, typeMapPtr->usageName)) > 0 ) { |
michael@0 | 447 | ++typeMapPtr; |
michael@0 | 448 | } |
michael@0 | 449 | if (typeMapPtr->usageName != NULL && compResult == 0) { |
michael@0 | 450 | fCapitalization[typeMapPtr->usageEnum][0] = intVector[0]; |
michael@0 | 451 | fCapitalization[typeMapPtr->usageEnum][1] = intVector[1]; |
michael@0 | 452 | } |
michael@0 | 453 | } |
michael@0 | 454 | } |
michael@0 | 455 | status = U_ZERO_ERROR; |
michael@0 | 456 | ures_close(contextTransformUsage); |
michael@0 | 457 | } |
michael@0 | 458 | ures_close(contextTransforms); |
michael@0 | 459 | } |
michael@0 | 460 | ures_close(localeBundle); |
michael@0 | 461 | } |
michael@0 | 462 | #endif |
michael@0 | 463 | } |
michael@0 | 464 | |
michael@0 | 465 | LocaleDisplayNamesImpl::~LocaleDisplayNamesImpl() { |
michael@0 | 466 | delete separatorFormat; |
michael@0 | 467 | delete format; |
michael@0 | 468 | delete keyTypeFormat; |
michael@0 | 469 | } |
michael@0 | 470 | |
michael@0 | 471 | const Locale& |
michael@0 | 472 | LocaleDisplayNamesImpl::getLocale() const { |
michael@0 | 473 | return locale; |
michael@0 | 474 | } |
michael@0 | 475 | |
michael@0 | 476 | UDialectHandling |
michael@0 | 477 | LocaleDisplayNamesImpl::getDialectHandling() const { |
michael@0 | 478 | return dialectHandling; |
michael@0 | 479 | } |
michael@0 | 480 | |
michael@0 | 481 | UDisplayContext |
michael@0 | 482 | LocaleDisplayNamesImpl::getContext(UDisplayContextType type) const { |
michael@0 | 483 | switch (type) { |
michael@0 | 484 | case UDISPCTX_TYPE_DIALECT_HANDLING: |
michael@0 | 485 | return (UDisplayContext)dialectHandling; |
michael@0 | 486 | case UDISPCTX_TYPE_CAPITALIZATION: |
michael@0 | 487 | return capitalizationContext; |
michael@0 | 488 | default: |
michael@0 | 489 | break; |
michael@0 | 490 | } |
michael@0 | 491 | return (UDisplayContext)0; |
michael@0 | 492 | } |
michael@0 | 493 | |
michael@0 | 494 | UnicodeString& |
michael@0 | 495 | LocaleDisplayNamesImpl::adjustForUsageAndContext(CapContextUsage usage, |
michael@0 | 496 | UnicodeString& result) const { |
michael@0 | 497 | #if !UCONFIG_NO_BREAK_ITERATION |
michael@0 | 498 | // check to see whether we need to titlecase result |
michael@0 | 499 | UBool titlecase = FALSE; |
michael@0 | 500 | switch (capitalizationContext) { |
michael@0 | 501 | case UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE: |
michael@0 | 502 | titlecase = TRUE; |
michael@0 | 503 | break; |
michael@0 | 504 | case UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU: |
michael@0 | 505 | titlecase = fCapitalization[usage][0]; |
michael@0 | 506 | break; |
michael@0 | 507 | case UDISPCTX_CAPITALIZATION_FOR_STANDALONE: |
michael@0 | 508 | titlecase = fCapitalization[usage][1]; |
michael@0 | 509 | break; |
michael@0 | 510 | default: |
michael@0 | 511 | // titlecase = FALSE; |
michael@0 | 512 | break; |
michael@0 | 513 | } |
michael@0 | 514 | if (titlecase) { |
michael@0 | 515 | // TODO: Fix this titlecase hack when we figure out something better to do. |
michael@0 | 516 | // We don't want to titlecase the whole text, only something like the first word, |
michael@0 | 517 | // of the first segment long enough to have a complete cluster, whichever is |
michael@0 | 518 | // shorter. We could have keep a word break iterator around, but I am not sure |
michael@0 | 519 | // that will do the ight thing for the purposes here. For now we assume that in |
michael@0 | 520 | // languages for which titlecasing makes a difference, we can stop at non-letter |
michael@0 | 521 | // characters in 0x0000-0x00FF and only titlecase up to the first occurrence of |
michael@0 | 522 | // any of those, or to a small number of chars, whichever comes first. |
michael@0 | 523 | int32_t stopPos, stopPosLimit = 8, len = result.length(); |
michael@0 | 524 | if ( stopPosLimit > len ) { |
michael@0 | 525 | stopPosLimit = len; |
michael@0 | 526 | } |
michael@0 | 527 | for ( stopPos = 0; stopPos < stopPosLimit; stopPos++ ) { |
michael@0 | 528 | UChar32 ch = result.char32At(stopPos); |
michael@0 | 529 | if ( (ch < 0x41) || (ch > 0x5A && ch < 0x61) || (ch > 0x7A && ch < 0xC0) ) { |
michael@0 | 530 | break; |
michael@0 | 531 | } |
michael@0 | 532 | if (ch >= 0x10000) { |
michael@0 | 533 | stopPos++; |
michael@0 | 534 | } |
michael@0 | 535 | } |
michael@0 | 536 | if ( stopPos > 0 && stopPos < len ) { |
michael@0 | 537 | UnicodeString firstWord(result, 0, stopPos); |
michael@0 | 538 | firstWord.toTitle(NULL, locale, U_TITLECASE_NO_LOWERCASE | U_TITLECASE_NO_BREAK_ADJUSTMENT); |
michael@0 | 539 | result.replaceBetween(0, stopPos, firstWord); |
michael@0 | 540 | } else { |
michael@0 | 541 | // no stopPos, titlecase the whole text |
michael@0 | 542 | result.toTitle(NULL, locale, U_TITLECASE_NO_LOWERCASE | U_TITLECASE_NO_BREAK_ADJUSTMENT); |
michael@0 | 543 | } |
michael@0 | 544 | } |
michael@0 | 545 | #endif |
michael@0 | 546 | return result; |
michael@0 | 547 | } |
michael@0 | 548 | |
michael@0 | 549 | UnicodeString& |
michael@0 | 550 | LocaleDisplayNamesImpl::localeDisplayName(const Locale& locale, |
michael@0 | 551 | UnicodeString& result) const { |
michael@0 | 552 | UnicodeString resultName; |
michael@0 | 553 | |
michael@0 | 554 | const char* lang = locale.getLanguage(); |
michael@0 | 555 | if (uprv_strlen(lang) == 0) { |
michael@0 | 556 | lang = "root"; |
michael@0 | 557 | } |
michael@0 | 558 | const char* script = locale.getScript(); |
michael@0 | 559 | const char* country = locale.getCountry(); |
michael@0 | 560 | const char* variant = locale.getVariant(); |
michael@0 | 561 | |
michael@0 | 562 | UBool hasScript = uprv_strlen(script) > 0; |
michael@0 | 563 | UBool hasCountry = uprv_strlen(country) > 0; |
michael@0 | 564 | UBool hasVariant = uprv_strlen(variant) > 0; |
michael@0 | 565 | |
michael@0 | 566 | if (dialectHandling == ULDN_DIALECT_NAMES) { |
michael@0 | 567 | char buffer[ULOC_FULLNAME_CAPACITY]; |
michael@0 | 568 | do { // loop construct is so we can break early out of search |
michael@0 | 569 | if (hasScript && hasCountry) { |
michael@0 | 570 | ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", script, "_", country, (char *)0); |
michael@0 | 571 | localeIdName(buffer, resultName); |
michael@0 | 572 | if (!resultName.isBogus()) { |
michael@0 | 573 | hasScript = FALSE; |
michael@0 | 574 | hasCountry = FALSE; |
michael@0 | 575 | break; |
michael@0 | 576 | } |
michael@0 | 577 | } |
michael@0 | 578 | if (hasScript) { |
michael@0 | 579 | ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", script, (char *)0); |
michael@0 | 580 | localeIdName(buffer, resultName); |
michael@0 | 581 | if (!resultName.isBogus()) { |
michael@0 | 582 | hasScript = FALSE; |
michael@0 | 583 | break; |
michael@0 | 584 | } |
michael@0 | 585 | } |
michael@0 | 586 | if (hasCountry) { |
michael@0 | 587 | ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", country, (char*)0); |
michael@0 | 588 | localeIdName(buffer, resultName); |
michael@0 | 589 | if (!resultName.isBogus()) { |
michael@0 | 590 | hasCountry = FALSE; |
michael@0 | 591 | break; |
michael@0 | 592 | } |
michael@0 | 593 | } |
michael@0 | 594 | } while (FALSE); |
michael@0 | 595 | } |
michael@0 | 596 | if (resultName.isBogus() || resultName.isEmpty()) { |
michael@0 | 597 | localeIdName(lang, resultName); |
michael@0 | 598 | } |
michael@0 | 599 | |
michael@0 | 600 | UnicodeString resultRemainder; |
michael@0 | 601 | UnicodeString temp; |
michael@0 | 602 | StringEnumeration *e = NULL; |
michael@0 | 603 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 604 | |
michael@0 | 605 | if (hasScript) { |
michael@0 | 606 | resultRemainder.append(scriptDisplayName(script, temp)); |
michael@0 | 607 | } |
michael@0 | 608 | if (hasCountry) { |
michael@0 | 609 | appendWithSep(resultRemainder, regionDisplayName(country, temp)); |
michael@0 | 610 | } |
michael@0 | 611 | if (hasVariant) { |
michael@0 | 612 | appendWithSep(resultRemainder, variantDisplayName(variant, temp)); |
michael@0 | 613 | } |
michael@0 | 614 | resultRemainder.findAndReplace(formatOpenParen, formatReplaceOpenParen); |
michael@0 | 615 | resultRemainder.findAndReplace(formatCloseParen, formatReplaceCloseParen); |
michael@0 | 616 | |
michael@0 | 617 | e = locale.createKeywords(status); |
michael@0 | 618 | if (e && U_SUCCESS(status)) { |
michael@0 | 619 | UnicodeString temp2; |
michael@0 | 620 | char value[ULOC_KEYWORD_AND_VALUES_CAPACITY]; // sigh, no ULOC_VALUE_CAPACITY |
michael@0 | 621 | const char* key; |
michael@0 | 622 | while ((key = e->next((int32_t *)0, status)) != NULL) { |
michael@0 | 623 | locale.getKeywordValue(key, value, ULOC_KEYWORD_AND_VALUES_CAPACITY, status); |
michael@0 | 624 | keyDisplayName(key, temp); |
michael@0 | 625 | temp.findAndReplace(formatOpenParen, formatReplaceOpenParen); |
michael@0 | 626 | temp.findAndReplace(formatCloseParen, formatReplaceCloseParen); |
michael@0 | 627 | keyValueDisplayName(key, value, temp2); |
michael@0 | 628 | temp2.findAndReplace(formatOpenParen, formatReplaceOpenParen); |
michael@0 | 629 | temp2.findAndReplace(formatCloseParen, formatReplaceCloseParen); |
michael@0 | 630 | if (temp2 != UnicodeString(value, -1, US_INV)) { |
michael@0 | 631 | appendWithSep(resultRemainder, temp2); |
michael@0 | 632 | } else if (temp != UnicodeString(key, -1, US_INV)) { |
michael@0 | 633 | UnicodeString temp3; |
michael@0 | 634 | Formattable data[] = { |
michael@0 | 635 | temp, |
michael@0 | 636 | temp2 |
michael@0 | 637 | }; |
michael@0 | 638 | FieldPosition fpos; |
michael@0 | 639 | status = U_ZERO_ERROR; |
michael@0 | 640 | keyTypeFormat->format(data, 2, temp3, fpos, status); |
michael@0 | 641 | appendWithSep(resultRemainder, temp3); |
michael@0 | 642 | } else { |
michael@0 | 643 | appendWithSep(resultRemainder, temp) |
michael@0 | 644 | .append((UChar)0x3d /* = */) |
michael@0 | 645 | .append(temp2); |
michael@0 | 646 | } |
michael@0 | 647 | } |
michael@0 | 648 | delete e; |
michael@0 | 649 | } |
michael@0 | 650 | |
michael@0 | 651 | if (!resultRemainder.isEmpty()) { |
michael@0 | 652 | Formattable data[] = { |
michael@0 | 653 | resultName, |
michael@0 | 654 | resultRemainder |
michael@0 | 655 | }; |
michael@0 | 656 | FieldPosition fpos; |
michael@0 | 657 | status = U_ZERO_ERROR; |
michael@0 | 658 | format->format(data, 2, result, fpos, status); |
michael@0 | 659 | return adjustForUsageAndContext(kCapContextUsageLanguage, result); |
michael@0 | 660 | } |
michael@0 | 661 | |
michael@0 | 662 | result = resultName; |
michael@0 | 663 | return adjustForUsageAndContext(kCapContextUsageLanguage, result); |
michael@0 | 664 | } |
michael@0 | 665 | |
michael@0 | 666 | UnicodeString& |
michael@0 | 667 | LocaleDisplayNamesImpl::appendWithSep(UnicodeString& buffer, const UnicodeString& src) const { |
michael@0 | 668 | if (buffer.isEmpty()) { |
michael@0 | 669 | buffer.setTo(src); |
michael@0 | 670 | } else { |
michael@0 | 671 | UnicodeString combined; |
michael@0 | 672 | Formattable data[] = { |
michael@0 | 673 | buffer, |
michael@0 | 674 | src |
michael@0 | 675 | }; |
michael@0 | 676 | FieldPosition fpos; |
michael@0 | 677 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 678 | separatorFormat->format(data, 2, combined, fpos, status); |
michael@0 | 679 | if (U_SUCCESS(status)) { |
michael@0 | 680 | buffer.setTo(combined); |
michael@0 | 681 | } |
michael@0 | 682 | } |
michael@0 | 683 | return buffer; |
michael@0 | 684 | } |
michael@0 | 685 | |
michael@0 | 686 | UnicodeString& |
michael@0 | 687 | LocaleDisplayNamesImpl::localeDisplayName(const char* localeId, |
michael@0 | 688 | UnicodeString& result) const { |
michael@0 | 689 | return localeDisplayName(Locale(localeId), result); |
michael@0 | 690 | } |
michael@0 | 691 | |
michael@0 | 692 | // private |
michael@0 | 693 | UnicodeString& |
michael@0 | 694 | LocaleDisplayNamesImpl::localeIdName(const char* localeId, |
michael@0 | 695 | UnicodeString& result) const { |
michael@0 | 696 | return langData.getNoFallback("Languages", localeId, result); |
michael@0 | 697 | } |
michael@0 | 698 | |
michael@0 | 699 | UnicodeString& |
michael@0 | 700 | LocaleDisplayNamesImpl::languageDisplayName(const char* lang, |
michael@0 | 701 | UnicodeString& result) const { |
michael@0 | 702 | if (uprv_strcmp("root", lang) == 0 || uprv_strchr(lang, '_') != NULL) { |
michael@0 | 703 | return result = UnicodeString(lang, -1, US_INV); |
michael@0 | 704 | } |
michael@0 | 705 | langData.get("Languages", lang, result); |
michael@0 | 706 | return adjustForUsageAndContext(kCapContextUsageLanguage, result); |
michael@0 | 707 | } |
michael@0 | 708 | |
michael@0 | 709 | UnicodeString& |
michael@0 | 710 | LocaleDisplayNamesImpl::scriptDisplayName(const char* script, |
michael@0 | 711 | UnicodeString& result) const { |
michael@0 | 712 | langData.get("Scripts", script, result); |
michael@0 | 713 | return adjustForUsageAndContext(kCapContextUsageScript, result); |
michael@0 | 714 | } |
michael@0 | 715 | |
michael@0 | 716 | UnicodeString& |
michael@0 | 717 | LocaleDisplayNamesImpl::scriptDisplayName(UScriptCode scriptCode, |
michael@0 | 718 | UnicodeString& result) const { |
michael@0 | 719 | const char* name = uscript_getName(scriptCode); |
michael@0 | 720 | langData.get("Scripts", name, result); |
michael@0 | 721 | return adjustForUsageAndContext(kCapContextUsageScript, result); |
michael@0 | 722 | } |
michael@0 | 723 | |
michael@0 | 724 | UnicodeString& |
michael@0 | 725 | LocaleDisplayNamesImpl::regionDisplayName(const char* region, |
michael@0 | 726 | UnicodeString& result) const { |
michael@0 | 727 | regionData.get("Countries", region, result); |
michael@0 | 728 | return adjustForUsageAndContext(kCapContextUsageTerritory, result); |
michael@0 | 729 | } |
michael@0 | 730 | |
michael@0 | 731 | UnicodeString& |
michael@0 | 732 | LocaleDisplayNamesImpl::variantDisplayName(const char* variant, |
michael@0 | 733 | UnicodeString& result) const { |
michael@0 | 734 | langData.get("Variants", variant, result); |
michael@0 | 735 | return adjustForUsageAndContext(kCapContextUsageVariant, result); |
michael@0 | 736 | } |
michael@0 | 737 | |
michael@0 | 738 | UnicodeString& |
michael@0 | 739 | LocaleDisplayNamesImpl::keyDisplayName(const char* key, |
michael@0 | 740 | UnicodeString& result) const { |
michael@0 | 741 | langData.get("Keys", key, result); |
michael@0 | 742 | return adjustForUsageAndContext(kCapContextUsageKey, result); |
michael@0 | 743 | } |
michael@0 | 744 | |
michael@0 | 745 | UnicodeString& |
michael@0 | 746 | LocaleDisplayNamesImpl::keyValueDisplayName(const char* key, |
michael@0 | 747 | const char* value, |
michael@0 | 748 | UnicodeString& result) const { |
michael@0 | 749 | langData.get("Types", key, value, result); |
michael@0 | 750 | return adjustForUsageAndContext(kCapContextUsageType, result); |
michael@0 | 751 | } |
michael@0 | 752 | |
michael@0 | 753 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
michael@0 | 754 | |
michael@0 | 755 | LocaleDisplayNames* |
michael@0 | 756 | LocaleDisplayNames::createInstance(const Locale& locale, |
michael@0 | 757 | UDialectHandling dialectHandling) { |
michael@0 | 758 | return new LocaleDisplayNamesImpl(locale, dialectHandling); |
michael@0 | 759 | } |
michael@0 | 760 | |
michael@0 | 761 | LocaleDisplayNames* |
michael@0 | 762 | LocaleDisplayNames::createInstance(const Locale& locale, |
michael@0 | 763 | UDisplayContext *contexts, int32_t length) { |
michael@0 | 764 | if (contexts == NULL) { |
michael@0 | 765 | length = 0; |
michael@0 | 766 | } |
michael@0 | 767 | return new LocaleDisplayNamesImpl(locale, contexts, length); |
michael@0 | 768 | } |
michael@0 | 769 | |
michael@0 | 770 | U_NAMESPACE_END |
michael@0 | 771 | |
michael@0 | 772 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
michael@0 | 773 | |
michael@0 | 774 | U_NAMESPACE_USE |
michael@0 | 775 | |
michael@0 | 776 | U_CAPI ULocaleDisplayNames * U_EXPORT2 |
michael@0 | 777 | uldn_open(const char * locale, |
michael@0 | 778 | UDialectHandling dialectHandling, |
michael@0 | 779 | UErrorCode *pErrorCode) { |
michael@0 | 780 | if (U_FAILURE(*pErrorCode)) { |
michael@0 | 781 | return 0; |
michael@0 | 782 | } |
michael@0 | 783 | if (locale == NULL) { |
michael@0 | 784 | locale = uloc_getDefault(); |
michael@0 | 785 | } |
michael@0 | 786 | return (ULocaleDisplayNames *)LocaleDisplayNames::createInstance(Locale(locale), dialectHandling); |
michael@0 | 787 | } |
michael@0 | 788 | |
michael@0 | 789 | U_CAPI ULocaleDisplayNames * U_EXPORT2 |
michael@0 | 790 | uldn_openForContext(const char * locale, |
michael@0 | 791 | UDisplayContext *contexts, int32_t length, |
michael@0 | 792 | UErrorCode *pErrorCode) { |
michael@0 | 793 | if (U_FAILURE(*pErrorCode)) { |
michael@0 | 794 | return 0; |
michael@0 | 795 | } |
michael@0 | 796 | if (locale == NULL) { |
michael@0 | 797 | locale = uloc_getDefault(); |
michael@0 | 798 | } |
michael@0 | 799 | return (ULocaleDisplayNames *)LocaleDisplayNames::createInstance(Locale(locale), contexts, length); |
michael@0 | 800 | } |
michael@0 | 801 | |
michael@0 | 802 | |
michael@0 | 803 | U_CAPI void U_EXPORT2 |
michael@0 | 804 | uldn_close(ULocaleDisplayNames *ldn) { |
michael@0 | 805 | delete (LocaleDisplayNames *)ldn; |
michael@0 | 806 | } |
michael@0 | 807 | |
michael@0 | 808 | U_CAPI const char * U_EXPORT2 |
michael@0 | 809 | uldn_getLocale(const ULocaleDisplayNames *ldn) { |
michael@0 | 810 | if (ldn) { |
michael@0 | 811 | return ((const LocaleDisplayNames *)ldn)->getLocale().getName(); |
michael@0 | 812 | } |
michael@0 | 813 | return NULL; |
michael@0 | 814 | } |
michael@0 | 815 | |
michael@0 | 816 | U_CAPI UDialectHandling U_EXPORT2 |
michael@0 | 817 | uldn_getDialectHandling(const ULocaleDisplayNames *ldn) { |
michael@0 | 818 | if (ldn) { |
michael@0 | 819 | return ((const LocaleDisplayNames *)ldn)->getDialectHandling(); |
michael@0 | 820 | } |
michael@0 | 821 | return ULDN_STANDARD_NAMES; |
michael@0 | 822 | } |
michael@0 | 823 | |
michael@0 | 824 | U_CAPI UDisplayContext U_EXPORT2 |
michael@0 | 825 | uldn_getContext(const ULocaleDisplayNames *ldn, |
michael@0 | 826 | UDisplayContextType type, |
michael@0 | 827 | UErrorCode *pErrorCode) { |
michael@0 | 828 | if (U_FAILURE(*pErrorCode)) { |
michael@0 | 829 | return (UDisplayContext)0; |
michael@0 | 830 | } |
michael@0 | 831 | return ((const LocaleDisplayNames *)ldn)->getContext(type); |
michael@0 | 832 | } |
michael@0 | 833 | |
michael@0 | 834 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 835 | uldn_localeDisplayName(const ULocaleDisplayNames *ldn, |
michael@0 | 836 | const char *locale, |
michael@0 | 837 | UChar *result, |
michael@0 | 838 | int32_t maxResultSize, |
michael@0 | 839 | UErrorCode *pErrorCode) { |
michael@0 | 840 | if (U_FAILURE(*pErrorCode)) { |
michael@0 | 841 | return 0; |
michael@0 | 842 | } |
michael@0 | 843 | if (ldn == NULL || locale == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) { |
michael@0 | 844 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 845 | return 0; |
michael@0 | 846 | } |
michael@0 | 847 | UnicodeString temp(result, 0, maxResultSize); |
michael@0 | 848 | ((const LocaleDisplayNames *)ldn)->localeDisplayName(locale, temp); |
michael@0 | 849 | return temp.extract(result, maxResultSize, *pErrorCode); |
michael@0 | 850 | } |
michael@0 | 851 | |
michael@0 | 852 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 853 | uldn_languageDisplayName(const ULocaleDisplayNames *ldn, |
michael@0 | 854 | const char *lang, |
michael@0 | 855 | UChar *result, |
michael@0 | 856 | int32_t maxResultSize, |
michael@0 | 857 | UErrorCode *pErrorCode) { |
michael@0 | 858 | if (U_FAILURE(*pErrorCode)) { |
michael@0 | 859 | return 0; |
michael@0 | 860 | } |
michael@0 | 861 | if (ldn == NULL || lang == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) { |
michael@0 | 862 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 863 | return 0; |
michael@0 | 864 | } |
michael@0 | 865 | UnicodeString temp(result, 0, maxResultSize); |
michael@0 | 866 | ((const LocaleDisplayNames *)ldn)->languageDisplayName(lang, temp); |
michael@0 | 867 | return temp.extract(result, maxResultSize, *pErrorCode); |
michael@0 | 868 | } |
michael@0 | 869 | |
michael@0 | 870 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 871 | uldn_scriptDisplayName(const ULocaleDisplayNames *ldn, |
michael@0 | 872 | const char *script, |
michael@0 | 873 | UChar *result, |
michael@0 | 874 | int32_t maxResultSize, |
michael@0 | 875 | UErrorCode *pErrorCode) { |
michael@0 | 876 | if (U_FAILURE(*pErrorCode)) { |
michael@0 | 877 | return 0; |
michael@0 | 878 | } |
michael@0 | 879 | if (ldn == NULL || script == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) { |
michael@0 | 880 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 881 | return 0; |
michael@0 | 882 | } |
michael@0 | 883 | UnicodeString temp(result, 0, maxResultSize); |
michael@0 | 884 | ((const LocaleDisplayNames *)ldn)->scriptDisplayName(script, temp); |
michael@0 | 885 | return temp.extract(result, maxResultSize, *pErrorCode); |
michael@0 | 886 | } |
michael@0 | 887 | |
michael@0 | 888 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 889 | uldn_scriptCodeDisplayName(const ULocaleDisplayNames *ldn, |
michael@0 | 890 | UScriptCode scriptCode, |
michael@0 | 891 | UChar *result, |
michael@0 | 892 | int32_t maxResultSize, |
michael@0 | 893 | UErrorCode *pErrorCode) { |
michael@0 | 894 | return uldn_scriptDisplayName(ldn, uscript_getName(scriptCode), result, maxResultSize, pErrorCode); |
michael@0 | 895 | } |
michael@0 | 896 | |
michael@0 | 897 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 898 | uldn_regionDisplayName(const ULocaleDisplayNames *ldn, |
michael@0 | 899 | const char *region, |
michael@0 | 900 | UChar *result, |
michael@0 | 901 | int32_t maxResultSize, |
michael@0 | 902 | UErrorCode *pErrorCode) { |
michael@0 | 903 | if (U_FAILURE(*pErrorCode)) { |
michael@0 | 904 | return 0; |
michael@0 | 905 | } |
michael@0 | 906 | if (ldn == NULL || region == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) { |
michael@0 | 907 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 908 | return 0; |
michael@0 | 909 | } |
michael@0 | 910 | UnicodeString temp(result, 0, maxResultSize); |
michael@0 | 911 | ((const LocaleDisplayNames *)ldn)->regionDisplayName(region, temp); |
michael@0 | 912 | return temp.extract(result, maxResultSize, *pErrorCode); |
michael@0 | 913 | } |
michael@0 | 914 | |
michael@0 | 915 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 916 | uldn_variantDisplayName(const ULocaleDisplayNames *ldn, |
michael@0 | 917 | const char *variant, |
michael@0 | 918 | UChar *result, |
michael@0 | 919 | int32_t maxResultSize, |
michael@0 | 920 | UErrorCode *pErrorCode) { |
michael@0 | 921 | if (U_FAILURE(*pErrorCode)) { |
michael@0 | 922 | return 0; |
michael@0 | 923 | } |
michael@0 | 924 | if (ldn == NULL || variant == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) { |
michael@0 | 925 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 926 | return 0; |
michael@0 | 927 | } |
michael@0 | 928 | UnicodeString temp(result, 0, maxResultSize); |
michael@0 | 929 | ((const LocaleDisplayNames *)ldn)->variantDisplayName(variant, temp); |
michael@0 | 930 | return temp.extract(result, maxResultSize, *pErrorCode); |
michael@0 | 931 | } |
michael@0 | 932 | |
michael@0 | 933 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 934 | uldn_keyDisplayName(const ULocaleDisplayNames *ldn, |
michael@0 | 935 | const char *key, |
michael@0 | 936 | UChar *result, |
michael@0 | 937 | int32_t maxResultSize, |
michael@0 | 938 | UErrorCode *pErrorCode) { |
michael@0 | 939 | if (U_FAILURE(*pErrorCode)) { |
michael@0 | 940 | return 0; |
michael@0 | 941 | } |
michael@0 | 942 | if (ldn == NULL || key == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) { |
michael@0 | 943 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 944 | return 0; |
michael@0 | 945 | } |
michael@0 | 946 | UnicodeString temp(result, 0, maxResultSize); |
michael@0 | 947 | ((const LocaleDisplayNames *)ldn)->keyDisplayName(key, temp); |
michael@0 | 948 | return temp.extract(result, maxResultSize, *pErrorCode); |
michael@0 | 949 | } |
michael@0 | 950 | |
michael@0 | 951 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 952 | uldn_keyValueDisplayName(const ULocaleDisplayNames *ldn, |
michael@0 | 953 | const char *key, |
michael@0 | 954 | const char *value, |
michael@0 | 955 | UChar *result, |
michael@0 | 956 | int32_t maxResultSize, |
michael@0 | 957 | UErrorCode *pErrorCode) { |
michael@0 | 958 | if (U_FAILURE(*pErrorCode)) { |
michael@0 | 959 | return 0; |
michael@0 | 960 | } |
michael@0 | 961 | if (ldn == NULL || key == NULL || value == NULL || (result == NULL && maxResultSize > 0) |
michael@0 | 962 | || maxResultSize < 0) { |
michael@0 | 963 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 964 | return 0; |
michael@0 | 965 | } |
michael@0 | 966 | UnicodeString temp(result, 0, maxResultSize); |
michael@0 | 967 | ((const LocaleDisplayNames *)ldn)->keyValueDisplayName(key, value, temp); |
michael@0 | 968 | return temp.extract(result, maxResultSize, *pErrorCode); |
michael@0 | 969 | } |
michael@0 | 970 | |
michael@0 | 971 | #endif |