michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 1997-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: loclikely.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 miscellaneous locale-related resource bundle data access, michael@0: * separated out from other .cpp files michael@0: * that then do not depend on resource bundle code and this data. michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/putil.h" michael@0: #include "unicode/uloc.h" michael@0: #include "unicode/ures.h" michael@0: #include "cstring.h" michael@0: #include "ulocimp.h" michael@0: #include "uresimp.h" michael@0: michael@0: /* michael@0: * Lookup a resource bundle table item with fallback on the table level. michael@0: * Regular resource bundle lookups perform fallback to parent locale bundles michael@0: * and eventually the root bundle, but only for top-level items. michael@0: * This function takes the name of a top-level table and of an item in that table michael@0: * and performs a lookup of both, falling back until a bundle contains a table michael@0: * with this item. michael@0: * michael@0: * Note: Only the opening of entire bundles falls back through the default locale michael@0: * before root. Once a bundle is open, item lookups do not go through the michael@0: * default locale because that would result in a mix of languages that is michael@0: * unpredictable to the programmer and most likely useless. michael@0: */ michael@0: U_CAPI const UChar * U_EXPORT2 michael@0: uloc_getTableStringWithFallback(const char *path, const char *locale, michael@0: const char *tableKey, const char *subTableKey, michael@0: const char *itemKey, michael@0: int32_t *pLength, michael@0: UErrorCode *pErrorCode) michael@0: { michael@0: /* char localeBuffer[ULOC_FULLNAME_CAPACITY*4];*/ michael@0: UResourceBundle *rb=NULL, table, subTable; michael@0: const UChar *item=NULL; michael@0: UErrorCode errorCode; michael@0: char explicitFallbackName[ULOC_FULLNAME_CAPACITY] = {0}; michael@0: michael@0: /* michael@0: * open the bundle for the current locale michael@0: * this falls back through the locale's chain to root michael@0: */ michael@0: errorCode=U_ZERO_ERROR; michael@0: rb=ures_open(path, locale, &errorCode); michael@0: michael@0: if(U_FAILURE(errorCode)) { michael@0: /* total failure, not even root could be opened */ michael@0: *pErrorCode=errorCode; michael@0: return NULL; michael@0: } else if(errorCode==U_USING_DEFAULT_WARNING || michael@0: (errorCode==U_USING_FALLBACK_WARNING && *pErrorCode!=U_USING_DEFAULT_WARNING) michael@0: ) { michael@0: /* set the "strongest" error code (success->fallback->default->failure) */ michael@0: *pErrorCode=errorCode; michael@0: } michael@0: michael@0: for(;;){ michael@0: ures_initStackObject(&table); michael@0: ures_initStackObject(&subTable); michael@0: ures_getByKeyWithFallback(rb, tableKey, &table, &errorCode); michael@0: michael@0: if (subTableKey != NULL) { michael@0: /* michael@0: ures_getByKeyWithFallback(&table,subTableKey, &subTable, &errorCode); michael@0: item = ures_getStringByKeyWithFallback(&subTable, itemKey, pLength, &errorCode); michael@0: if(U_FAILURE(errorCode)){ michael@0: *pErrorCode = errorCode; michael@0: } michael@0: michael@0: break;*/ michael@0: michael@0: ures_getByKeyWithFallback(&table,subTableKey, &table, &errorCode); michael@0: } michael@0: if(U_SUCCESS(errorCode)){ michael@0: item = ures_getStringByKeyWithFallback(&table, itemKey, pLength, &errorCode); michael@0: if(U_FAILURE(errorCode)){ michael@0: const char* replacement = NULL; michael@0: *pErrorCode = errorCode; /*save the errorCode*/ michael@0: errorCode = U_ZERO_ERROR; michael@0: /* may be a deprecated code */ michael@0: if(uprv_strcmp(tableKey, "Countries")==0){ michael@0: replacement = uloc_getCurrentCountryID(itemKey); michael@0: }else if(uprv_strcmp(tableKey, "Languages")==0){ michael@0: replacement = uloc_getCurrentLanguageID(itemKey); michael@0: } michael@0: /*pointer comparison is ok since uloc_getCurrentCountryID & uloc_getCurrentLanguageID return the key itself is replacement is not found*/ michael@0: if(replacement!=NULL && itemKey != replacement){ michael@0: item = ures_getStringByKeyWithFallback(&table, replacement, pLength, &errorCode); michael@0: if(U_SUCCESS(errorCode)){ michael@0: *pErrorCode = errorCode; michael@0: break; michael@0: } michael@0: } michael@0: }else{ michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if(U_FAILURE(errorCode)){ michael@0: michael@0: /* still can't figure out ?.. try the fallback mechanism */ michael@0: int32_t len = 0; michael@0: const UChar* fallbackLocale = NULL; michael@0: *pErrorCode = errorCode; michael@0: errorCode = U_ZERO_ERROR; michael@0: michael@0: fallbackLocale = ures_getStringByKeyWithFallback(&table, "Fallback", &len, &errorCode); michael@0: if(U_FAILURE(errorCode)){ michael@0: *pErrorCode = errorCode; michael@0: break; michael@0: } michael@0: michael@0: u_UCharsToChars(fallbackLocale, explicitFallbackName, len); michael@0: michael@0: /* guard against recursive fallback */ michael@0: if(uprv_strcmp(explicitFallbackName, locale)==0){ michael@0: *pErrorCode = U_INTERNAL_PROGRAM_ERROR; michael@0: break; michael@0: } michael@0: ures_close(rb); michael@0: rb = ures_open(path, explicitFallbackName, &errorCode); michael@0: if(U_FAILURE(errorCode)){ michael@0: *pErrorCode = errorCode; michael@0: break; michael@0: } michael@0: /* succeeded in opening the fallback bundle .. continue and try to fetch the item */ michael@0: }else{ michael@0: break; michael@0: } michael@0: } michael@0: /* done with the locale string - ready to close table and rb */ michael@0: ures_close(&subTable); michael@0: ures_close(&table); michael@0: ures_close(rb); michael@0: return item; michael@0: } michael@0: michael@0: static ULayoutType michael@0: _uloc_getOrientationHelper(const char* localeId, michael@0: const char* key, michael@0: UErrorCode *status) michael@0: { michael@0: ULayoutType result = ULOC_LAYOUT_UNKNOWN; michael@0: michael@0: if (!U_FAILURE(*status)) { michael@0: int32_t length = 0; michael@0: char localeBuffer[ULOC_FULLNAME_CAPACITY]; michael@0: michael@0: uloc_canonicalize(localeId, localeBuffer, sizeof(localeBuffer), status); michael@0: michael@0: if (!U_FAILURE(*status)) { michael@0: const UChar* const value = michael@0: uloc_getTableStringWithFallback( michael@0: NULL, michael@0: localeBuffer, michael@0: "layout", michael@0: NULL, michael@0: key, michael@0: &length, michael@0: status); michael@0: michael@0: if (!U_FAILURE(*status) && length != 0) { michael@0: switch(value[0]) michael@0: { michael@0: case 0x0062: /* 'b' */ michael@0: result = ULOC_LAYOUT_BTT; michael@0: break; michael@0: case 0x006C: /* 'l' */ michael@0: result = ULOC_LAYOUT_LTR; michael@0: break; michael@0: case 0x0072: /* 'r' */ michael@0: result = ULOC_LAYOUT_RTL; michael@0: break; michael@0: case 0x0074: /* 't' */ michael@0: result = ULOC_LAYOUT_TTB; michael@0: break; michael@0: default: michael@0: *status = U_INTERNAL_PROGRAM_ERROR; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: U_CAPI ULayoutType U_EXPORT2 michael@0: uloc_getCharacterOrientation(const char* localeId, michael@0: UErrorCode *status) michael@0: { michael@0: return _uloc_getOrientationHelper(localeId, "characters", status); michael@0: } michael@0: michael@0: /** michael@0: * Get the layout line orientation for the specified locale. michael@0: * michael@0: * @param localeID locale name michael@0: * @param status Error status michael@0: * @return an enum indicating the layout orientation for lines. michael@0: */ michael@0: U_CAPI ULayoutType U_EXPORT2 michael@0: uloc_getLineOrientation(const char* localeId, michael@0: UErrorCode *status) michael@0: { michael@0: return _uloc_getOrientationHelper(localeId, "lines", status); michael@0: }