2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <unicode/uloc.h>
18 #include <utils_i18n_ulocale.h>
19 #include <utils_i18n_private.h>
21 int i18n_ulocale_get_default (const char **locale)
23 retv_if (locale == NULL, I18N_ERROR_INVALID_PARAMETER);
25 *locale = uloc_getDefault();
27 return I18N_ERROR_NONE;
30 int i18n_ulocale_set_default (const char *locale_id)
32 UErrorCode icu_error = U_ZERO_ERROR;
33 uloc_setDefault(locale_id, &icu_error);
35 return _i18n_error_mapping(icu_error);
38 int i18n_ulocale_get_language (const char *locale_id, char *language, int32_t language_capacity, int32_t *buf_size_language)
40 retv_if(buf_size_language == NULL || language == NULL, I18N_ERROR_INVALID_PARAMETER);
42 UErrorCode icu_error = U_ZERO_ERROR;
43 *buf_size_language = uloc_getLanguage(locale_id, language, language_capacity, &icu_error);
45 return _i18n_error_mapping(icu_error);
48 int32_t i18n_ulocale_get_country (const char *locale_id, char *country, int32_t country_capacity, int *error)
52 *error = I18N_ERROR_INVALID_PARAMETER;
57 UErrorCode icu_error = U_ZERO_ERROR;
58 int32_t result = uloc_getCountry(locale_id, country, country_capacity, &icu_error);
60 *error = _i18n_error_mapping(icu_error);
66 int i18n_ulocale_get_display_name (const char *locale_id, const char *in_locale_id, i18n_uchar *result_w, int32_t max_result_size, int32_t *buf_size_display_name)
68 retv_if(buf_size_display_name == NULL, I18N_ERROR_INVALID_PARAMETER);
70 UErrorCode icu_error = U_ZERO_ERROR;
71 *buf_size_display_name = uloc_getDisplayName(locale_id, in_locale_id, result_w, max_result_size, &icu_error);
73 return _i18n_error_mapping(icu_error);
76 const char* i18n_ulocale_get_available (int32_t n)
79 set_last_result(I18N_ERROR_INVALID_PARAMETER);
82 set_last_result(I18N_ERROR_NONE);
83 return uloc_getAvailable(n);
86 int32_t i18n_ulocale_count_available (void)
88 set_last_result(I18N_ERROR_NONE);
89 return uloc_countAvailable();
94 int32_t i18n_ulocale_get_script (const char *locale_id, char *script, int32_t script_capacity)
96 if(NULL == script || script_capacity < 0){
97 set_last_result(I18N_ERROR_INVALID_PARAMETER);
101 UErrorCode icu_error = U_ZERO_ERROR;
102 int32_t needed_buffer_size = uloc_getScript(locale_id, script, script_capacity, &icu_error);
103 ERR("Error Code : %d", icu_error);
105 i18n_error_code_e i18n_error;
106 ERR_MAPPING(icu_error, i18n_error);
107 set_last_result(i18n_error);
108 return needed_buffer_size;
111 int32_t i18n_ulocale_get_variant (const char *locale_id, char *variant, int32_t variant_capacity)
113 if(NULL == variant || variant_capacity < 0){
114 set_last_result(I18N_ERROR_INVALID_PARAMETER);
118 UErrorCode icu_error = U_ZERO_ERROR;
119 int32_t needed_buffer_size = uloc_getVariant(locale_id, variant, variant_capacity, &icu_error);
120 ERR("Error Code : %d", icu_error);
122 i18n_error_code_e i18n_error;
123 ERR_MAPPING(icu_error, i18n_error);
124 set_last_result(i18n_error);
125 return needed_buffer_size;
128 int32_t i18n_ulocale_get_name (const char *locale_id, char *name, int32_t name_capacity)
130 UErrorCode icu_error = U_ZERO_ERROR;
131 int32_t needed_buffer_size = uloc_getName(locale_id, name, name_capacity, &icu_error);
132 ERR("Error Code : %d", icu_error);
134 i18n_error_code_e i18n_error;
135 ERR_MAPPING(icu_error, i18n_error);
136 set_last_result(i18n_error);
137 return needed_buffer_size;
140 int32_t i18n_ulocale_canonicalize (const char *locale_id, char *name, int32_t name_capacity)
142 UErrorCode icu_error = U_ZERO_ERROR;
143 int32_t needed_buffer_size = uloc_canonicalize(locale_id, name, name_capacity, &icu_error);
144 ERR("Error Code : %d", icu_error);
146 i18n_error_code_e i18n_error;
147 ERR_MAPPING(icu_error, i18n_error);
148 set_last_result(i18n_error);
149 return needed_buffer_size;
152 const char *i18n_ulocale_get_iso3_language (const char *locale_id)
154 set_last_result(I18N_ERROR_NONE);
155 return uloc_getISO3Language(locale_id);
158 const char *i18n_ulocale_get_iso3_country (const char *locale_id)
160 set_last_result(I18N_ERROR_NONE);
161 return uloc_getISO3Country(locale_id);
164 uint32_t i18n_ulocale_get_lcid (const char *locale_id)
166 set_last_result(I18N_ERROR_NONE);
167 return uloc_getLCID(locale_id);
170 int32_t i18n_ulocale_get_display_language (const char *locale, const char *display_locale, i18n_uchar *language,
171 int32_t language_capacity)
173 UErrorCode icu_error = U_ZERO_ERROR;
174 int32_t needed_buffer_size = uloc_getDisplayLanguage(locale, display_locale, language, language_capacity, &icu_error);
175 ERR("Error Code : %d", icu_error);
177 i18n_error_code_e i18n_error;
178 ERR_MAPPING(icu_error, i18n_error);
179 set_last_result(i18n_error);
180 return needed_buffer_size;
183 int32_t i18n_ulocale_get_display_script (const char *locale, const char *display_locale, i18n_uchar *script,
184 int32_t script_capacity)
186 UErrorCode icu_error = U_ZERO_ERROR;
187 int32_t needed_buffer_size = uloc_getDisplayScript(locale, display_locale, script, script_capacity, &icu_error);
188 ERR("Error Code : %d", icu_error);
190 i18n_error_code_e i18n_error;
191 ERR_MAPPING(icu_error, i18n_error);
192 set_last_result(i18n_error);
193 return needed_buffer_size;
196 int32_t i18n_ulocale_get_display_country (const char *locale, const char *display_locale, i18n_uchar *country,
197 int32_t country_capacity)
199 UErrorCode icu_error = U_ZERO_ERROR;
200 int32_t needed_buffer_size = uloc_getDisplayCountry(locale, display_locale, country, country_capacity, &icu_error);
201 ERR("Error Code : %d", icu_error);
203 i18n_error_code_e i18n_error;
204 ERR_MAPPING(icu_error, i18n_error);
205 set_last_result(i18n_error);
206 return needed_buffer_size;
209 int32_t i18n_ulocale_get_display_variant (const char *locale, const char *display_locale, i18n_uchar *variant,
210 int32_t variant_capacity)
212 UErrorCode icu_error = U_ZERO_ERROR;
213 int32_t needed_buffer_size = uloc_getDisplayVariant(locale, display_locale, variant, variant_capacity, &icu_error);
214 ERR("Error Code : %d", icu_error);
216 i18n_error_code_e i18n_error;
217 ERR_MAPPING(icu_error, i18n_error);
218 set_last_result(i18n_error);
219 return needed_buffer_size;
222 int32_t i18n_ulocale_get_display_keyword (const char *keyword, const char *display_locale, i18n_uchar *dest,
223 int32_t dest_capacity)
225 UErrorCode icu_error = U_ZERO_ERROR;
226 int32_t needed_buffer_size = uloc_getDisplayKeyword(keyword, display_locale, dest, dest_capacity, &icu_error);
227 ERR("Error Code : %d", icu_error);
229 i18n_error_code_e i18n_error;
230 ERR_MAPPING(icu_error, i18n_error);
231 set_last_result(i18n_error);
232 return needed_buffer_size;
235 int32_t i18n_ulocale_get_display_keyword_value (const char *locale, const char *keyword, const char *display_locale,
236 i18n_uchar *dest, int32_t dest_capacity)
238 UErrorCode icu_error = U_ZERO_ERROR;
239 int32_t needed_buffer_size = uloc_getDisplayKeywordValue(locale, keyword, display_locale, dest, dest_capacity, &icu_error);
240 ERR("Error Code : %d", icu_error);
242 i18n_error_code_e i18n_error;
243 ERR_MAPPING(icu_error, i18n_error);
244 set_last_result(i18n_error);
245 return needed_buffer_size;
248 const char * const *i18n_ulocale_get_iso_languages (void)
250 set_last_result(I18N_ERROR_NONE);
251 return uloc_getISOLanguages();
254 const char * const *i18n_ulocale_get_iso_countries (void)
256 set_last_result(I18N_ERROR_NONE);
257 return uloc_getISOCountries();
260 int32_t i18n_ulocale_get_parent (const char *locale_id, char *parent, int32_t parent_capacity)
262 if(NULL == parent || parent_capacity < 0){
263 set_last_result(I18N_ERROR_INVALID_PARAMETER);
267 UErrorCode icu_error = U_ZERO_ERROR;
268 int32_t len_of_loc = uloc_getParent(locale_id, parent, parent_capacity, &icu_error);
269 ERR("Error Code : %d", icu_error);
271 i18n_error_code_e i18n_error;
272 ERR_MAPPING(icu_error, i18n_error);
273 set_last_result(i18n_error);
277 int32_t i18n_ulocale_get_base_name (const char *locale_id, char *name, int32_t name_capacity)
279 UErrorCode icu_error = U_ZERO_ERROR;
280 int32_t needed_buffer_size = uloc_getBaseName(locale_id, name, name_capacity, &icu_error);
281 ERR("Error Code : %d", icu_error);
283 i18n_error_code_e i18n_error;
284 ERR_MAPPING(icu_error, i18n_error);
285 set_last_result(i18n_error);
286 return needed_buffer_size;
289 int i18n_ulocale_keywords_create (const char *locale_id, i18n_uenumeration_h *enumeration )
291 if (NULL == enumeration){
292 return I18N_ERROR_INVALID_PARAMETER;
294 UErrorCode icu_error = U_ZERO_ERROR;
295 *enumeration = (i18n_uenumeration_h)uloc_openKeywords(locale_id, &icu_error);
296 ERR("Error Code : %d", icu_error);
297 i18n_error_code_e i18n_error;
298 ERR_MAPPING(icu_error, i18n_error);
302 int32_t i18n_ulocale_get_keyword_value (const char *locale_id, const char *keyword_name, char *buffer,
303 int32_t buffer_capacity)
305 if(NULL == locale_id || NULL == keyword_name || NULL == buffer || buffer_capacity < 0){
306 set_last_result(I18N_ERROR_INVALID_PARAMETER);
310 UErrorCode icu_error = U_ZERO_ERROR;
311 int32_t len_of_keyword = uloc_getKeywordValue(locale_id, keyword_name, buffer, buffer_capacity, &icu_error);
312 ERR("Error Code : %d", icu_error);
314 i18n_error_code_e i18n_error;
315 ERR_MAPPING(icu_error, i18n_error);
316 set_last_result(i18n_error);
317 return len_of_keyword;
320 int32_t i18n_ulocale_set_keyword_value (const char *keyword_name, const char *keyword_value, char *buffer,
321 int32_t buffer_capacity)
323 if(NULL == keyword_name || NULL == buffer || buffer_capacity < 0){
324 set_last_result(I18N_ERROR_INVALID_PARAMETER);
328 UErrorCode icu_error = U_ZERO_ERROR;
329 int32_t needed_buffer_size = uloc_setKeywordValue(keyword_name, keyword_value, buffer, buffer_capacity, &icu_error);
330 ERR("Error Code : %d", icu_error);
332 i18n_error_code_e i18n_error;
333 ERR_MAPPING(icu_error, i18n_error);
334 set_last_result(i18n_error);
335 return needed_buffer_size;
338 int i18n_ulocale_get_character_orientation (const char *locale_id, i18n_ulocale_layout_type_e *layout_type)
340 if (NULL == layout_type){
341 return I18N_ERROR_INVALID_PARAMETER;
343 UErrorCode icu_error = U_ZERO_ERROR;
344 *layout_type = uloc_getCharacterOrientation(locale_id, &icu_error);
345 ERR("Error Code : %d", icu_error);
347 i18n_error_code_e i18n_error;
348 ERR_MAPPING(icu_error, i18n_error);
352 int i18n_ulocale_get_line_orientation (const char *locale_id, i18n_ulocale_layout_type_e *layout_type)
354 if (NULL == layout_type){
355 return I18N_ERROR_INVALID_PARAMETER;
357 UErrorCode icu_error = U_ZERO_ERROR;
358 *layout_type = uloc_getLineOrientation(locale_id, &icu_error);
359 ERR("Error Code : %d", icu_error);
361 i18n_error_code_e i18n_error;
362 ERR_MAPPING(icu_error, i18n_error);
366 int32_t i18n_ulocale_get_locale_for_lcid (uint32_t host_id, char *locale, int32_t locale_capacity)
368 if(NULL == locale || locale_capacity < 0){
369 set_last_result(I18N_ERROR_INVALID_PARAMETER);
373 UErrorCode icu_error = U_ZERO_ERROR;
374 int32_t needed_buffer_size = uloc_getLocaleForLCID(host_id, locale, locale_capacity, &icu_error);
375 ERR("Error Code : %d", icu_error);
377 i18n_error_code_e i18n_error;
378 ERR_MAPPING(icu_error, i18n_error);
379 set_last_result(i18n_error);
380 return needed_buffer_size;
383 int32_t i18n_ulocale_add_likely_subtags (const char *locale_id, char *maximized_locale_id,
384 int32_t maximized_locale_id_capacity)
386 UErrorCode icu_error = U_ZERO_ERROR;
387 int32_t needed_buffer_size = uloc_addLikelySubtags(locale_id, maximized_locale_id, maximized_locale_id_capacity, &icu_error);
388 ERR("Error Code : %d", icu_error);
390 i18n_error_code_e i18n_error;
391 ERR_MAPPING(icu_error, i18n_error);
392 set_last_result(i18n_error);
393 return needed_buffer_size;
396 int32_t i18n_ulocale_minimize_subtags (const char *locale_id, char *minimized_locale_id,
397 int32_t minimized_locale_id_capacity)
399 UErrorCode icu_error = U_ZERO_ERROR;
400 int32_t needed_buffer_size = uloc_minimizeSubtags(locale_id, minimized_locale_id, minimized_locale_id_capacity, &icu_error);
401 ERR("Error Code : %d", icu_error);
403 i18n_error_code_e i18n_error;
404 ERR_MAPPING(icu_error, i18n_error);
405 set_last_result(i18n_error);
406 return needed_buffer_size;
409 int32_t i18n_ulocale_for_language_tag (const char *langtag, char *locale_id, int32_t locale_id_capacity,
410 int32_t *parsed_length)
412 if(NULL == langtag || NULL == locale_id || locale_id_capacity < 0){
413 set_last_result(I18N_ERROR_INVALID_PARAMETER);
417 UErrorCode icu_error = U_ZERO_ERROR;
418 int32_t len_of_loc = uloc_forLanguageTag(langtag, locale_id, locale_id_capacity, parsed_length, &icu_error);
419 ERR("Error Code : %d", icu_error);
421 i18n_error_code_e i18n_error;
422 ERR_MAPPING(icu_error, i18n_error);
423 set_last_result(i18n_error);
427 int32_t i18n_ulocale_to_language_tag (const char *locale_id, char *langtag, int32_t langtag_capacity,
430 if(NULL == locale_id || NULL == langtag || langtag_capacity < 0){
431 set_last_result(I18N_ERROR_INVALID_PARAMETER);
435 UErrorCode icu_error = U_ZERO_ERROR;
436 int32_t len_of_loc = uloc_toLanguageTag(locale_id, langtag, langtag_capacity, strict, &icu_error);
437 ERR("Error Code : %d", icu_error);
439 i18n_error_code_e i18n_error;
440 ERR_MAPPING(icu_error, i18n_error);
441 set_last_result(i18n_error);