appcore-widget : initial commit
[platform/core/appfw/appcore-widget.git] / src / widget-i18n.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 <locale.h>
18 #include <libintl.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include <linux/limits.h>
25 #include <glib.h>
26 #include <fcntl.h>
27
28 #include <aul.h>
29 #include <vconf.h>
30
31 #include "widget-log.h"
32 #include "widget-private.h"
33
34 void _update_lang(void)
35 {
36         char *lang = vconf_get_str(VCONFKEY_LANGSET);
37         if (lang) {
38                 setenv("LANG", lang, 1);
39                 setenv("LC_MESSAGES", lang, 1);
40
41                 char *r = setlocale(LC_ALL, "");
42                 if (r == NULL) {
43                         r = setlocale(LC_ALL, lang);
44                         if (r) {
45                                 _D("*****appcore setlocale=%s\n", r);
46                         }
47                 }
48                 free(lang);
49         } else {
50                 _E("failed to get current language for set lang env");
51         }
52 }
53
54 void _update_region(void)
55 {
56         char *region;
57         char *r;
58
59         region = vconf_get_str(VCONFKEY_REGIONFORMAT);
60         if (region) {
61                 setenv("LC_CTYPE", region, 1);
62                 setenv("LC_NUMERIC", region, 1);
63                 setenv("LC_TIME", region, 1);
64                 setenv("LC_COLLATE", region, 1);
65                 setenv("LC_MONETARY", region, 1);
66                 setenv("LC_PAPER", region, 1);
67                 setenv("LC_NAME", region, 1);
68                 setenv("LC_ADDRESS", region, 1);
69                 setenv("LC_TELEPHONE", region, 1);
70                 setenv("LC_MEASUREMENT", region, 1);
71                 setenv("LC_IDENTIFICATION", region, 1);
72                 r = setlocale(LC_ALL, "");
73                 if (r != NULL) {
74                         _D("*****appcore setlocale=%s\n", r);
75                 }
76                 free(region);
77         } else {
78                 _E("failed to get current region format for set region env");
79         }
80 }
81
82 static int __read_proc(const char *path, char *buf, int size)
83 {
84         int fd;
85         int ret;
86
87         if (buf == NULL || path == NULL)
88                 return -1;
89
90         fd = open(path, O_RDONLY);
91         if (fd < 0)
92                 return -1;
93
94         ret = read(fd, buf, size - 1);
95         if (ret <= 0) {
96                 close(fd);
97                 return -1;
98         } else
99                 buf[ret] = 0;
100
101         close(fd);
102
103         return ret;
104 }
105
106 static char* __proc_get_cmdline()
107 {
108 #define MAX_CMD_BUFSZ 1024
109
110         char buf[MAX_CMD_BUFSZ];
111         int ret;
112
113         snprintf(buf, sizeof(buf), "/proc/%d/cmdline", getpid());
114         ret = __read_proc(buf, buf, sizeof(buf));
115         if (ret <= 0)
116                 return NULL;
117
118         return strdup(buf);
119 }
120
121 static void __get_dir_name(char *dirname, int maxlen)
122 {
123         const char *path = NULL;
124
125         path = aul_get_app_root_path();
126         snprintf(dirname, maxlen, "%s/res/locale", path);
127         free(path);
128 }
129
130
131 static int __set_i18n(const char *domain)
132 {
133         char *r;
134         char dirname[PATH_MAX] = {0, };
135
136         if (domain == NULL) {
137                 errno = EINVAL;
138                 return -1;
139         }
140
141         __get_dir_name(dirname, PATH_MAX - 1);
142         _D("locale dir: %s", dirname);
143
144         r = setlocale(LC_ALL, "");
145         /* if locale is not set properly, try again to set as language base */
146         if (r == NULL) {
147                 char *lang = vconf_get_str(VCONFKEY_LANGSET);
148                 r = setlocale(LC_ALL, lang);
149                 if (r) {
150                         _D("*****appcore setlocale=%s\n", r);
151                 }
152                 if (lang) {
153                         free(lang);
154                 }
155         }
156         if (r == NULL) {
157                 _E("appcore: setlocale() error");
158         }
159
160         r = bindtextdomain(domain, dirname);
161         if (r == NULL) {
162                 _E("appcore: bindtextdomain() error");
163         }
164
165         r = textdomain(domain);
166         if (r == NULL) {
167                 _E("appcore: textdomain() error");
168         }
169
170         return 0;
171 }
172
173 int _set_i18n(const char *domainname)
174 {
175         _update_lang();
176         _update_region();
177
178         return __set_i18n(domainname);
179 }
180