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