Improvements to bmesh edge rotate
[blender-staging.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         "ptb", "pt_BR",
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",
108 };
109
110 void BLF_lang_init(void)
111 {
112         char *messagepath= BLI_get_folder(BLENDER_DATAFILES, "locale");
113         
114         BLI_strncpy(global_encoding_name, SYSTEM_ENCODING_DEFAULT, sizeof(global_encoding_name));
115         
116         if (messagepath) {
117                 BLI_strncpy(global_messagepath, messagepath, sizeof(global_messagepath));
118         }
119         else {
120                 printf("%s: 'locale' data path for translations not found, continuing\n", __func__);
121                 global_messagepath[0]= '\0';
122         }
123         
124 }
125
126 /* get LANG/LANGUAGE environment variable */
127 static void get_language_variable(const char *varname, char *var, int maxlen)
128 {
129         char *env= getenv(varname);
130
131         if(env) {
132                 char *s;
133
134                 /* store defaul locale */
135                 BLI_strncpy(var, env, maxlen);
136
137                 /* use first language as default */
138                 s= strchr(var, ':');
139                 if(s)
140                         s[0]= 0;
141         }
142 }
143
144 /* get language to be used based on locale(which might be empty when using default language) and
145  * LANG environment variable
146  */
147 static void get_language(const char *locale, const char *lang, char *language, int maxlen)
148 {
149         if(locale[0]) {
150                 BLI_strncpy(language, locale, maxlen);
151         }
152         else {
153                 char *s;
154
155                 BLI_strncpy(language, lang, maxlen);
156
157                 s= strchr(language, '.');
158                 if(s)
159                         s[0]= 0;
160         }
161 }
162
163 /* XXX WARNING!!! IN osx somehow the previous function call jumps in this one??? (ton, ppc) */
164 void BLF_lang_set(const char *str)
165 {
166         char *locreturn;
167         const char *short_locale;
168         int ok= 1;
169         const char *long_locale = locales[2 * U.language];
170
171         if((U.transopts&USER_DOTRANSLATE)==0)
172                 return;
173
174         if(str)
175                 short_locale = str;
176         else
177                 short_locale = locales[ 2 * U.language + 1];
178
179 #if defined (_WIN32) && !defined(FREE_WINDOWS)
180         if(short_locale) {
181                 char *envStr;
182
183                 if( U.language==0 )/* use system setting */
184                         envStr = BLI_sprintfN( "LANG=%s", getenv("LANG") );
185                 else
186                         envStr = BLI_sprintfN( "LANG=%s", short_locale );
187
188                 gettext_putenv(envStr);
189                 MEM_freeN(envStr);
190         }
191
192         locreturn= setlocale(LC_ALL, long_locale);
193
194         if (locreturn == NULL) {
195                 if(G.f & G_DEBUG)
196                         printf("Could not change locale to %s\n", long_locale);
197
198                 ok= 0;
199         }
200 #else
201         {
202                 static char default_lang[64]="\0";
203                 static char default_language[64]="\0";
204
205                 if(default_lang[0]==0)
206                         get_language_variable("LANG", default_lang, sizeof(default_lang));
207
208                 if(default_language[0]==0)
209                         get_language_variable("LANGUAGE", default_language, sizeof(default_language));
210
211                 if(short_locale[0]) {
212                         if(G.f & G_DEBUG)
213                                 printf("Setting LANG= and LANGUAGE to %s\n", short_locale);
214
215                         BLI_setenv("LANG", short_locale);
216                         BLI_setenv("LANGUAGE", short_locale);
217                 }
218                 else {
219                         if(G.f & G_DEBUG)
220                                 printf("Setting LANG=%s and LANGUAGE=%s\n", default_lang, default_language);
221
222                         BLI_setenv("LANG", default_lang);
223                         BLI_setenv("LANGUAGE", default_language);
224                 }
225
226                 locreturn= setlocale(LC_ALL, short_locale);
227
228                 if(locreturn == NULL) {
229                         char *short_locale_utf8= NULL;
230
231                         if(short_locale[0]) {
232                                 short_locale_utf8= BLI_sprintfN("%s.UTF-8", short_locale);
233                                 locreturn= setlocale(LC_ALL, short_locale_utf8);
234                         }
235
236                         if (locreturn == NULL) {
237                                 char language[65];
238
239                                 get_language(long_locale, default_lang, language, sizeof(language));
240
241                                 if(G.f & G_DEBUG) {
242                                         if(short_locale[0])
243                                                 printf("Could not change locale to %s nor %s\n", short_locale, short_locale_utf8);
244                                         else
245                                                 printf("Could not reset locale\n");
246
247                                         printf("Fallback to LANG=%s and LANGUAGE=%s\n", default_lang, language);
248                                 }
249
250                                 /* fallback to default settings */
251                                 BLI_setenv("LANG", default_lang);
252                                 BLI_setenv("LANGUAGE", language);
253
254                                 locreturn= setlocale(LC_ALL, "");
255
256                                 ok= 0;
257                         }
258
259                         if(short_locale_utf8)
260                                 MEM_freeN(short_locale_utf8);
261                 }
262         }
263 #endif
264
265         if(ok) {
266                 //printf("Change locale to %s\n", locreturn );
267                 BLI_strncpy(global_language, locreturn, sizeof(global_language));
268         }
269
270         setlocale(LC_NUMERIC, "C");
271
272         textdomain(TEXT_DOMAIN_NAME);
273         bindtextdomain(TEXT_DOMAIN_NAME, global_messagepath);
274         bind_textdomain_codeset(TEXT_DOMAIN_NAME, global_encoding_name);
275 }
276
277 void BLF_lang_encoding(const char *str)
278 {
279         BLI_strncpy(global_encoding_name, str, sizeof(global_encoding_name));
280         /* bind_textdomain_codeset(TEXT_DOMAIN_NAME, encoding_name); */
281 }
282
283 #else /* ! WITH_INTERNATIONAL */
284
285 void BLF_lang_init(void)
286 {
287         return;
288 }
289
290 void BLF_lang_encoding(const char *str)
291 {
292         (void)str;
293         return;
294 }
295
296 void BLF_lang_set(const char *str)
297 {
298         (void)str;
299         return;
300 }
301
302 #endif /* WITH_INTERNATIONAL */