91804e264b0bd02a2052c2c39d366d177dd5b563
[platform/core/appfw/appcore-widget.git] / src / widget-i18n.c
1 /*
2  * Copyright (c) 2015 - 2016 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 *r;
37         char *lang = vconf_get_str(VCONFKEY_LANGSET);
38         if (lang) {
39                 setenv("LANG", lang, 1);
40                 setenv("LC_MESSAGES", lang, 1);
41
42                 r = setlocale(LC_ALL, "");
43                 if (r == NULL) {
44                         r = setlocale(LC_ALL, lang);
45                         if (r)
46                                 _D("*****appcore setlocale=%s\n", r);
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
73                 r = setlocale(LC_ALL, "");
74                 if (r != NULL)
75                         _D("*****appcore setlocale=%s\n", r);
76
77                 free(region);
78         } else {
79                 _E("failed to get current region format for set region env");
80         }
81 }
82
83 static int __set_i18n(const char *domain)
84 {
85         char *r;
86         char dirname[PATH_MAX] = {0, };
87         char *lang;
88
89         if (domain == NULL) {
90                 errno = EINVAL;
91                 return -1;
92         }
93
94         snprintf(dirname, PATH_MAX, "%s/res/locale", aul_get_app_root_path());
95         _D("locale dir: %s", dirname);
96
97         r = setlocale(LC_ALL, "");
98         /* if locale is not set properly, try again to set as language base */
99         if (r == NULL) {
100                 lang = vconf_get_str(VCONFKEY_LANGSET);
101                 r = setlocale(LC_ALL, lang);
102                 if (r)
103                         _D("*****appcore setlocale=%s\n", r);
104
105                 if (lang)
106                         free(lang);
107         }
108         if (r == NULL)
109                 _E("appcore: setlocale() error");
110
111         r = bindtextdomain(domain, dirname);
112         if (r == NULL)
113                 _E("appcore: bindtextdomain() error");
114
115         r = textdomain(domain);
116         if (r == NULL)
117                 _E("appcore: textdomain() error");
118
119         return 0;
120 }
121
122 int _set_i18n(const char *domainname)
123 {
124         _update_lang();
125         _update_region();
126
127         return __set_i18n(domainname);
128 }
129