de03620616b62cd05825ee603ac4c11a38b3b99a
[framework/appfw/app-core.git] / src / appcore-i18n.c
1 /*
2  *  app-core
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22
23 #include <locale.h>
24 #include <libintl.h>
25 #include <stdlib.h>
26 #include <errno.h>
27
28 #include <vconf.h>
29
30 #include "appcore-internal.h"
31
32 static int _set;
33
34 void update_lang(void)
35 {
36         char *lang;
37         char *r;
38
39         lang = vconf_get_str(VCONFKEY_LANGSET);
40         if (lang) {
41                 setenv("LANG", lang, 1);
42                 setenv("LC_MESSAGES", lang, 1);
43                 r = setlocale(LC_ALL, "");
44                 if (r == NULL) {
45                         r = setlocale(LC_ALL, vconf_get_str(VCONFKEY_LANGSET));
46                         _DBG("*****appcore setlocale=%s\n", r);
47                 }
48                 free(lang);
49         }
50 }
51
52 void update_region(void)
53 {
54         char *region;
55
56         region = vconf_get_str(VCONFKEY_REGIONFORMAT);
57         if (region) {
58                 setenv("LC_CTYPE", region, 1);
59                 setenv("LC_NUMERIC", region, 1);
60                 setenv("LC_TIME", region, 1);
61                 setenv("LC_COLLATE", region, 1);
62                 setenv("LC_MONETARY", region, 1);
63                 setenv("LC_PAPER", region, 1);
64                 setenv("LC_NAME", region, 1);
65                 setenv("LC_ADDRESS", region, 1);
66                 setenv("LC_TELEPHONE", region, 1);
67                 setenv("LC_MEASUREMENT", region, 1);
68                 setenv("LC_IDENTIFICATION", region, 1);
69                 free(region);
70         }
71 }
72
73 static int __set_i18n(const char *domain, const char *dir)
74 {
75         char *r;
76
77         if (domain == NULL) {
78                 errno = EINVAL;
79                 return -1;
80         }
81
82         r = setlocale(LC_ALL, "");
83         /* if locale is not set properly, try again to set as language base */
84         if (r == NULL) {
85                 r = setlocale(LC_ALL, vconf_get_str(VCONFKEY_LANGSET));
86                 _DBG("*****appcore setlocale=%s\n", r);
87         }
88         _retvm_if(r == NULL, -1, "appcore: setlocale() error");
89
90         r = bindtextdomain(domain, dir);
91         _retvm_if(r == NULL, -1, "appcore: bindtextdomain() error");
92
93         r = textdomain(domain);
94         _retvm_if(r == NULL, -1, "appcore: textdomain() error");
95
96         return 0;
97 }
98
99 EXPORT_API int appcore_set_i18n(const char *domainname, const char *dirname)
100 {
101         int r;
102
103         update_lang();
104         update_region();
105
106         r = __set_i18n(domainname, dirname);
107         if (r == 0)
108                 _set = 1;
109
110         return r;
111 }
112
113 int set_i18n(const char *domainname, const char *dirname)
114 {
115         _retv_if(_set, 0);
116
117         update_lang();
118         update_region();
119
120         return __set_i18n(domainname, dirname);
121 }
122
123 EXPORT_API int appcore_get_timeformat(enum appcore_time_format *timeformat)
124 {
125         int r;
126
127         if (timeformat == NULL) {
128                 errno = EINVAL;
129                 return -1;
130         }
131
132         r = vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, (int *)timeformat);
133
134         if (r < 0) {
135                 *timeformat = APPCORE_TIME_FORMAT_UNKNOWN;
136                 return -1;
137         } else
138                 return 0;
139 }