use class_name for instance id
[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
45                         if (r)
46                                 _D("*****appcore setlocale=%s\n", r);
47
48                 }
49                 free(lang);
50         } else {
51                 _E("failed to get current language for set lang env");
52         }
53 }
54
55 void _update_region(void)
56 {
57         char *region;
58         char *r;
59
60         region = vconf_get_str(VCONFKEY_REGIONFORMAT);
61         if (region) {
62                 setenv("LC_CTYPE", region, 1);
63                 setenv("LC_NUMERIC", region, 1);
64                 setenv("LC_TIME", region, 1);
65                 setenv("LC_COLLATE", region, 1);
66                 setenv("LC_MONETARY", region, 1);
67                 setenv("LC_PAPER", region, 1);
68                 setenv("LC_NAME", region, 1);
69                 setenv("LC_ADDRESS", region, 1);
70                 setenv("LC_TELEPHONE", region, 1);
71                 setenv("LC_MEASUREMENT", region, 1);
72                 setenv("LC_IDENTIFICATION", region, 1);
73                 r = setlocale(LC_ALL, "");
74
75                 if (r != NULL)
76                         _D("*****appcore setlocale=%s\n", r);
77
78                 free(region);
79         } else {
80                 _E("failed to get current region format for set region env");
81         }
82 }
83
84 static int __set_i18n(const char *domain)
85 {
86         char *r;
87         char dirname[PATH_MAX] = {0, };
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                 char *lang = vconf_get_str(VCONFKEY_LANGSET);
101                 r = setlocale(LC_ALL, lang);
102
103                 if (r)
104                         _D("*****appcore setlocale=%s\n", r);
105
106                 if (lang)
107                         free(lang);
108
109         }
110         if (r == NULL)
111                 _E("appcore: setlocale() error");
112
113         r = bindtextdomain(domain, dirname);
114         if (r == NULL)
115                 _E("appcore: bindtextdomain() error");
116
117         r = textdomain(domain);
118         if (r == NULL)
119                 _E("appcore: textdomain() error");
120
121         return 0;
122 }
123
124 int _set_i18n(const char *domainname)
125 {
126         _update_lang();
127         _update_region();
128
129         return __set_i18n(domainname);
130 }
131