1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 1997-2012, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
10 * file name: loclikely.cpp
12 * tab size: 8 (not used)
15 * created on: 2010feb25
16 * created by: Markus W. Scherer
18 * Code for miscellaneous locale-related resource bundle data access,
19 * separated out from other .cpp files
20 * that then do not depend on resource bundle code and this data.
23 #include "unicode/utypes.h"
24 #include "unicode/putil.h"
25 #include "unicode/uloc.h"
26 #include "unicode/ures.h"
32 * Lookup a resource bundle table item with fallback on the table level.
33 * Regular resource bundle lookups perform fallback to parent locale bundles
34 * and eventually the root bundle, but only for top-level items.
35 * This function takes the name of a top-level table and of an item in that table
36 * and performs a lookup of both, falling back until a bundle contains a table
39 * Note: Only the opening of entire bundles falls back through the default locale
40 * before root. Once a bundle is open, item lookups do not go through the
41 * default locale because that would result in a mix of languages that is
42 * unpredictable to the programmer and most likely useless.
44 U_CAPI
const UChar
* U_EXPORT2
45 uloc_getTableStringWithFallback(const char *path
, const char *locale
,
46 const char *tableKey
, const char *subTableKey
,
49 UErrorCode
*pErrorCode
)
51 /* char localeBuffer[ULOC_FULLNAME_CAPACITY*4];*/
52 UResourceBundle
*rb
=NULL
, table
, subTable
;
53 const UChar
*item
=NULL
;
55 char explicitFallbackName
[ULOC_FULLNAME_CAPACITY
] = {0};
58 * open the bundle for the current locale
59 * this falls back through the locale's chain to root
61 errorCode
=U_ZERO_ERROR
;
62 rb
=ures_open(path
, locale
, &errorCode
);
64 if(U_FAILURE(errorCode
)) {
65 /* total failure, not even root could be opened */
66 *pErrorCode
=errorCode
;
68 } else if(errorCode
==U_USING_DEFAULT_WARNING
||
69 (errorCode
==U_USING_FALLBACK_WARNING
&& *pErrorCode
!=U_USING_DEFAULT_WARNING
)
71 /* set the "strongest" error code (success->fallback->default->failure) */
72 *pErrorCode
=errorCode
;
76 ures_initStackObject(&table
);
77 ures_initStackObject(&subTable
);
78 ures_getByKeyWithFallback(rb
, tableKey
, &table
, &errorCode
);
80 if (subTableKey
!= NULL
) {
82 ures_getByKeyWithFallback(&table,subTableKey, &subTable, &errorCode);
83 item = ures_getStringByKeyWithFallback(&subTable, itemKey, pLength, &errorCode);
84 if(U_FAILURE(errorCode)){
85 *pErrorCode = errorCode;
90 ures_getByKeyWithFallback(&table
,subTableKey
, &table
, &errorCode
);
92 if(U_SUCCESS(errorCode
)){
93 item
= ures_getStringByKeyWithFallback(&table
, itemKey
, pLength
, &errorCode
);
94 if(U_FAILURE(errorCode
)){
95 const char* replacement
= NULL
;
96 *pErrorCode
= errorCode
; /*save the errorCode*/
97 errorCode
= U_ZERO_ERROR
;
98 /* may be a deprecated code */
99 if(uprv_strcmp(tableKey
, "Countries")==0){
100 replacement
= uloc_getCurrentCountryID(itemKey
);
101 }else if(uprv_strcmp(tableKey
, "Languages")==0){
102 replacement
= uloc_getCurrentLanguageID(itemKey
);
104 /*pointer comparison is ok since uloc_getCurrentCountryID & uloc_getCurrentLanguageID return the key itself is replacement is not found*/
105 if(replacement
!=NULL
&& itemKey
!= replacement
){
106 item
= ures_getStringByKeyWithFallback(&table
, replacement
, pLength
, &errorCode
);
107 if(U_SUCCESS(errorCode
)){
108 *pErrorCode
= errorCode
;
117 if(U_FAILURE(errorCode
)){
119 /* still can't figure out ?.. try the fallback mechanism */
121 const UChar
* fallbackLocale
= NULL
;
122 *pErrorCode
= errorCode
;
123 errorCode
= U_ZERO_ERROR
;
125 fallbackLocale
= ures_getStringByKeyWithFallback(&table
, "Fallback", &len
, &errorCode
);
126 if(U_FAILURE(errorCode
)){
127 *pErrorCode
= errorCode
;
131 u_UCharsToChars(fallbackLocale
, explicitFallbackName
, len
);
133 /* guard against recursive fallback */
134 if(uprv_strcmp(explicitFallbackName
, locale
)==0){
135 *pErrorCode
= U_INTERNAL_PROGRAM_ERROR
;
139 rb
= ures_open(path
, explicitFallbackName
, &errorCode
);
140 if(U_FAILURE(errorCode
)){
141 *pErrorCode
= errorCode
;
144 /* succeeded in opening the fallback bundle .. continue and try to fetch the item */
149 /* done with the locale string - ready to close table and rb */
150 ures_close(&subTable
);
157 _uloc_getOrientationHelper(const char* localeId
,
161 ULayoutType result
= ULOC_LAYOUT_UNKNOWN
;
163 if (!U_FAILURE(*status
)) {
165 char localeBuffer
[ULOC_FULLNAME_CAPACITY
];
167 uloc_canonicalize(localeId
, localeBuffer
, sizeof(localeBuffer
), status
);
169 if (!U_FAILURE(*status
)) {
170 const UChar
* const value
=
171 uloc_getTableStringWithFallback(
180 if (!U_FAILURE(*status
) && length
!= 0) {
183 case 0x0062: /* 'b' */
184 result
= ULOC_LAYOUT_BTT
;
186 case 0x006C: /* 'l' */
187 result
= ULOC_LAYOUT_LTR
;
189 case 0x0072: /* 'r' */
190 result
= ULOC_LAYOUT_RTL
;
192 case 0x0074: /* 't' */
193 result
= ULOC_LAYOUT_TTB
;
196 *status
= U_INTERNAL_PROGRAM_ERROR
;
206 U_CAPI ULayoutType U_EXPORT2
207 uloc_getCharacterOrientation(const char* localeId
,
210 return _uloc_getOrientationHelper(localeId
, "characters", status
);
214 * Get the layout line orientation for the specified locale.
216 * @param localeID locale name
217 * @param status Error status
218 * @return an enum indicating the layout orientation for lines.
220 U_CAPI ULayoutType U_EXPORT2
221 uloc_getLineOrientation(const char* localeId
,
224 return _uloc_getOrientationHelper(localeId
, "lines", status
);