Welcome to mirror list, hosted at ThFree Co, Russian Federation.

blf_lang.c « intern « blenfont « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b694f1fafa7af7c3043dd9dd139ae91808c869a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2008 Blender Foundation.
 * All rights reserved.
 * 
 * Contributor(s): Blender Foundation.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/blenfont/intern/blf_lang.c
 *  \ingroup blf
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "BKE_global.h"

#include "BLF_api.h"

#include "BLF_translation.h" /* own include */

#ifdef WITH_INTERNATIONAL

#include <locale.h>

#if defined(_WIN32)
#include <windows.h>
#endif

#include "libintl.h"

#include "DNA_userdef_types.h"

#include "DNA_listBase.h"
#include "DNA_vec_types.h"

#include "MEM_guardedalloc.h"

#include "BLI_linklist.h" /* linknode */
#include "BLI_string.h"
#include "BLI_utildefines.h"
#include "BLI_path_util.h"

#define SYSTEM_ENCODING_DEFAULT "UTF-8"
#define FONT_SIZE_DEFAULT 12

/* Locale options. */
static char global_messagepath[1024];
static char global_language[32];
static char global_encoding_name[32];

/* Map from the rna_userdef.c:rna_def_userdef_system(BlenderRNA *brna):language_items */
static const char *locales[] = {
	"", "",
	"english", "en_US",
	"japanese", "ja_JP",
	"dutch", "nl_NL",
	"italian", "it_IT",
	"german", "de_DE",
	"finnish", "fi_FI",
	"swedish", "sv_SE",
	"french", "fr_FR",
	"spanish", "es",
	"catalan", "ca_AD",
	"czech", "cs_CZ",
	"portuguese", "pt_PT",
#if defined(_WIN32) && !defined(FREE_WINDOWS)
	"Chinese (Simplified)_China.1252", "zh_CN",
	"Chinese (Traditional)_China.1252", "zh_TW",
#else
	"chs", "zh_CN",
	"cht", "zh_TW",
#endif
	"russian", "ru_RU",
	"croatian", "hr_HR",
	"serbian", "sr_RS",
	"ukrainian", "uk_UA",
	"polish", "pl_PL",
	"romanian", "ro_RO",
	"arabic", "ar_EG",
	"bulgarian", "bg_BG",
	"greek", "el_GR",
	"korean", "ko_KR",
	"nepali", "ne_NP",
	"persian", "fa_IR",
	"indonesian", "id_ID",
	"serbian (latin)", "sr_RS@latin",
	"kyrgyz", "ky_KG",
	"turkish", "tr_TR",
	"hungarian", "hu_HU",
	"portuguese-brazilian", "pt_BR",
	"hebrew", "he_IL",
};

void BLF_lang_init(void)
{
	char *messagepath = BLI_get_folder(BLENDER_DATAFILES, "locale");

	BLI_strncpy(global_encoding_name, SYSTEM_ENCODING_DEFAULT, sizeof(global_encoding_name));

	if (messagepath) {
		BLI_strncpy(global_messagepath, messagepath, sizeof(global_messagepath));
	}
	else {
		printf("%s: 'locale' data path for translations not found, continuing\n", __func__);
		global_messagepath[0] = '\0';
	}
}

/* Get LANG/LANGUAGE environment variable. */
static void get_language_variable(const char *varname, char *var, const size_t maxlen)
{
	char *env = getenv(varname);

	if (env) {
		char *s;

		/* Store defaul locale. */
		BLI_strncpy(var, env, maxlen);

		/* Use first language as default. */
		s = strchr(var, ':');
		if (s)
			s[0] = 0;
	}
}

/* Get language to be used based on locale (which might be empty when using default language) and
 * LANG environment variable.
 */
static void get_language(const char *locale, const char *lang, char *language, const size_t maxlen)
{
	if (locale[0]) {
		BLI_strncpy(language, locale, maxlen);
	}
	else {
		char *s;

		BLI_strncpy(language, lang, maxlen);

		s = strchr(language, '.');
		if (s)
			s[0] = 0;
	}
}

