UnicodeString& LocaleDisplayNamesImpl::keyValueDisplayName(const char* key, const char* value, UnicodeString& result, UBool skipAdjust) const { if (uprv_strcmp(key, "currency") == 0) { // ICU4C does not have ICU4J CurrencyDisplayInfo equivalent for now. UErrorCode sts = U_ZERO_ERROR; UnicodeString ustrValue(value, -1, US_INV); int32_t len; UBool isChoice = FALSE; const UChar *currencyName = ucurr_getName(ustrValue.getTerminatedBuffer(), locale.getBaseName(), UCURR_LONG_NAME, &isChoice, &len, &sts); if (U_FAILURE(sts)) { // Return the value as is on failure result = ustrValue; return result; } result.setTo(currencyName, len); return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result); } if (nameLength == UDISPCTX_LENGTH_SHORT) { langData.get("Types%short", key, value, result); if (!result.isBogus()) { return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result); } } langData.get("Types", key, value, result); return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result); }
/* Function: GetLocaleCurrencyName Gets the locale currency English or native name and convert the result to UChars */ static UErrorCode GetLocaleCurrencyName(const char* locale, UBool nativeName, UChar* value, int32_t valueLength) { UErrorCode status = U_ZERO_ERROR; UChar currencyThreeLettersName[4]; // 3 letters currency iso name + NULL ucurr_forLocale(locale, currencyThreeLettersName, 4, &status); if (!U_SUCCESS(status)) { return status; } int32_t len; UBool formatChoice; const UChar *pCurrencyLongName = ucurr_getName( currencyThreeLettersName, nativeName ? locale : ULOC_US, UCURR_LONG_NAME, &formatChoice, &len, &status); if (!U_SUCCESS(status)) { return status; } if (len >= valueLength) // we need to have room for NULL too { return U_BUFFER_OVERFLOW_ERROR; } u_strncpy(value, pCurrencyLongName, len); value[len] = 0; return status; }
static jstring getCurrencyName(JNIEnv* env, jstring javaLanguageTag, jstring javaCurrencyCode, UCurrNameStyle nameStyle) { ScopedUtfChars languageTag(env, javaLanguageTag); if (languageTag.c_str() == NULL) { return NULL; } ScopedJavaUnicodeString currencyCode(env, javaCurrencyCode); if (!currencyCode.valid()) { return NULL; } UnicodeString icuCurrencyCode(currencyCode.unicodeString()); UErrorCode status = U_ZERO_ERROR; UBool isChoiceFormat = false; int32_t charCount; const UChar* chars = ucurr_getName(icuCurrencyCode.getTerminatedBuffer(), languageTag.c_str(), nameStyle, &isChoiceFormat, &charCount, &status); if (status == U_USING_DEFAULT_WARNING) { if (nameStyle == UCURR_SYMBOL_NAME) { // ICU doesn't distinguish between falling back to the root locale and meeting a genuinely // unknown currency. The Currency class does. if (!ucurr_isAvailable(icuCurrencyCode.getTerminatedBuffer(), U_DATE_MIN, U_DATE_MAX, &status)) { return NULL; } } if (nameStyle == UCURR_LONG_NAME) { // ICU's default is English. We want the ISO 4217 currency code instead. chars = icuCurrencyCode.getBuffer(); charCount = icuCurrencyCode.length(); } } return (charCount == 0) ? NULL : env->NewString(chars, charCount); }
static bool __CFLocaleICUCurrencyName(const char *locale, const char *value, UCurrNameStyle style, CFStringRef *out) { size_t valLen = strlen(value); if (valLen != 3) // not a valid ISO code return false; UChar curr[4]; UBool isChoice = FALSE; int32_t size = 0; UErrorCode icuStatus = U_ZERO_ERROR; u_charsToUChars(value, curr, (int32_t)valLen); curr[valLen] = '\0'; const UChar *name; name = ucurr_getName(curr, locale, style, &isChoice, &size, &icuStatus); if (U_FAILURE(icuStatus) || icuStatus == U_USING_DEFAULT_WARNING) return false; UChar result[kMaxICUNameSize]; if (isChoice) { UChar pattern[kMaxICUNameSize]; CFStringRef patternRef = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("{0,choice,%S}"), name); CFIndex pattlen = CFStringGetLength(patternRef); CFStringGetCharacters(patternRef, CFRangeMake(0, pattlen), (UniChar *)pattern); CFRelease(patternRef); pattern[pattlen] = '\0'; // null terminate the pattern // Format the message assuming a large amount of the currency size = u_formatMessage("en_US", pattern, pattlen, result, kMaxICUNameSize, &icuStatus, 10.0); if (U_FAILURE(icuStatus)) return false; name = result; } *out = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (UniChar *)name, size); return (*out != NULL); }
static jstring ICU_getCurrencyDisplayName(JNIEnv* env, jclass, jstring javaLocaleName, jstring javaCurrencyCode) { ScopedUtfChars localeName(env, javaLocaleName); ScopedJavaUnicodeString currencyCode(env, javaCurrencyCode); UnicodeString icuCurrencyCode(currencyCode.unicodeString()); UErrorCode status = U_ZERO_ERROR; UBool isChoiceFormat; int32_t charCount; const UChar* chars = ucurr_getName(icuCurrencyCode.getTerminatedBuffer(), localeName.c_str(), UCURR_LONG_NAME, &isChoiceFormat, &charCount, &status); if (status == U_USING_DEFAULT_WARNING) { // ICU's default is English. We want the ISO 4217 currency code instead. chars = icuCurrencyCode.getBuffer(); charCount = icuCurrencyCode.length(); } return (charCount == 0) ? NULL : env->NewString(chars, charCount); }
void CurrencyAffixInfo::set( const char *locale, const PluralRules *rules, const UChar *currency, UErrorCode &status) { if (U_FAILURE(status)) { return; } fIsDefault = FALSE; if (currency == NULL) { fSymbol.setTo(gDefaultSymbols, 1); fISO.setTo(gDefaultSymbols, 2); fLong.remove(); fLong.append(gDefaultSymbols, 3); fIsDefault = TRUE; return; } int32_t len; UBool unusedIsChoice; const UChar *symbol = ucurr_getName( currency, locale, UCURR_SYMBOL_NAME, &unusedIsChoice, &len, &status); if (U_FAILURE(status)) { return; } fSymbol.setTo(symbol, len); fISO.setTo(currency, u_strlen(currency)); fLong.remove(); StringEnumeration* keywords = rules->getKeywords(status); if (U_FAILURE(status)) { return; } const UnicodeString* pluralCount; while ((pluralCount = keywords->snext(status)) != NULL) { CharString pCount; pCount.appendInvariantChars(*pluralCount, status); const UChar *pluralName = ucurr_getPluralName( currency, locale, &unusedIsChoice, pCount.data(), &len, &status); fLong.setVariant(pCount.data(), UnicodeString(pluralName, len), status); } delete keywords; }