Blender V2.61 - r43446

blf_lang.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version. 
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2008 Blender Foundation.
00019  * All rights reserved.
00020  * 
00021  * Contributor(s): Blender Foundation.
00022  *
00023  * ***** END GPL LICENSE BLOCK *****
00024  */
00025 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 
00035 #include "BKE_global.h"
00036 
00037 #include "BLF_api.h"
00038 
00039 #include "BLF_translation.h" /* own include */
00040 
00041 #ifdef WITH_INTERNATIONAL
00042 
00043 #include <locale.h>
00044 
00045 #if defined (_WIN32)
00046 #include <windows.h>
00047 #endif
00048 
00049 #include "libintl.h"
00050 
00051 #include "DNA_userdef_types.h"
00052 
00053 #include "DNA_listBase.h"
00054 #include "DNA_vec_types.h"
00055 
00056 #include "MEM_guardedalloc.h"
00057 
00058 #include "BLI_linklist.h"   /* linknode */
00059 #include "BLI_string.h"
00060 #include "BLI_utildefines.h"
00061 #include "BLI_path_util.h"
00062 
00063 #define SYSTEM_ENCODING_DEFAULT "UTF-8"
00064 #define FONT_SIZE_DEFAULT 12
00065 
00066 /* locale options. */
00067 static char global_messagepath[1024];
00068 static char global_language[32];
00069 static char global_encoding_name[32];
00070 
00071 /* map from the rna_userdef.c:rna_def_userdef_system(BlenderRNA *brna):language_items */
00072 static const char *locales[] = {
00073     "", "",
00074     "english", "en_US",
00075     "japanese", "ja_JP",
00076     "dutch", "nl_NL",
00077     "italian", "it_IT",
00078     "german", "de_DE",
00079     "finnish", "fi_FI",
00080     "swedish", "sv_SE",
00081     "french", "fr_FR",
00082     "spanish", "es",
00083     "catalan", "ca_AD",
00084     "czech", "cs_CZ",
00085     "ptb", "pt_BR",
00086 #if defined (_WIN32) && !defined(FREE_WINDOWS)
00087     "Chinese (Simplified)_China.1252", "zh_CN",
00088     "Chinese (Traditional)_China.1252", "zh_TW",
00089 #else
00090     "chs", "zh_CN",
00091     "cht", "zh_TW",
00092 #endif
00093     "russian", "ru_RU",
00094     "croatian", "hr_HR",
00095     "serbian", "sr_RS",
00096     "ukrainian", "uk_UA",
00097     "polish", "pl_PL",
00098     "romanian", "ro_RO",
00099     "arabic", "ar_EG",
00100     "bulgarian", "bg_BG",
00101     "greek", "el_GR",
00102     "korean", "ko_KR",
00103     "nepali", "ne_NP",
00104     "persian", "fa_PE",
00105     "indonesian", "id_ID",
00106     "serbian (latin)", "sr_RS@latin",
00107     "kyrgyz", "ky",
00108 };
00109 
00110 void BLF_lang_init(void)
00111 {
00112     char *messagepath= BLI_get_folder(BLENDER_DATAFILES, "locale");
00113     
00114     BLI_strncpy(global_encoding_name, SYSTEM_ENCODING_DEFAULT, sizeof(global_encoding_name));
00115     
00116     if (messagepath) {
00117         BLI_strncpy(global_messagepath, messagepath, sizeof(global_messagepath));
00118     }
00119     else {
00120         printf("%s: 'locale' data path for translations not found, continuing\n", __func__);
00121         global_messagepath[0]= '\0';
00122     }
00123     
00124 }
00125 
00126 /* get LANG/LANGUAGE environment variable */
00127 static void get_language_variable(const char *varname, char *var, int maxlen)
00128 {
00129     char *env= getenv(varname);
00130 
00131     if(env) {
00132         char *s;
00133 
00134         /* store defaul locale */
00135         BLI_strncpy(var, env, maxlen);
00136 
00137         /* use first language as default */
00138         s= strchr(var, ':');
00139         if(s)
00140             s[0]= 0;
00141     }
00142 }
00143 
00144 /* get language to be used based on locale(which might be empty when using default language) and
00145  * LANG environment variable
00146  */
00147 static void get_language(const char *locale, const char *lang, char *language, int maxlen)
00148 {
00149     if(locale[0]) {
00150         BLI_strncpy(language, locale, maxlen);
00151     }
00152     else {
00153         char *s;
00154 
00155         BLI_strncpy(language, lang, maxlen);
00156 
00157         s= strchr(language, '.');
00158         if(s)
00159             s[0]= 0;
00160     }
00161 }
00162 
00163 /* XXX WARNING!!! IN osx somehow the previous function call jumps in this one??? (ton, ppc) */
00164 void BLF_lang_set(const char *str)
00165 {
00166     char *locreturn;
00167     const char *short_locale;
00168     int ok= 1;
00169     const char *long_locale = locales[2 * U.language];
00170 
00171     if((U.transopts&USER_DOTRANSLATE)==0)
00172         return;
00173 
00174     if(str)
00175         short_locale = str;
00176     else
00177         short_locale = locales[ 2 * U.language + 1];
00178 
00179 #if defined (_WIN32) && !defined(FREE_WINDOWS)
00180     if(short_locale) {
00181         char *envStr;
00182 
00183         if( U.language==0 )/* use system setting */
00184             envStr = BLI_sprintfN( "LANG=%s", getenv("LANG") );
00185         else
00186             envStr = BLI_sprintfN( "LANG=%s", short_locale );
00187 
00188         gettext_putenv(envStr);
00189         MEM_freeN(envStr);
00190     }
00191 
00192     locreturn= setlocale(LC_ALL, long_locale);
00193 
00194     if (locreturn == NULL) {
00195         if(G.f & G_DEBUG)
00196             printf("Could not change locale to %s\n", long_locale);
00197 
00198         ok= 0;
00199     }
00200 #else
00201     {
00202         static char default_lang[64]="\0";
00203         static char default_language[64]="\0";
00204 
00205         if(default_lang[0]==0)
00206             get_language_variable("LANG", default_lang, sizeof(default_lang));
00207 
00208         if(default_language[0]==0)
00209             get_language_variable("LANGUAGE", default_language, sizeof(default_language));
00210 
00211         if(short_locale[0]) {
00212             if(G.f & G_DEBUG)
00213                 printf("Setting LANG= and LANGUAGE to %s\n", short_locale);
00214 
00215             BLI_setenv("LANG", short_locale);
00216             BLI_setenv("LANGUAGE", short_locale);
00217         }
00218         else {
00219             if(G.f & G_DEBUG)
00220                 printf("Setting LANG=%s and LANGUAGE=%s\n", default_lang, default_language);
00221 
00222             BLI_setenv("LANG", default_lang);
00223             BLI_setenv("LANGUAGE", default_language);
00224         }
00225 
00226         locreturn= setlocale(LC_ALL, short_locale);
00227 
00228         if(locreturn == NULL) {
00229             char *short_locale_utf8= NULL;
00230 
00231             if(short_locale[0]) {
00232                 short_locale_utf8= BLI_sprintfN("%s.UTF-8", short_locale);
00233                 locreturn= setlocale(LC_ALL, short_locale_utf8);
00234             }
00235 
00236             if (locreturn == NULL) {
00237                 char language[65];
00238 
00239                 get_language(long_locale, default_lang, language, sizeof(language));
00240 
00241                 if(G.f & G_DEBUG) {
00242                     if(short_locale[0])
00243                         printf("Could not change locale to %s nor %s\n", short_locale, short_locale_utf8);
00244                     else
00245                         printf("Could not reset locale\n");
00246 
00247                     printf("Fallback to LANG=%s and LANGUAGE=%s\n", default_lang, language);
00248                 }
00249 
00250                 /* fallback to default settings */
00251                 BLI_setenv("LANG", default_lang);
00252                 BLI_setenv("LANGUAGE", language);
00253 
00254                 locreturn= setlocale(LC_ALL, "");
00255 
00256                 ok= 0;
00257             }
00258 
00259             if(short_locale_utf8)
00260                 MEM_freeN(short_locale_utf8);
00261         }
00262     }
00263 #endif
00264 
00265     if(ok) {
00266         //printf("Change locale to %s\n", locreturn );
00267         BLI_strncpy(global_language, locreturn, sizeof(global_language));
00268     }
00269 
00270     setlocale(LC_NUMERIC, "C");
00271 
00272     textdomain(TEXT_DOMAIN_NAME);
00273     bindtextdomain(TEXT_DOMAIN_NAME, global_messagepath);
00274     bind_textdomain_codeset(TEXT_DOMAIN_NAME, global_encoding_name);
00275 }
00276 
00277 void BLF_lang_encoding(const char *str)
00278 {
00279     BLI_strncpy(global_encoding_name, str, sizeof(global_encoding_name));
00280     /* bind_textdomain_codeset(TEXT_DOMAIN_NAME, encoding_name); */
00281 }
00282 
00283 #else /* ! WITH_INTERNATIONAL */
00284 
00285 void BLF_lang_init(void)
00286 {
00287     return;
00288 }
00289 
00290 void BLF_lang_encoding(const char *str)
00291 {
00292     (void)str;
00293     return;
00294 }
00295 
00296 void BLF_lang_set(const char *str)
00297 {
00298     (void)str;
00299     return;
00300 }
00301 
00302 #endif /* WITH_INTERNATIONAL */