Merging r50470 through r50477 from trunk into soc-2011-tomato
[blender.git] / source / blender / blenfont / intern / blf_lang.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  * 
21  * Contributor(s): Blender Foundation.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/blenfont/intern/blf_lang.c
27  *  \ingroup blf
28  */
29
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "BKE_global.h"
36
37 #include "BLF_api.h"
38
39 #include "BLF_translation.h" /* own include */
40
41 #ifdef WITH_INTERNATIONAL
42
43 #include <locale.h>
44
45 #if defined(_WIN32)
46 #include <windows.h>
47 #endif
48
49 #include "libintl.h"
50
51 #include "DNA_userdef_types.h"
52
53 #include "DNA_listBase.h"
54 #include "DNA_vec_types.h"
55
56 #include "MEM_guardedalloc.h"
57
58 #include "BLI_linklist.h" /* linknode */
59 #include "BLI_string.h"
60 #include "BLI_utildefines.h"
61 #include "BLI_path_util.h"
62
63 #define SYSTEM_ENCODING_DEFAULT "UTF-8"
64 #define FONT_SIZE_DEFAULT 12
65
66 /* locale options. */
67 static char global_messagepath[1024];
68 static char global_language[32];
69 static char global_encoding_name[32];
70
71 /* map from the rna_userdef.c:rna_def_userdef_system(BlenderRNA *brna):language_items */
72 static const char *locales[] = {
73         "", "",
74         "english", "en_US",
75         "japanese", "ja_JP",
76         "dutch", "nl_NL",
77         "italian", "it_IT",
78         "german", "de_DE",
79         "finnish", "fi_FI",
80         "swedish", "sv_SE",
81         "french", "fr_FR",
82         "spanish", "es",
83         "catalan", "ca_AD",
84         "czech", "cs_CZ",
85         "portuguese", "pt_PT",
86 #if defined(_WIN32) && !defined(FREE_WINDOWS)
87         "Chinese (Simplified)_China.1252", "zh_CN",
88         "Chinese (Traditional)_China.1252", "zh_TW",
89 #else
90         "chs", "zh_CN",
91         "cht", "zh_TW",
92 #endif
93         "russian", "ru_RU",
94         "croatian", "hr_HR",
95         "serbian", "sr_RS",
96         "ukrainian", "uk_UA",
97         "polish", "pl_PL",
98         "romanian", "ro_RO",
99         "arabic", "ar_EG",
100         "bulgarian", "bg_BG",
101         "greek", "el_GR",
102         "korean", "ko_KR",
103         "nepali", "ne_NP",
104         "persian", "fa_IR",
105         "indonesian", "id_ID",
106         "serbian (latin)", "sr_RS@latin",
107         "kyrgyz", "ky_KG",
108         "turkish", "tr_TR",
109         "hungarian", "hu_HU",
110         "brazilian portuguese", "pt_BR",
111         "hebrew", "he_IL",
112 };
113
114 void BLF_lang_init(void)
115 {
116         char *messagepath = BLI_get_folder(BLENDER_DATAFILES, "locale");
117 /*      printf("%s\n", messagepath);*/
118
119         BLI_strncpy(global_encoding_name, SYSTEM_ENCODING_DEFAULT, sizeof(global_encoding_name));
120         
121         if (messagepath) {
122                 BLI_strncpy(global_messagepath, messagepath, sizeof(global_messagepath));
123         }
124         else {
125                 printf("%s: 'locale' data path for translations not found, continuing\n", __func__);
126                 global_messagepath[0] = '\0';
127         }
128         
129 }
130
131 /* get LANG/LANGUAGE environment variable */
132 static void get_language_variable(const char *varname, char *var, int maxlen)
133 {
134         char *env = getenv(varname);
135
136         if (env) {
137                 char *s;
138
139                 /* store defaul locale */
140                 BLI_strncpy(var, env, maxlen);
141
142                 /* use first language as default */
143                 s = strchr(var, ':');
144                 if (s)
145                         s[0] = 0;
146         }
147 }
148
149 /* get language to be used based on locale(which might be empty when using default language) and
150  * LANG environment variable
151  */
152 static void get_language(const char *locale, const char *lang, char *language, int maxlen)
153 {
154         if (locale[0]) {
155                 BLI_strncpy(language, locale, maxlen);
156         }
157         else {
158                 char *s;
159
160                 BLI_strncpy(language, lang, maxlen);
161
162                 s = strchr(language, '.');
163                 if (s)
164                         s[0] = 0;
165         }
166 }
167
168 /* XXX WARNING!!! IN osx somehow the previous function call jumps in this one??? (ton, ppc) */
169 void BLF_lang_set(const char *str)
170 {
171         char *locreturn;
172         const char *short_locale;
173         int ok = 1;
174         const char *long_locale = locales[2 * U.language];
175
176         if ((U.transopts & USER_DOTRANSLATE) == 0)
177                 return;
178
179         if (str)
180                 short_locale = str;
181         else
182                 short_locale = locales[2 * U.language + 1];
183
184 #if defined(_WIN32) && !defined(FREE_WINDOWS)
185         if (short_locale) {
186                 char *envStr;
187
188                 if (U.language == 0) /* use system setting */
189                         envStr = BLI_sprintfN("LANG=%s", getenv("LANG"));
190                 else
191                         envStr = BLI_sprintfN("LANG=%s", short_locale);
192
193                 gettext_putenv(envStr);
194                 MEM_freeN(envStr);
195         }
196
197         locreturn = setlocale(LC_ALL, long_locale);
198
199         if (locreturn == NULL) {
200                 if (G.debug & G_DEBUG)
201                         printf("Could not change locale to %s\n", long_locale);
202
203                 ok = 0;
204         }
205 #else
206         {
207                 static char default_lang[64] = "\0";
208                 static char default_language[64] = "\0";
209
210                 if (default_lang[0] == 0)
211                         get_language_variable("LANG", default_lang, sizeof(default_lang));
212
213                 if (default_language[0] == 0)
214                         get_language_variable("LANGUAGE", default_language, sizeof(default_language));
215
216                 if (short_locale[0]) {
217                         if (G.debug & G_DEBUG)
218                                 printf("Setting LANG= and LANGUAGE to %s\n", short_locale);
219
220                         BLI_setenv("LANG", short_locale);
221                         BLI_setenv("LANGUAGE", short_locale);
222                 }
223                 else {
224                         if (G.debug & G_DEBUG)
225                                 printf("Setting LANG=%s and LANGUAGE=%s\n", default_lang, default_language);
226
227                         BLI_setenv("LANG", default_lang);
228                         BLI_setenv("LANGUAGE", default_language);
229                 }
230
231                 locreturn = setlocale(LC_ALL, short_locale);
232
233                 if (locreturn == NULL) {
234                         char *short_locale_utf8 = NULL;
235
236                         if (short_locale[0]) {
237                                 short_locale_utf8 = BLI_sprintfN("%s.UTF-8", short_locale);
238                                 locreturn = setlocale(LC_ALL, short_locale_utf8);
239                         }
240
241                         if (locreturn == NULL) {
242                                 char language[65];
243
244                                 get_language(long_locale, default_lang, language, sizeof(language));
245
246                                 if (G.debug & G_DEBUG) {
247                                         if (short_locale[0])
248                                                 printf("Could not change locale to %s nor %s\n", short_locale, short_locale_utf8);
249                                         else
250                                                 printf("Could not reset locale\n");
251
252                                         printf("Fallback to LANG=%s and LANGUAGE=%s\n", default_lang, language);
253                                 }
254
255                                 /* fallback to default settings */
256                                 BLI_setenv("LANG", default_lang);
257                                 BLI_setenv("LANGUAGE", language);
258
259                                 locreturn = setlocale(LC_ALL, "");
260
261                                 ok = 0;
262                         }
263
264                         if (short_locale_utf8)
265                                 MEM_freeN(short_locale_utf8);
266                 }
267         }
268 #endif
269
270         if (ok) {
271                 /*printf("Change locale to %s\n", locreturn ); */
272                 BLI_strncpy(global_language, locreturn, sizeof(global_language));
273         }
274
275         setlocale(LC_NUMERIC, "C");
276
277         textdomain(TEXT_DOMAIN_NAME);
278         bindtextdomain(TEXT_DOMAIN_NAME, global_messagepath);
279         bind_textdomain_codeset(TEXT_DOMAIN_NAME, global_encoding_name);
280 }
281
282 const char *BLF_lang_get(void)
283 {
284         return locales[2 * U.language + 1];
285 }
286
287 void BLF_lang_encoding(const char *str)
288 {
289         BLI_strncpy(global_encoding_name, str, sizeof(global_encoding_name));
290         /* bind_textdomain_codeset(TEXT_DOMAIN_NAME, encoding_name); */
291 }
292
293 #else /* ! WITH_INTERNATIONAL */
294
295 void BLF_lang_init(void)
296 {
297         return;
298 }
299
300 void BLF_lang_encoding(const char *str)
301 {
302         (void)str;
303         return;
304 }
305
306 void BLF_lang_set(const char *str)
307 {
308         (void)str;
309         return;
310 }
311
312 const char *BLF_lang_get(void)
313 {
314         return "";
315 }
316
317 #endif /* WITH_INTERNATIONAL */