/* XXX WARNING!!! In osx somehow the previous function call jumps in this one??? (ton, ppc) */
void BLF_lang_set(const char *str)
{
	char *locreturn;
	const char *short_locale;
	int ok = 1;
	const char *long_locale = locales[2 * U.language];

	if ((U.transopts & USER_DOTRANSLATE) == 0)
		return;

	if (str)
		short_locale = str;
	else
		short_locale = locales[2 * U.language + 1];

#if defined(_WIN32) && !defined(FREE_WINDOWS)
	if (short_locale) {
		char *envStr;

		if (U.language == 0) /* Use system setting. */
			envStr = BLI_sprintfN("LANG=%s", getenv("LANG"));
		else
			envStr = BLI_sprintfN("LANG=%s", short_locale);

		gettext_putenv(envStr);
		MEM_freeN(envStr);
	}

	locreturn = setlocale(LC_ALL, long_locale);

	if (locreturn == NULL) {
		if (G.debug & G_DEBUG)
			printf("Could not change locale to %s\n", long_locale);

		ok = 0;
	}
#else
	{
		static char default_lang[64] = "\0";
		static char default_language[64] = "\0";

		if (default_lang[0] == 0)
			get_language_variable("LANG", default_lang, sizeof(default_lang));

		if (default_language[0] == 0)
			get_language_variable("LANGUAGE", default_language, sizeof(default_language));

		if (short_locale[0]) {
			if (G.debug & G_DEBUG)
				printf("Setting LANG= and LANGUAGE to %s\n", short_locale);

			BLI_setenv("LANG", short_locale);
			BLI_setenv("LANGUAGE", short_locale);
		}
		else {
			if (G.debug & G_DEBUG)
				printf("Setting LANG=%s and LANGUAGE=%s\n", default_lang, default_language);

			BLI_setenv("LANG", default_lang);
			BLI_setenv("LANGUAGE", default_language);
		}

		locreturn = setlocale(LC_ALL, short_locale);

		if (locreturn == NULL) {
			char *short_locale_utf8 = NULL;

			if (short_locale[0]) {
				short_locale_utf8 = BLI_sprintfN("%s.UTF-8", short_locale);
				locreturn = setlocale(LC_ALL, short_locale_utf8);
			}

			if (locreturn == NULL) {
				char language[65];

				get_language(long_locale, default_lang, language, sizeof(language));

				if (G.debug & G_DEBUG) {
					if (short_locale[0])
						printf("Could not change locale to %s nor %s\n", short_locale, short_locale_utf8);
					else
						printf("Could not reset locale\n");

					printf("Fallback to LANG=%s and LANGUAGE=%s\n", default_lang, language);
				}

				/* Fallback to default settings. */
				BLI_setenv("LANG", default_lang);
				BLI_setenv("LANGUAGE", language);

				locreturn = setlocale(LC_ALL, "");

				ok = 0;
			}

			if (short_locale_utf8)
				MEM_freeN(short_locale_utf8);
		}
	}
#endif

	if (ok) {
		/*printf("Change locale to %s\n", locreturn ); */
		BLI_strncpy(global_language, locreturn, sizeof(global_language));
	}

	setlocale(LC_NUMERIC, "C");

	textdomain(TEXT_DOMAIN_NAME);
	bindtextdomain(TEXT_DOMAIN_NAME, global_messagepath);
	bind_textdomain_codeset(TEXT_DOMAIN_NAME, global_encoding_name);
}

const char *BLF_lang_get(void)
{
	return locales[2 * U.language + 1];
}

void BLF_lang_encoding(const char *str)
{
	BLI_strncpy(global_encoding_name, str, sizeof(global_encoding_name));
	/* bind_textdomain_codeset(TEXT_DOMAIN_NAME, encoding_name); */
}

#else /* ! WITH_INTERNATIONAL */

void BLF_lang_init(void)
{
	return;
}

void BLF_lang_encoding(const char *str)
{
	(void)str;
	return;
}

void BLF_lang_set(const char *str)
{
	(void)str;
	return;
}

const char *BLF_lang_get(void)
{
	return "";
}

#endif /* WITH_INTERNATIONAL */