Adding FA (Persian - Farsi) language to po + flipping Arabic name in the Language...
[blender.git] / source / blender / blenfont / intern / blf_lang.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  * 
23  * Contributor(s): Blender Foundation.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenfont/intern/blf_lang.c
29  *  \ingroup blf
30  */
31
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "BLF_api.h"
38
39 #ifdef WITH_INTERNATIONAL
40
41 #include <locale.h>
42
43 #if defined (_WIN32)
44 #include <windows.h>
45 #endif
46
47 #include "libintl.h"
48
49 #include "DNA_userdef_types.h"
50
51 #include "DNA_listBase.h"
52 #include "DNA_vec_types.h"
53
54 #include "MEM_guardedalloc.h"
55
56 #include "BLI_linklist.h"       /* linknode */
57 #include "BLI_string.h"
58 #include "BLI_path_util.h"
59
60 #define DOMAIN_NAME "blender"
61 #define SYSTEM_ENCODING_DEFAULT "UTF-8"
62 #define FONT_SIZE_DEFAULT 12
63
64 /* locale options. */
65 static char global_messagepath[1024];
66 static char global_language[32];
67 static char global_encoding_name[32];
68
69 /* map from the rna_userdef.c:rna_def_userdef_system(BlenderRNA *brna):language_items */
70 static const char *locales[] = {
71         "", "",
72         "english", "en_US",
73         "japanese", "ja_JP",
74         "dutch", "nl_NL",
75         "italian", "it_IT",
76         "german", "de_DE",
77         "finnish", "fi_FI",
78         "swedish", "sv_SE",
79         "french", "fr_FR",
80         "spanish", "es_ES",
81         "catalan", "ca_AD",
82         "czech", "cs_CZ",
83         "ptb", "pt_BR",
84         "Chinese (Simplified)_China.1252", "zh_CN",
85         "Chinese (Traditional)_China.1252", "zh_TW",
86         "russian", "ru_RU",
87         "croatian", "hr_HR",
88         "serbian", "sr_RS",
89         "ukrainian", "uk_UA",
90         "polish", "pl_PL",
91         "romanian", "ro_RO",
92         "arabic", "ar_EG",
93         "bulgarian", "bg_BG",
94         "greek", "el_GR",
95         "korean", "ko_KR",
96         "nepali", "ne_NP",
97         "persian", "fa_PE",
98 };
99
100 void BLF_lang_init(void)
101 {
102         char *messagepath= BLI_get_folder(BLENDER_DATAFILES, "locale");
103         
104         BLI_strncpy(global_encoding_name, SYSTEM_ENCODING_DEFAULT, sizeof(global_encoding_name));
105         
106         if (messagepath)
107                 BLI_strncpy(global_messagepath, messagepath, sizeof(global_messagepath));
108         else
109                 global_messagepath[0]= '\0';
110         
111 }
112
113 /* XXX WARNING!!! IN osx somehow the previous function call jumps in this one??? (ton, ppc) */
114 void BLF_lang_set(const char *str)
115 {
116         char *locreturn;
117         const char *short_locale;
118         int ok= 1;
119 #if defined (_WIN32) && !defined(FREE_WINDOWS)
120         char *long_locale = locales[ 2 * U.language];
121 #endif
122
123         if((U.transopts&USER_DOTRANSLATE)==0)
124                 return;
125
126         if(str)
127                 short_locale = str;
128         else
129                 short_locale = locales[ 2 * U.language + 1];
130
131 #if defined (_WIN32) && !defined(FREE_WINDOWS)
132         if(short_locale) {
133                 char *envStr;
134
135                 if( U.language==0 )/* use system setting */
136                         envStr = BLI_sprintfN( "LANG=%s", getenv("LANG") );
137                 else
138                         envStr = BLI_sprintfN( "LANG=%s", short_locale );
139
140                 gettext_putenv(envStr);
141                 MEM_freeN(envStr);
142         }
143
144         locreturn= setlocale(LC_ALL, long_locale);
145
146         if (locreturn == NULL) {
147                 printf("Could not change locale to %s\n", long_locale);
148                 ok= 0;
149         }
150 #else
151         {
152                 const char *locale;
153                 static char default_locale[64]="\0";
154
155                 if(default_locale[0]==0) {
156                         char *env_language= getenv("LANGUAGE");
157
158                         if(env_language) {
159                                 char *s;
160
161                                 /* store defaul locale */
162                                 BLI_strncpy(default_locale, env_language, sizeof(default_locale));
163
164                                 /* use first language as default */
165                                 s= strchr(default_locale, ':');
166                                 if(s) s[0]= 0;
167                         }
168                 }
169
170                 if(short_locale[0])
171                         locale= short_locale;
172                 else
173                         locale= default_locale;
174
175                 BLI_setenv("LANG", locale);
176                 BLI_setenv("LANGUAGE", locale);
177
178                 locreturn= setlocale(LC_ALL, locale);
179
180                 if (locreturn == NULL) {
181                         char *short_locale_utf8= BLI_sprintfN("%s.UTF-8", short_locale);
182
183                         locreturn= setlocale(LC_ALL, short_locale_utf8);
184
185                         if (locreturn == NULL) {
186                                 printf("Could not change locale to %s nor %s\n", short_locale, short_locale_utf8);
187                                 ok= 0;
188                         }
189
190                         MEM_freeN(short_locale_utf8);
191                 }
192         }
193 #endif
194
195         if(ok) {
196                 //printf("Change locale to %s\n", locreturn );
197                 BLI_strncpy(global_language, locreturn, sizeof(global_language));
198         }
199
200         setlocale(LC_NUMERIC, "C");
201
202         textdomain(DOMAIN_NAME);
203         bindtextdomain(DOMAIN_NAME, global_messagepath);
204         bind_textdomain_codeset(DOMAIN_NAME, global_encoding_name);
205 }
206
207 void BLF_lang_encoding(const char *str)
208 {
209         BLI_strncpy(global_encoding_name, str, sizeof(global_encoding_name));
210         /* bind_textdomain_codeset(DOMAIN_NAME, encoding_name); */
211 }
212
213 #else /* ! WITH_INTERNATIONAL */
214
215 void BLF_lang_init(void)
216 {
217         return;
218 }
219
220 void BLF_lang_encoding(const char *str)
221 {
222         (void)str;
223         return;
224 }
225
226 void BLF_lang_set(const char *str)
227 {
228         (void)str;
229         return;
230 }
231
232 #endif /* WITH_INTERNATIONAL */