michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 1997-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: locdispnames.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2010feb25 michael@0: * created by: Markus W. Scherer michael@0: * michael@0: * Code for locale display names, separated out from other .cpp files michael@0: * that then do not depend on resource bundle code and display name data. michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/brkiter.h" michael@0: #include "unicode/locid.h" michael@0: #include "unicode/uloc.h" michael@0: #include "unicode/ures.h" michael@0: #include "unicode/ustring.h" michael@0: #include "cmemory.h" michael@0: #include "cstring.h" michael@0: #include "putilimp.h" michael@0: #include "ulocimp.h" michael@0: #include "uresimp.h" michael@0: #include "ureslocs.h" michael@0: #include "ustr_imp.h" michael@0: michael@0: // C++ API ----------------------------------------------------------------- *** michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: UnicodeString& michael@0: Locale::getDisplayLanguage(UnicodeString& dispLang) const michael@0: { michael@0: return this->getDisplayLanguage(getDefault(), dispLang); michael@0: } michael@0: michael@0: /*We cannot make any assumptions on the size of the output display strings michael@0: * Yet, since we are calling through to a C API, we need to set limits on michael@0: * buffer size. For all the following getDisplay functions we first attempt michael@0: * to fill up a stack allocated buffer. If it is to small we heap allocated michael@0: * the exact buffer we need copy it to the UnicodeString and delete it*/ michael@0: michael@0: UnicodeString& michael@0: Locale::getDisplayLanguage(const Locale &displayLocale, michael@0: UnicodeString &result) const { michael@0: UChar *buffer; michael@0: UErrorCode errorCode=U_ZERO_ERROR; michael@0: int32_t length; michael@0: michael@0: buffer=result.getBuffer(ULOC_FULLNAME_CAPACITY); michael@0: if(buffer==0) { michael@0: result.truncate(0); michael@0: return result; michael@0: } michael@0: michael@0: length=uloc_getDisplayLanguage(fullName, displayLocale.fullName, michael@0: buffer, result.getCapacity(), michael@0: &errorCode); michael@0: result.releaseBuffer(U_SUCCESS(errorCode) ? length : 0); michael@0: michael@0: if(errorCode==U_BUFFER_OVERFLOW_ERROR) { michael@0: buffer=result.getBuffer(length); michael@0: if(buffer==0) { michael@0: result.truncate(0); michael@0: return result; michael@0: } michael@0: errorCode=U_ZERO_ERROR; michael@0: length=uloc_getDisplayLanguage(fullName, displayLocale.fullName, michael@0: buffer, result.getCapacity(), michael@0: &errorCode); michael@0: result.releaseBuffer(U_SUCCESS(errorCode) ? length : 0); michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: UnicodeString& michael@0: Locale::getDisplayScript(UnicodeString& dispScript) const michael@0: { michael@0: return this->getDisplayScript(getDefault(), dispScript); michael@0: } michael@0: michael@0: UnicodeString& michael@0: Locale::getDisplayScript(const Locale &displayLocale, michael@0: UnicodeString &result) const { michael@0: UChar *buffer; michael@0: UErrorCode errorCode=U_ZERO_ERROR; michael@0: int32_t length; michael@0: michael@0: buffer=result.getBuffer(ULOC_FULLNAME_CAPACITY); michael@0: if(buffer==0) { michael@0: result.truncate(0); michael@0: return result; michael@0: } michael@0: michael@0: length=uloc_getDisplayScript(fullName, displayLocale.fullName, michael@0: buffer, result.getCapacity(), michael@0: &errorCode); michael@0: result.releaseBuffer(U_SUCCESS(errorCode) ? length : 0); michael@0: michael@0: if(errorCode==U_BUFFER_OVERFLOW_ERROR) { michael@0: buffer=result.getBuffer(length); michael@0: if(buffer==0) { michael@0: result.truncate(0); michael@0: return result; michael@0: } michael@0: errorCode=U_ZERO_ERROR; michael@0: length=uloc_getDisplayScript(fullName, displayLocale.fullName, michael@0: buffer, result.getCapacity(), michael@0: &errorCode); michael@0: result.releaseBuffer(U_SUCCESS(errorCode) ? length : 0); michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: UnicodeString& michael@0: Locale::getDisplayCountry(UnicodeString& dispCntry) const michael@0: { michael@0: return this->getDisplayCountry(getDefault(), dispCntry); michael@0: } michael@0: michael@0: UnicodeString& michael@0: Locale::getDisplayCountry(const Locale &displayLocale, michael@0: UnicodeString &result) const { michael@0: UChar *buffer; michael@0: UErrorCode errorCode=U_ZERO_ERROR; michael@0: int32_t length; michael@0: michael@0: buffer=result.getBuffer(ULOC_FULLNAME_CAPACITY); michael@0: if(buffer==0) { michael@0: result.truncate(0); michael@0: return result; michael@0: } michael@0: michael@0: length=uloc_getDisplayCountry(fullName, displayLocale.fullName, michael@0: buffer, result.getCapacity(), michael@0: &errorCode); michael@0: result.releaseBuffer(U_SUCCESS(errorCode) ? length : 0); michael@0: michael@0: if(errorCode==U_BUFFER_OVERFLOW_ERROR) { michael@0: buffer=result.getBuffer(length); michael@0: if(buffer==0) { michael@0: result.truncate(0); michael@0: return result; michael@0: } michael@0: errorCode=U_ZERO_ERROR; michael@0: length=uloc_getDisplayCountry(fullName, displayLocale.fullName, michael@0: buffer, result.getCapacity(), michael@0: &errorCode); michael@0: result.releaseBuffer(U_SUCCESS(errorCode) ? length : 0); michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: UnicodeString& michael@0: Locale::getDisplayVariant(UnicodeString& dispVar) const michael@0: { michael@0: return this->getDisplayVariant(getDefault(), dispVar); michael@0: } michael@0: michael@0: UnicodeString& michael@0: Locale::getDisplayVariant(const Locale &displayLocale, michael@0: UnicodeString &result) const { michael@0: UChar *buffer; michael@0: UErrorCode errorCode=U_ZERO_ERROR; michael@0: int32_t length; michael@0: michael@0: buffer=result.getBuffer(ULOC_FULLNAME_CAPACITY); michael@0: if(buffer==0) { michael@0: result.truncate(0); michael@0: return result; michael@0: } michael@0: michael@0: length=uloc_getDisplayVariant(fullName, displayLocale.fullName, michael@0: buffer, result.getCapacity(), michael@0: &errorCode); michael@0: result.releaseBuffer(U_SUCCESS(errorCode) ? length : 0); michael@0: michael@0: if(errorCode==U_BUFFER_OVERFLOW_ERROR) { michael@0: buffer=result.getBuffer(length); michael@0: if(buffer==0) { michael@0: result.truncate(0); michael@0: return result; michael@0: } michael@0: errorCode=U_ZERO_ERROR; michael@0: length=uloc_getDisplayVariant(fullName, displayLocale.fullName, michael@0: buffer, result.getCapacity(), michael@0: &errorCode); michael@0: result.releaseBuffer(U_SUCCESS(errorCode) ? length : 0); michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: UnicodeString& michael@0: Locale::getDisplayName( UnicodeString& name ) const michael@0: { michael@0: return this->getDisplayName(getDefault(), name); michael@0: } michael@0: michael@0: UnicodeString& michael@0: Locale::getDisplayName(const Locale &displayLocale, michael@0: UnicodeString &result) const { michael@0: UChar *buffer; michael@0: UErrorCode errorCode=U_ZERO_ERROR; michael@0: int32_t length; michael@0: michael@0: buffer=result.getBuffer(ULOC_FULLNAME_CAPACITY); michael@0: if(buffer==0) { michael@0: result.truncate(0); michael@0: return result; michael@0: } michael@0: michael@0: length=uloc_getDisplayName(fullName, displayLocale.fullName, michael@0: buffer, result.getCapacity(), michael@0: &errorCode); michael@0: result.releaseBuffer(U_SUCCESS(errorCode) ? length : 0); michael@0: michael@0: if(errorCode==U_BUFFER_OVERFLOW_ERROR) { michael@0: buffer=result.getBuffer(length); michael@0: if(buffer==0) { michael@0: result.truncate(0); michael@0: return result; michael@0: } michael@0: errorCode=U_ZERO_ERROR; michael@0: length=uloc_getDisplayName(fullName, displayLocale.fullName, michael@0: buffer, result.getCapacity(), michael@0: &errorCode); michael@0: result.releaseBuffer(U_SUCCESS(errorCode) ? length : 0); michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: #if ! UCONFIG_NO_BREAK_ITERATION michael@0: michael@0: // ------------------------------------- michael@0: // Gets the objectLocale display name in the default locale language. michael@0: UnicodeString& U_EXPORT2 michael@0: BreakIterator::getDisplayName(const Locale& objectLocale, michael@0: UnicodeString& name) michael@0: { michael@0: return objectLocale.getDisplayName(name); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: // Gets the objectLocale display name in the displayLocale language. michael@0: UnicodeString& U_EXPORT2 michael@0: BreakIterator::getDisplayName(const Locale& objectLocale, michael@0: const Locale& displayLocale, michael@0: UnicodeString& name) michael@0: { michael@0: return objectLocale.getDisplayName(displayLocale, name); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: // C API ------------------------------------------------------------------- *** michael@0: michael@0: U_NAMESPACE_USE michael@0: michael@0: /* ### Constants **************************************************/ michael@0: michael@0: /* These strings describe the resources we attempt to load from michael@0: the locale ResourceBundle data file.*/ michael@0: static const char _kLanguages[] = "Languages"; michael@0: static const char _kScripts[] = "Scripts"; michael@0: static const char _kScriptsStandAlone[] = "Scripts%stand-alone"; michael@0: static const char _kCountries[] = "Countries"; michael@0: static const char _kVariants[] = "Variants"; michael@0: static const char _kKeys[] = "Keys"; michael@0: static const char _kTypes[] = "Types"; michael@0: //static const char _kRootName[] = "root"; michael@0: static const char _kCurrency[] = "currency"; michael@0: static const char _kCurrencies[] = "Currencies"; michael@0: static const char _kLocaleDisplayPattern[] = "localeDisplayPattern"; michael@0: static const char _kPattern[] = "pattern"; michael@0: static const char _kSeparator[] = "separator"; michael@0: michael@0: /* ### Display name **************************************************/ michael@0: michael@0: static int32_t michael@0: _getStringOrCopyKey(const char *path, const char *locale, michael@0: const char *tableKey, michael@0: const char* subTableKey, michael@0: const char *itemKey, michael@0: const char *substitute, michael@0: UChar *dest, int32_t destCapacity, michael@0: UErrorCode *pErrorCode) { michael@0: const UChar *s = NULL; michael@0: int32_t length = 0; michael@0: michael@0: if(itemKey==NULL) { michael@0: /* top-level item: normal resource bundle access */ michael@0: UResourceBundle *rb; michael@0: michael@0: rb=ures_open(path, locale, pErrorCode); michael@0: michael@0: if(U_SUCCESS(*pErrorCode)) { michael@0: s=ures_getStringByKey(rb, tableKey, &length, pErrorCode); michael@0: /* see comment about closing rb near "return item;" in _res_getTableStringWithFallback() */ michael@0: ures_close(rb); michael@0: } michael@0: } else { michael@0: /* Language code should not be a number. If it is, set the error code. */ michael@0: if (!uprv_strncmp(tableKey, "Languages", 9) && uprv_strtol(itemKey, NULL, 10)) { michael@0: *pErrorCode = U_MISSING_RESOURCE_ERROR; michael@0: } else { michael@0: /* second-level item, use special fallback */ michael@0: s=uloc_getTableStringWithFallback(path, locale, michael@0: tableKey, michael@0: subTableKey, michael@0: itemKey, michael@0: &length, michael@0: pErrorCode); michael@0: } michael@0: } michael@0: michael@0: if(U_SUCCESS(*pErrorCode)) { michael@0: int32_t copyLength=uprv_min(length, destCapacity); michael@0: if(copyLength>0 && s != NULL) { michael@0: u_memcpy(dest, s, copyLength); michael@0: } michael@0: } else { michael@0: /* no string from a resource bundle: convert the substitute */ michael@0: length=(int32_t)uprv_strlen(substitute); michael@0: u_charsToUChars(substitute, dest, uprv_min(length, destCapacity)); michael@0: *pErrorCode=U_USING_DEFAULT_WARNING; michael@0: } michael@0: michael@0: return u_terminateUChars(dest, destCapacity, length, pErrorCode); michael@0: } michael@0: michael@0: typedef int32_t U_CALLCONV UDisplayNameGetter(const char *, char *, int32_t, UErrorCode *); michael@0: michael@0: static int32_t michael@0: _getDisplayNameForComponent(const char *locale, michael@0: const char *displayLocale, michael@0: UChar *dest, int32_t destCapacity, michael@0: UDisplayNameGetter *getter, michael@0: const char *tag, michael@0: UErrorCode *pErrorCode) { michael@0: char localeBuffer[ULOC_FULLNAME_CAPACITY*4]; michael@0: int32_t length; michael@0: UErrorCode localStatus; michael@0: const char* root = NULL; michael@0: michael@0: /* argument checking */ michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: michael@0: if(destCapacity<0 || (destCapacity>0 && dest==NULL)) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: localStatus = U_ZERO_ERROR; michael@0: length=(*getter)(locale, localeBuffer, sizeof(localeBuffer), &localStatus); michael@0: if(U_FAILURE(localStatus) || localStatus==U_STRING_NOT_TERMINATED_WARNING) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: if(length==0) { michael@0: return u_terminateUChars(dest, destCapacity, 0, pErrorCode); michael@0: } michael@0: michael@0: root = tag == _kCountries ? U_ICUDATA_REGION : U_ICUDATA_LANG; michael@0: michael@0: return _getStringOrCopyKey(root, displayLocale, michael@0: tag, NULL, localeBuffer, michael@0: localeBuffer, michael@0: dest, destCapacity, michael@0: pErrorCode); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uloc_getDisplayLanguage(const char *locale, michael@0: const char *displayLocale, michael@0: UChar *dest, int32_t destCapacity, michael@0: UErrorCode *pErrorCode) { michael@0: return _getDisplayNameForComponent(locale, displayLocale, dest, destCapacity, michael@0: uloc_getLanguage, _kLanguages, pErrorCode); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uloc_getDisplayScript(const char* locale, michael@0: const char* displayLocale, michael@0: UChar *dest, int32_t destCapacity, michael@0: UErrorCode *pErrorCode) michael@0: { michael@0: UErrorCode err = U_ZERO_ERROR; michael@0: int32_t res = _getDisplayNameForComponent(locale, displayLocale, dest, destCapacity, michael@0: uloc_getScript, _kScriptsStandAlone, &err); michael@0: michael@0: if ( err == U_USING_DEFAULT_WARNING ) { michael@0: return _getDisplayNameForComponent(locale, displayLocale, dest, destCapacity, michael@0: uloc_getScript, _kScripts, pErrorCode); michael@0: } else { michael@0: *pErrorCode = err; michael@0: return res; michael@0: } michael@0: } michael@0: michael@0: U_INTERNAL int32_t U_EXPORT2 michael@0: uloc_getDisplayScriptInContext(const char* locale, michael@0: const char* displayLocale, michael@0: UChar *dest, int32_t destCapacity, michael@0: UErrorCode *pErrorCode) michael@0: { michael@0: return _getDisplayNameForComponent(locale, displayLocale, dest, destCapacity, michael@0: uloc_getScript, _kScripts, pErrorCode); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uloc_getDisplayCountry(const char *locale, michael@0: const char *displayLocale, michael@0: UChar *dest, int32_t destCapacity, michael@0: UErrorCode *pErrorCode) { michael@0: return _getDisplayNameForComponent(locale, displayLocale, dest, destCapacity, michael@0: uloc_getCountry, _kCountries, pErrorCode); michael@0: } michael@0: michael@0: /* michael@0: * TODO separate variant1_variant2_variant3... michael@0: * by getting each tag's display string and concatenating them with ", " michael@0: * in between - similar to uloc_getDisplayName() michael@0: */ michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uloc_getDisplayVariant(const char *locale, michael@0: const char *displayLocale, michael@0: UChar *dest, int32_t destCapacity, michael@0: UErrorCode *pErrorCode) { michael@0: return _getDisplayNameForComponent(locale, displayLocale, dest, destCapacity, michael@0: uloc_getVariant, _kVariants, pErrorCode); michael@0: } michael@0: michael@0: /* Instead of having a separate pass for 'special' patterns, reintegrate the two michael@0: * so we don't get bitten by preflight bugs again. We can be reasonably efficient michael@0: * without two separate code paths, this code isn't that performance-critical. michael@0: * michael@0: * This code is general enough to deal with patterns that have a prefix or swap the michael@0: * language and remainder components, since we gave developers enough rope to do such michael@0: * things if they futz with the pattern data. But since we don't give them a way to michael@0: * specify a pattern for arbitrary combinations of components, there's not much use in michael@0: * that. I don't think our data includes such patterns, the only variable I know if is michael@0: * whether there is a space before the open paren, or not. Oh, and zh uses different michael@0: * chars than the standard open/close paren (which ja and ko use, btw). michael@0: */ michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uloc_getDisplayName(const char *locale, michael@0: const char *displayLocale, michael@0: UChar *dest, int32_t destCapacity, michael@0: UErrorCode *pErrorCode) michael@0: { michael@0: static const UChar defaultSeparator[9] = { 0x007b, 0x0030, 0x007d, 0x002c, 0x0020, 0x007b, 0x0031, 0x007d, 0x0000 }; /* "{0}, {1}" */ michael@0: static const UChar sub0[4] = { 0x007b, 0x0030, 0x007d , 0x0000 } ; /* {0} */ michael@0: static const UChar sub1[4] = { 0x007b, 0x0031, 0x007d , 0x0000 } ; /* {1} */ michael@0: static const int32_t subLen = 3; michael@0: static const UChar defaultPattern[10] = { michael@0: 0x007b, 0x0030, 0x007d, 0x0020, 0x0028, 0x007b, 0x0031, 0x007d, 0x0029, 0x0000 michael@0: }; /* {0} ({1}) */ michael@0: static const int32_t defaultPatLen = 9; michael@0: static const int32_t defaultSub0Pos = 0; michael@0: static const int32_t defaultSub1Pos = 5; michael@0: michael@0: int32_t length; /* of formatted result */ michael@0: michael@0: const UChar *separator; michael@0: int32_t sepLen = 0; michael@0: const UChar *pattern; michael@0: int32_t patLen = 0; michael@0: int32_t sub0Pos, sub1Pos; michael@0: michael@0: UChar formatOpenParen = 0x0028; // ( michael@0: UChar formatReplaceOpenParen = 0x005B; // [ michael@0: UChar formatCloseParen = 0x0029; // ) michael@0: UChar formatReplaceCloseParen = 0x005D; // ] michael@0: michael@0: UBool haveLang = TRUE; /* assume true, set false if we find we don't have michael@0: a lang component in the locale */ michael@0: UBool haveRest = TRUE; /* assume true, set false if we find we don't have michael@0: any other component in the locale */ michael@0: UBool retry = FALSE; /* set true if we need to retry, see below */ michael@0: michael@0: int32_t langi = 0; /* index of the language substitution (0 or 1), virtually always 0 */ michael@0: michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: michael@0: if(destCapacity<0 || (destCapacity>0 && dest==NULL)) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: UResourceBundle* locbundle=ures_open(U_ICUDATA_LANG, displayLocale, &status); michael@0: UResourceBundle* dspbundle=ures_getByKeyWithFallback(locbundle, _kLocaleDisplayPattern, michael@0: NULL, &status); michael@0: michael@0: separator=ures_getStringByKeyWithFallback(dspbundle, _kSeparator, &sepLen, &status); michael@0: pattern=ures_getStringByKeyWithFallback(dspbundle, _kPattern, &patLen, &status); michael@0: michael@0: ures_close(dspbundle); michael@0: ures_close(locbundle); michael@0: } michael@0: michael@0: /* If we couldn't find any data, then use the defaults */ michael@0: if(sepLen == 0) { michael@0: separator = defaultSeparator; michael@0: } michael@0: /* #10244: Even though separator is now a pattern, it is awkward to handle it as such michael@0: * here since we are trying to build the display string in place in the dest buffer, michael@0: * and to handle it as a pattern would entail having separate storage for the michael@0: * substrings that need to be combined (the first of which may be the result of michael@0: * previous such combinations). So for now we continue to treat the portion between michael@0: * {0} and {1} as a string to be appended when joining substrings, ignoring anything michael@0: * that is before {0} or after {1} (no existing separator pattern has any such thing). michael@0: * This is similar to how pattern is handled below. michael@0: */ michael@0: { michael@0: UChar *p0=u_strstr(separator, sub0); michael@0: UChar *p1=u_strstr(separator, sub1); michael@0: if (p0==NULL || p1==NULL || p1= sub0Pos) { michael@0: while (patPos < sub0Pos) { michael@0: *p++ = pattern[patPos++]; michael@0: } michael@0: } else { michael@0: patPos=sub0Pos; michael@0: } michael@0: length=sub0Pos; michael@0: } else { michael@0: length=0; michael@0: } michael@0: michael@0: for(int32_t subi=0,resti=0;subi<2;) { /* iterate through patterns 0 and 1*/ michael@0: UBool subdone = FALSE; /* set true when ready to move to next substitution */ michael@0: michael@0: /* prep p and cap for calls to get display components, pin cap to 0 since michael@0: they complain if cap is negative */ michael@0: int32_t cap=destCapacity-length; michael@0: if (cap <= 0) { michael@0: cap=0; michael@0: } else { michael@0: p=dest+length; michael@0: } michael@0: michael@0: if (subi == langi) { /* {0}*/ michael@0: if(haveLang) { michael@0: langPos=length; michael@0: langLen=uloc_getDisplayLanguage(locale, displayLocale, p, cap, pErrorCode); michael@0: length+=langLen; michael@0: haveLang=langLen>0; michael@0: } michael@0: subdone=TRUE; michael@0: } else { /* {1} */ michael@0: if(!haveRest) { michael@0: subdone=TRUE; michael@0: } else { michael@0: int32_t len; /* length of component (plus other stuff) we just fetched */ michael@0: switch(resti++) { michael@0: case 0: michael@0: restPos=length; michael@0: len=uloc_getDisplayScriptInContext(locale, displayLocale, p, cap, pErrorCode); michael@0: break; michael@0: case 1: michael@0: len=uloc_getDisplayCountry(locale, displayLocale, p, cap, pErrorCode); michael@0: break; michael@0: case 2: michael@0: len=uloc_getDisplayVariant(locale, displayLocale, p, cap, pErrorCode); michael@0: break; michael@0: case 3: michael@0: kenum = uloc_openKeywords(locale, pErrorCode); michael@0: /* fall through */ michael@0: default: { michael@0: const char* kw=uenum_next(kenum, &len, pErrorCode); michael@0: if (kw == NULL) { michael@0: uenum_close(kenum); michael@0: len=0; /* mark that we didn't add a component */ michael@0: subdone=TRUE; michael@0: } else { michael@0: /* incorporating this behavior into the loop made it even more complex, michael@0: so just special case it here */ michael@0: len = uloc_getDisplayKeyword(kw, displayLocale, p, cap, pErrorCode); michael@0: if(len) { michael@0: if(len < cap) { michael@0: p[len]=0x3d; /* '=', assume we'll need it */ michael@0: } michael@0: len+=1; michael@0: michael@0: /* adjust for call to get keyword */ michael@0: cap-=len; michael@0: if(cap <= 0) { michael@0: cap=0; michael@0: } else { michael@0: p+=len; michael@0: } michael@0: } michael@0: /* reset for call below */ michael@0: if(*pErrorCode == U_BUFFER_OVERFLOW_ERROR) { michael@0: *pErrorCode=U_ZERO_ERROR; michael@0: } michael@0: int32_t vlen = uloc_getDisplayKeywordValue(locale, kw, displayLocale, michael@0: p, cap, pErrorCode); michael@0: if(len) { michael@0: if(vlen==0) { michael@0: --len; /* remove unneeded '=' */ michael@0: } michael@0: /* restore cap and p to what they were at start */ michael@0: cap=destCapacity-length; michael@0: if(cap <= 0) { michael@0: cap=0; michael@0: } else { michael@0: p=dest+length; michael@0: } michael@0: } michael@0: len+=vlen; /* total we added for key + '=' + value */ michael@0: } michael@0: } break; michael@0: } /* end switch */ michael@0: michael@0: if (len>0) { michael@0: /* we addeed a component, so add separator and write it if there's room. */ michael@0: if(len+sepLen<=cap) { michael@0: const UChar * plimit = p + len; michael@0: for (; p < plimit; p++) { michael@0: if (*p == formatOpenParen) { michael@0: *p = formatReplaceOpenParen; michael@0: } else if (*p == formatCloseParen) { michael@0: *p = formatReplaceCloseParen; michael@0: } michael@0: } michael@0: for(int32_t i=0;i0; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if(*pErrorCode == U_BUFFER_OVERFLOW_ERROR) { michael@0: *pErrorCode=U_ZERO_ERROR; michael@0: } michael@0: michael@0: if(subdone) { michael@0: if(haveLang && haveRest) { michael@0: /* append internal portion of pattern, the first time, michael@0: or last portion of pattern the second time */ michael@0: int32_t padLen; michael@0: patPos+=subLen; michael@0: padLen=(subi==0 ? sub1Pos : patLen)-patPos; michael@0: if(length+padLen < destCapacity) { michael@0: p=dest+length; michael@0: for(int32_t i=0;i0) { michael@0: /* true length is the length of just the component we got. */ michael@0: length=haveLang?langLen:restLen; michael@0: if(dest && sub0Pos!=0) { michael@0: if (sub0Pos+length<=destCapacity) { michael@0: /* first component not at start of result, michael@0: but we have full component in buffer. */ michael@0: u_memmove(dest, dest+(haveLang?langPos:restPos), length); michael@0: } else { michael@0: /* would have fit, but didn't because of pattern prefix. */ michael@0: sub0Pos=0; /* stops initial padding (and a second retry, michael@0: so we won't end up here again) */ michael@0: retry=TRUE; michael@0: } michael@0: } michael@0: } michael@0: michael@0: ++subi; /* move on to next substitution */ michael@0: } michael@0: } michael@0: } while(retry); michael@0: michael@0: return u_terminateUChars(dest, destCapacity, length, pErrorCode); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uloc_getDisplayKeyword(const char* keyword, michael@0: const char* displayLocale, michael@0: UChar* dest, michael@0: int32_t destCapacity, michael@0: UErrorCode* status){ michael@0: michael@0: /* argument checking */ michael@0: if(status==NULL || U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: michael@0: if(destCapacity<0 || (destCapacity>0 && dest==NULL)) { michael@0: *status=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: michael@0: /* pass itemKey=NULL to look for a top-level item */ michael@0: return _getStringOrCopyKey(U_ICUDATA_LANG, displayLocale, michael@0: _kKeys, NULL, michael@0: keyword, michael@0: keyword, michael@0: dest, destCapacity, michael@0: status); michael@0: michael@0: } michael@0: michael@0: michael@0: #define UCURRENCY_DISPLAY_NAME_INDEX 1 michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uloc_getDisplayKeywordValue( const char* locale, michael@0: const char* keyword, michael@0: const char* displayLocale, michael@0: UChar* dest, michael@0: int32_t destCapacity, michael@0: UErrorCode* status){ michael@0: michael@0: michael@0: char keywordValue[ULOC_FULLNAME_CAPACITY*4]; michael@0: int32_t capacity = ULOC_FULLNAME_CAPACITY*4; michael@0: int32_t keywordValueLen =0; michael@0: michael@0: /* argument checking */ michael@0: if(status==NULL || U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: michael@0: if(destCapacity<0 || (destCapacity>0 && dest==NULL)) { michael@0: *status=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: /* get the keyword value */ michael@0: keywordValue[0]=0; michael@0: keywordValueLen = uloc_getKeywordValue(locale, keyword, keywordValue, capacity, status); michael@0: michael@0: /* michael@0: * if the keyword is equal to currency .. then to get the display name michael@0: * we need to do the fallback ourselves michael@0: */ michael@0: if(uprv_stricmp(keyword, _kCurrency)==0){ michael@0: michael@0: int32_t dispNameLen = 0; michael@0: const UChar *dispName = NULL; michael@0: michael@0: UResourceBundle *bundle = ures_open(U_ICUDATA_CURR, displayLocale, status); michael@0: UResourceBundle *currencies = ures_getByKey(bundle, _kCurrencies, NULL, status); michael@0: UResourceBundle *currency = ures_getByKeyWithFallback(currencies, keywordValue, NULL, status); michael@0: michael@0: dispName = ures_getStringByIndex(currency, UCURRENCY_DISPLAY_NAME_INDEX, &dispNameLen, status); michael@0: michael@0: /*close the bundles */ michael@0: ures_close(currency); michael@0: ures_close(currencies); michael@0: ures_close(bundle); michael@0: michael@0: if(U_FAILURE(*status)){ michael@0: if(*status == U_MISSING_RESOURCE_ERROR){ michael@0: /* we just want to write the value over if nothing is available */ michael@0: *status = U_USING_DEFAULT_WARNING; michael@0: }else{ michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: /* now copy the dispName over if not NULL */ michael@0: if(dispName != NULL){ michael@0: if(dispNameLen <= destCapacity){ michael@0: uprv_memcpy(dest, dispName, dispNameLen * U_SIZEOF_UCHAR); michael@0: return u_terminateUChars(dest, destCapacity, dispNameLen, status); michael@0: }else{ michael@0: *status = U_BUFFER_OVERFLOW_ERROR; michael@0: return dispNameLen; michael@0: } michael@0: }else{ michael@0: /* we have not found the display name for the value .. just copy over */ michael@0: if(keywordValueLen <= destCapacity){ michael@0: u_charsToUChars(keywordValue, dest, keywordValueLen); michael@0: return u_terminateUChars(dest, destCapacity, keywordValueLen, status); michael@0: }else{ michael@0: *status = U_BUFFER_OVERFLOW_ERROR; michael@0: return keywordValueLen; michael@0: } michael@0: } michael@0: michael@0: michael@0: }else{ michael@0: michael@0: return _getStringOrCopyKey(U_ICUDATA_LANG, displayLocale, michael@0: _kTypes, keyword, michael@0: keywordValue, michael@0: keywordValue, michael@0: dest, destCapacity, michael@0: status); michael@0: } michael@0: }