tizen 2.3.1 release
[framework/api/base-utils.git] / src / utils_i18n_ulocale.c
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <unicode/uloc.h>
18 #include <utils_i18n_ulocale.h>
19 #include <utils_i18n_private.h>
20
21 int i18n_ulocale_get_default (const char **locale)
22 {
23     retv_if (locale == NULL, I18N_ERROR_INVALID_PARAMETER);
24
25     *locale = uloc_getDefault();
26
27     return I18N_ERROR_NONE;
28 }
29
30 int i18n_ulocale_set_default (const char *locale_id)
31 {
32     UErrorCode icu_error = U_ZERO_ERROR;
33     uloc_setDefault(locale_id, &icu_error);
34
35     return _i18n_error_mapping(icu_error);
36 }
37
38 int i18n_ulocale_get_language (const char *locale_id, char *language, int32_t language_capacity, int32_t *buf_size_language)
39 {
40     retv_if(buf_size_language == NULL || language == NULL, I18N_ERROR_INVALID_PARAMETER);
41
42     UErrorCode icu_error = U_ZERO_ERROR;
43     *buf_size_language = uloc_getLanguage(locale_id, language, language_capacity, &icu_error);
44
45     return _i18n_error_mapping(icu_error);
46 }
47
48 int32_t i18n_ulocale_get_country (const char *locale_id, char *country, int32_t country_capacity, int *error)
49 {
50     if(NULL == country) {
51         if(NULL != error) {
52             *error = I18N_ERROR_INVALID_PARAMETER;
53         }
54         return 0;
55     }
56
57     UErrorCode icu_error = U_ZERO_ERROR;
58     int32_t result = uloc_getCountry(locale_id, country, country_capacity, &icu_error);
59     if(NULL != error) {
60         *error = _i18n_error_mapping(icu_error);
61     }
62
63     return result;
64 }
65
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)
67 {
68     retv_if(buf_size_display_name == NULL, I18N_ERROR_INVALID_PARAMETER);
69
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);
72
73     return _i18n_error_mapping(icu_error);
74 }
75
76 const char* i18n_ulocale_get_available (int32_t n)
77 {
78     if(n < 0){
79         set_last_result(I18N_ERROR_INVALID_PARAMETER);
80         return NULL;
81     }
82     set_last_result(I18N_ERROR_NONE);
83     return uloc_getAvailable(n);
84 }
85
86 int32_t i18n_ulocale_count_available (void)
87 {
88     set_last_result(I18N_ERROR_NONE);
89     return uloc_countAvailable();
90 }
91
92 // Newly Added APIs
93
94 int32_t i18n_ulocale_get_script (const char *locale_id, char *script, int32_t script_capacity)
95 {
96     if(NULL == script || script_capacity < 0){
97         set_last_result(I18N_ERROR_INVALID_PARAMETER);
98         return -1;
99     }
100
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);
104
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;
109 }
110
111 int32_t i18n_ulocale_get_variant (const char *locale_id, char *variant, int32_t variant_capacity)
112 {
113     if(NULL == variant || variant_capacity < 0){
114         set_last_result(I18N_ERROR_INVALID_PARAMETER);
115         return -1;
116     }
117
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);
121
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;
126 }
127
128 int32_t i18n_ulocale_get_name (const char *locale_id, char *name, int32_t name_capacity)
129 {
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);
133
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;
138 }
139
140 int32_t i18n_ulocale_canonicalize (const char *locale_id, char *name, int32_t name_capacity)
141 {
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);
145
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;
150 }
151
152 const char *i18n_ulocale_get_iso3_language (const char *locale_id)
153 {
154     set_last_result(I18N_ERROR_NONE);
155     return uloc_getISO3Language(locale_id);
156 }
157
158 const char *i18n_ulocale_get_iso3_country (const char *locale_id)
159 {
160     set_last_result(I18N_ERROR_NONE);
161     return uloc_getISO3Country(locale_id);
162 }
163
164 uint32_t i18n_ulocale_get_lcid (const char *locale_id)
165 {
166     set_last_result(I18N_ERROR_NONE);
167     return uloc_getLCID(locale_id);
168 }
169
170 int32_t i18n_ulocale_get_display_language (const char *locale, const char *display_locale, i18n_uchar *language,
171         int32_t language_capacity)
172 {
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);
176
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;
181 }
182
183 int32_t i18n_ulocale_get_display_script (const char *locale, const char *display_locale, i18n_uchar *script,
184         int32_t script_capacity)
185 {
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);
189
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;
194 }
195
196 int32_t i18n_ulocale_get_display_country (const char *locale, const char *display_locale, i18n_uchar *country,
197         int32_t country_capacity)
198 {
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);
202
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;
207 }
208
209 int32_t i18n_ulocale_get_display_variant (const char *locale, const char *display_locale, i18n_uchar *variant,
210         int32_t variant_capacity)
211 {
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);
215
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;
220 }
221
222 int32_t i18n_ulocale_get_display_keyword (const char *keyword, const char *display_locale, i18n_uchar *dest,
223         int32_t dest_capacity)
224 {
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);
228
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;
233 }
234
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)
237 {
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);
241
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;
246 }
247
248 const char * const *i18n_ulocale_get_iso_languages (void)
249 {
250     set_last_result(I18N_ERROR_NONE);
251     return uloc_getISOLanguages();
252 }
253
254 const char * const *i18n_ulocale_get_iso_countries (void)
255 {
256     set_last_result(I18N_ERROR_NONE);
257     return uloc_getISOCountries();
258 }
259
260 int32_t i18n_ulocale_get_parent (const char *locale_id, char *parent, int32_t parent_capacity)
261 {
262     if(NULL == parent || parent_capacity < 0){
263         set_last_result(I18N_ERROR_INVALID_PARAMETER);
264         return -1;
265     }
266
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);
270
271     i18n_error_code_e i18n_error;
272     ERR_MAPPING(icu_error, i18n_error);
273     set_last_result(i18n_error);
274     return len_of_loc;
275 }
276
277 int32_t i18n_ulocale_get_base_name (const char *locale_id, char *name, int32_t name_capacity)
278 {
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);
282
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;
287 }
288
289 int i18n_ulocale_keywords_create (const char *locale_id, i18n_uenumeration_h *enumeration )
290 {
291     if (NULL == enumeration){
292         return I18N_ERROR_INVALID_PARAMETER;
293     }
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);
299     return i18n_error;
300 }
301
302 int32_t i18n_ulocale_get_keyword_value (const char *locale_id, const char *keyword_name, char *buffer,
303         int32_t buffer_capacity)
304 {
305     if(NULL == locale_id || NULL == keyword_name || NULL == buffer || buffer_capacity < 0){
306         set_last_result(I18N_ERROR_INVALID_PARAMETER);
307         return -1;
308     }
309
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);
313
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;
318 }
319
320 int32_t i18n_ulocale_set_keyword_value (const char *keyword_name, const char *keyword_value, char *buffer,
321         int32_t buffer_capacity)
322 {
323     if(NULL == keyword_name || NULL == buffer || buffer_capacity < 0){
324         set_last_result(I18N_ERROR_INVALID_PARAMETER);
325         return -1;
326     }
327
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);
331
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;
336 }
337
338 int i18n_ulocale_get_character_orientation (const char *locale_id, i18n_ulocale_layout_type_e *layout_type)
339 {
340     if (NULL == layout_type){
341         return I18N_ERROR_INVALID_PARAMETER;
342     }
343     UErrorCode icu_error = U_ZERO_ERROR;
344     *layout_type = uloc_getCharacterOrientation(locale_id, &icu_error);
345     ERR("Error Code : %d", icu_error);
346
347     i18n_error_code_e i18n_error;
348     ERR_MAPPING(icu_error, i18n_error);
349     return i18n_error;
350 }
351
352 int i18n_ulocale_get_line_orientation (const char *locale_id, i18n_ulocale_layout_type_e *layout_type)
353 {
354     if (NULL == layout_type){
355         return I18N_ERROR_INVALID_PARAMETER;
356     }
357     UErrorCode icu_error = U_ZERO_ERROR;
358    *layout_type = uloc_getLineOrientation(locale_id, &icu_error);
359     ERR("Error Code : %d", icu_error);
360
361     i18n_error_code_e i18n_error;
362     ERR_MAPPING(icu_error, i18n_error);
363     return i18n_error;
364 }
365
366 int32_t i18n_ulocale_get_locale_for_lcid (uint32_t host_id, char *locale, int32_t locale_capacity)
367 {
368     if(NULL == locale || locale_capacity < 0){
369         set_last_result(I18N_ERROR_INVALID_PARAMETER);
370         return -1;
371     }
372
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);
376
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;
381 }
382
383 int32_t i18n_ulocale_add_likely_subtags (const char *locale_id, char *maximized_locale_id,
384         int32_t maximized_locale_id_capacity)
385 {
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);
389
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;
394 }
395
396 int32_t i18n_ulocale_minimize_subtags (const char *locale_id, char *minimized_locale_id,
397         int32_t minimized_locale_id_capacity)
398 {
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);
402
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;
407 }
408
409 int32_t i18n_ulocale_for_language_tag (const char *langtag, char *locale_id, int32_t locale_id_capacity,
410         int32_t *parsed_length)
411 {
412     if(NULL == langtag || NULL == locale_id || locale_id_capacity < 0){
413         set_last_result(I18N_ERROR_INVALID_PARAMETER);
414         return -1;
415     }
416
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);
420
421     i18n_error_code_e i18n_error;
422     ERR_MAPPING(icu_error, i18n_error);
423     set_last_result(i18n_error);
424     return len_of_loc;
425 }
426
427 int32_t i18n_ulocale_to_language_tag (const char *locale_id, char *langtag, int32_t langtag_capacity,
428         i18n_ubool strict)
429 {
430     if(NULL == locale_id || NULL == langtag || langtag_capacity < 0){
431         set_last_result(I18N_ERROR_INVALID_PARAMETER);
432         return -1;
433     }
434
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);
438
439     i18n_error_code_e i18n_error;
440     ERR_MAPPING(icu_error, i18n_error);
441     set_last_result(i18n_error);
442     return len_of_loc;
443 }