2 *******************************************************************************
4 * Copyright (C) 1997-2010, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: loclikely.cpp
10 * tab size: 8 (not used)
13 * created on: 2010feb25
14 * created by: Markus W. Scherer
16 * Code for miscellaneous locale-related resource bundle data access,
17 * separated out from other .cpp files
18 * that then do not depend on resource bundle code and this data.
21 #include "unicode/utypes.h"
22 #include "unicode/putil.h"
23 #include "unicode/uloc.h"
24 #include "unicode/ures.h"
30 * Lookup a resource bundle table item with fallback on the table level.
31 * Regular resource bundle lookups perform fallback to parent locale bundles
32 * and eventually the root bundle, but only for top-level items.
33 * This function takes the name of a top-level table and of an item in that table
34 * and performs a lookup of both, falling back until a bundle contains a table
37 * Note: Only the opening of entire bundles falls back through the default locale
38 * before root. Once a bundle is open, item lookups do not go through the
39 * default locale because that would result in a mix of languages that is
40 * unpredictable to the programmer and most likely useless.
42 U_CAPI
const UChar
* U_EXPORT2
43 uloc_getTableStringWithFallback(const char *path
, const char *locale
,
44 const char *tableKey
, const char *subTableKey
,
47 UErrorCode
*pErrorCode
)
49 /* char localeBuffer[ULOC_FULLNAME_CAPACITY*4];*/
50 UResourceBundle
*rb
=NULL
, table
, subTable
;
51 const UChar
*item
=NULL
;
53 char explicitFallbackName
[ULOC_FULLNAME_CAPACITY
] = {0};
56 * open the bundle for the current locale
57 * this falls back through the locale's chain to root
59 errorCode
=U_ZERO_ERROR
;
60 rb
=ures_open(path
, locale
, &errorCode
);
62 if(U_FAILURE(errorCode
)) {
63 /* total failure, not even root could be opened */
64 *pErrorCode
=errorCode
;
66 } else if(errorCode
==U_USING_DEFAULT_WARNING
||
67 (errorCode
==U_USING_FALLBACK_WARNING
&& *pErrorCode
!=U_USING_DEFAULT_WARNING
)
69 /* set the "strongest" error code (success->fallback->default->failure) */
70 *pErrorCode
=errorCode
;
74 ures_initStackObject(&table
);
75 ures_initStackObject(&subTable
);
76 ures_getByKeyWithFallback(rb
, tableKey
, &table
, &errorCode
);
78 if (subTableKey
!= NULL
) {
80 ures_getByKeyWithFallback(&table,subTableKey, &subTable, &errorCode);
81 item = ures_getStringByKeyWithFallback(&subTable, itemKey, pLength, &errorCode);
82 if(U_FAILURE(errorCode)){
83 *pErrorCode = errorCode;
88 ures_getByKeyWithFallback(&table
,subTableKey
, &table
, &errorCode
);
90 if(U_SUCCESS(errorCode
)){
91 item
= ures_getStringByKeyWithFallback(&table
, itemKey
, pLength
, &errorCode
);
92 if(U_FAILURE(errorCode
)){
93 const char* replacement
= NULL
;
94 *pErrorCode
= errorCode
; /*save the errorCode*/
95 errorCode
= U_ZERO_ERROR
;
96 /* may be a deprecated code */
97 if(uprv_strcmp(tableKey
, "Countries")==0){
98 replacement
= uloc_getCurrentCountryID(itemKey
);
99 }else if(uprv_strcmp(tableKey
, "Languages")==0){
100 replacement
= uloc_getCurrentLanguageID(itemKey
);
102 /*pointer comparison is ok since uloc_getCurrentCountryID & uloc_getCurrentLanguageID return the key itself is replacement is not found*/
103 if(replacement
!=NULL
&& itemKey
!= replacement
){
104 item
= ures_getStringByKeyWithFallback(&table
, replacement
, pLength
, &errorCode
);
105 if(U_SUCCESS(errorCode
)){
106 *pErrorCode
= errorCode
;
115 if(U_FAILURE(errorCode
)){
117 /* still can't figure out ?.. try the fallback mechanism */
119 const UChar
* fallbackLocale
= NULL
;
120 *pErrorCode
= errorCode
;
121 errorCode
= U_ZERO_ERROR
;
123 fallbackLocale
= ures_getStringByKeyWithFallback(&table
, "Fallback", &len
, &errorCode
);
124 if(U_FAILURE(errorCode
)){
125 *pErrorCode
= errorCode
;
129 u_UCharsToChars(fallbackLocale
, explicitFallbackName
, len
);
131 /* guard against recursive fallback */
132 if(uprv_strcmp(explicitFallbackName
, locale
)==0){
133 *pErrorCode
= U_INTERNAL_PROGRAM_ERROR
;
137 rb
= ures_open(path
, explicitFallbackName
, &errorCode
);
138 if(U_FAILURE(errorCode
)){
139 *pErrorCode
= errorCode
;
142 /* succeeded in opening the fallback bundle .. continue and try to fetch the item */
147 /* done with the locale string - ready to close table and rb */
148 ures_close(&subTable
);
155 _uloc_getOrientationHelper(const char* localeId
,
159 ULayoutType result
= ULOC_LAYOUT_UNKNOWN
;
161 if (!U_FAILURE(*status
)) {
163 char localeBuffer
[ULOC_FULLNAME_CAPACITY
];
165 uloc_canonicalize(localeId
, localeBuffer
, sizeof(localeBuffer
), status
);
167 if (!U_FAILURE(*status
)) {
168 const UChar
* const value
=
169 uloc_getTableStringWithFallback(
178 if (!U_FAILURE(*status
) && length
!= 0) {
181 case 0x0062: /* 'b' */
182 result
= ULOC_LAYOUT_BTT
;
184 case 0x006C: /* 'l' */
185 result
= ULOC_LAYOUT_LTR
;
187 case 0x0072: /* 'r' */
188 result
= ULOC_LAYOUT_RTL
;
190 case 0x0074: /* 't' */
191 result
= ULOC_LAYOUT_TTB
;
194 *status
= U_INTERNAL_PROGRAM_ERROR
;
204 U_DRAFT ULayoutType U_EXPORT2
205 uloc_getCharacterOrientation(const char* localeId
,
208 return _uloc_getOrientationHelper(localeId
, "characters", status
);
212 * Get the layout line orientation for the specified locale.
214 * @param localeID locale name
215 * @param status Error status
216 * @return an enum indicating the layout orientation for lines.
219 U_DRAFT ULayoutType U_EXPORT2
220 uloc_getLineOrientation(const char* localeId
,
223 return _uloc_getOrientationHelper(localeId
, "lines", status
);