Handle widget resize event
[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 #define PATH_LOCALE "locale"
35
36 void _update_lang(void)
37 {
38         char language[32];
39         char *r;
40         char *lang = vconf_get_str(VCONFKEY_LANGSET);
41         if (lang) {
42                 snprintf(language, sizeof(language), "%s:en_US:en_GB:en", lang);
43                 setenv("LANGUAGE", language, 1);
44                 setenv("LANG", lang, 1);
45                 setenv("LC_MESSAGES", lang, 1);
46
47                 r = setlocale(LC_ALL, "");
48                 if (r == NULL) {
49                         /* LCOV_EXCL_START */
50                         r = setlocale(LC_ALL, lang);
51                         if (r)
52                                 _D("*****appcore setlocale=%s\n", r);
53                         /* LCOV_EXCL_STOP */
54                 }
55                 free(lang);
56         } else {
57                 _E("failed to get current language for set lang env"); /* LCOV_EXCL_LINE */
58         }
59 }
60
61 void _update_region(void)
62 {
63         char *region;
64         char *r;
65
66         region = vconf_get_str(VCONFKEY_REGIONFORMAT);
67         if (region) {
68                 setenv("LC_CTYPE", region, 1);
69                 setenv("LC_NUMERIC", region, 1);
70                 setenv("LC_TIME", region, 1);
71                 setenv("LC_COLLATE", region, 1);
72                 setenv("LC_MONETARY", region, 1);
73                 setenv("LC_PAPER", region, 1);
74                 setenv("LC_NAME", region, 1);
75                 setenv("LC_ADDRESS", region, 1);
76                 setenv("LC_TELEPHONE", region, 1);
77                 setenv("LC_MEASUREMENT", region, 1);
78                 setenv("LC_IDENTIFICATION", region, 1);
79
80                 r = setlocale(LC_ALL, "");
81                 if (r != NULL)
82                         _D("*****appcore setlocale=%s\n", r);
83
84                 free(region);
85         } else {
86                 _E("failed to get current region format for set region env"); /* LCOV_EXCL_LINE */
87         }
88 }
89
90 static int __get_locale_resource_dir(char *locale_dir, int size)
91 {
92         const char *res_path;
93
94         res_path = aul_get_app_resource_path();
95         if (res_path == NULL) {
96                 _E("Failed to get resource path"); /* LCOV_EXCL_LINE */
97                 return -1; /* LCOV_EXCL_LINE */
98         }
99
100         snprintf(locale_dir, size, "%s" PATH_LOCALE, res_path);
101         if (access(locale_dir, R_OK) != 0)
102                 return -1;
103
104         return 0;
105 }
106
107 static int __set_i18n(const char *domain)
108 {
109         char *r;
110         char locale_dir[PATH_MAX];
111         char *lang;
112
113         if (domain == NULL) {
114                 errno = EINVAL; /* LCOV_EXCL_LINE */
115                 return -1; /* LCOV_EXCL_LINE */
116         }
117
118         __get_locale_resource_dir(locale_dir, sizeof(locale_dir));
119         _D("locale dir: %s", locale_dir);
120
121         r = setlocale(LC_ALL, "");
122         /* if locale is not set properly, try again to set as language base */
123         if (r == NULL) {
124                 /* LCOV_EXCL_START */
125                 lang = vconf_get_str(VCONFKEY_LANGSET);
126                 r = setlocale(LC_ALL, lang);
127                 if (r)
128                         _D("*****appcore setlocale=%s\n", r);
129
130                 if (lang)
131                         free(lang);
132                 /* LCOV_EXCL_STOP */
133         }
134         if (r == NULL)
135                 _E("appcore: setlocale() error"); /* LCOV_EXCL_LINE */
136
137         r = bindtextdomain(domain, locale_dir);
138         if (r == NULL)
139                 _E("appcore: bindtextdomain() error"); /* LCOV_EXCL_LINE */
140
141         r = textdomain(domain);
142         if (r == NULL)
143                 _E("appcore: textdomain() error"); /* LCOV_EXCL_LINE */
144
145         return 0;
146 }
147
148 int _set_i18n(const char *domainname)
149 {
150         _update_lang();
151         _update_region();
152
153         return __set_i18n(domainname);
154 }
155