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

blt_lang.c « intern « blentranslation « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 91e8a81aec06b830cc40830075bbc52a9c91919c (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 * 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.
 */

/** \file
 * \ingroup blt
 *
 * Main internationalization functions to set the locale and query available languages.
 */

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

#ifndef _WIN32
#  include <locale.h>
#endif

#include "RNA_types.h"

#include "BLT_lang.h" /* own include */
#include "BLT_translation.h"

#include "BLI_path_util.h"
#include "BLI_string.h"
#include "BLI_utildefines.h"

#include "BKE_appdir.h"

#include "IMB_thumbs.h"

#include "DNA_userdef_types.h"

#include "MEM_guardedalloc.h"

#ifdef WITH_INTERNATIONAL

#  include "BLI_fileops.h"
#  include "BLI_linklist.h"

#  include "boost_locale_wrapper.h"

/* Locale options. */
static const char **locales = NULL;
static int num_locales = 0;
static EnumPropertyItem *locales_menu = NULL;
static int num_locales_menu = 0;

static void free_locales(void)
{
  if (locales) {
    int idx = num_locales_menu - 1; /* Last item does not need to be freed! */
    while (idx--) {
      MEM_freeN((void *)locales_menu[idx].identifier);
      MEM_freeN((void *)locales_menu[idx].name);
      MEM_freeN((void *)locales_menu[idx].description); /* Also frees locales's relevant value! */
    }

    MEM_freeN((void *)locales);
    locales = NULL;
  }
  MEM_SAFE_FREE(locales_menu);
  num_locales = num_locales_menu = 0;
}

static void fill_locales(void)
{
  const char *const languages_path = BKE_appdir_folder_id(BLENDER_DATAFILES, "locale");
  char languages[FILE_MAX];
  LinkNode *lines = NULL, *line;
  char *str;
  int idx = 0;

  free_locales();

  BLI_join_dirfile(languages, FILE_MAX, languages_path, "languages");
  line = lines = BLI_file_read_as_lines(languages);

  /* This whole "parsing" code is a bit weak, in that it expects strictly formatted input file...
   * Should not be a problem, though, as this file is script-generated! */

  /* First loop to find highest locale ID */
  while (line) {
    int t;
    str = (char *)line->link;
    if (ELEM(str[0], '#', '\0')) {
      line = line->next;
      continue; /* Comment or void... */
    }
    t = atoi(str);
    if (t >= num_locales) {
      num_locales = t + 1;
    }
    num_locales_menu++;
    line = line->next;
  }
  num_locales_menu++; /* The "closing" void item... */

  /* And now, build locales and locale_menu! */
  locales_menu = MEM_callocN(num_locales_menu * sizeof(EnumPropertyItem), __func__);
  line = lines;
  /* Do not allocate locales with zero-sized mem,
   * as LOCALE macro uses NULL locales as invalid marker! */
  if (num_locales > 0) {
    locales = MEM_callocN(num_locales * sizeof(char *), __func__);
    while (line) {
      int id;
      char *loc, *sep1, *sep2, *sep3;

      str = (char *)line->link;
      if (ELEM(str[0], '#', '\0')) {
        line = line->next;
        continue;
      }

      id = atoi(str);
      sep1 = strchr(str, ':');
      if (sep1) {
        sep1++;
        sep2 = strchr(sep1, ':');
        if (sep2) {
          locales_menu[idx].value = id;
          locales_menu[idx].icon = 0;
          locales_menu[idx].name = BLI_strdupn(sep1, sep2 - sep1);

          sep2++;
          sep3 = strchr(sep2, ':');

          if (sep3) {
            locales_menu[idx].identifier = loc = BLI_strdupn(sep2, sep3 - sep2);
          }
          else {
            locales_menu[idx].identifier = loc = BLI_strdup(sep2);
          }

          if (id == 0) {
            /* The DEFAULT/Automatic item... */
            if (BLI_strnlen(loc, 2)) {
              locales[id] = "";
              /* Keep this tip in sync with the one in rna_userdef
               * (rna_enum_language_default_items). */
              locales_menu[idx].description = BLI_strdup(
                  "Automatically choose system's defined language "
                  "if available, or fall-back to English");
            }
            /* Menu "label", not to be stored in locales! */
            else {
              locales_menu[idx].description = BLI_strdup("");
            }
          }
          else {
            locales[id] = locales_menu[idx].description = BLI_strdup(loc);
          }
          idx++;
        }
      }

      line = line->next;
    }
  }

  /* Add closing item to menu! */
  locales_menu[idx].identifier = NULL;
  locales_menu[idx].value = locales_menu[idx].icon = 0;
  locales_menu[idx].name = locales_menu[idx].description = "";

  BLI_file_free_lines(lines);
}
#endif /* WITH_INTERNATIONAL */

EnumPropertyItem *BLT_lang_RNA_enum_properties(void)
{
#ifdef WITH_INTERNATIONAL
  return locales_menu;
#else
  return NULL;
#endif
}

void BLT_lang_init(void)
{
#ifdef WITH_INTERNATIONAL
  const char *const messagepath = BKE_appdir_folder_id(BLENDER_DATAFILES, "locale");
#endif

  /* Make sure LANG is correct and wouldn't cause #std::runtime_error. */
#ifndef _WIN32
  /* TODO(sergey): This code only ensures LANG is set properly, so later when
   * Cycles will try to use file system API from boost there will be no runtime
   * exception generated by #std::locale() which _requires_ having proper LANG
   * set in the environment.
   *
   * Ideally we also need to ensure LC_ALL, LC_MESSAGES and others are also
   * set to a proper value, but currently it's not a huge deal and doesn't
   * cause any headache.
   *
   * Would also be good to find nicer way to check if LANG is correct.
   */
  const char *lang = BLI_getenv("LANG");
  if (lang != NULL) {
    char *old_locale = setlocale(LC_ALL, NULL);
    /* Make a copy so subsequent #setlocale() doesn't interfere. */
    old_locale = BLI_strdup(old_locale);
    if (setlocale(LC_ALL, lang) == NULL) {
      setenv("LANG", "C", 1);
      printf("Warning: Falling back to the standard locale (\"C\")\n");
    }
    setlocale(LC_ALL, old_locale);
    MEM_freeN(old_locale);
  }
#endif

#ifdef WITH_INTERNATIONAL
  if (messagepath) {
    bl_locale_init(messagepath, TEXT_DOMAIN_NAME);
    fill_locales();
  }
  else {
    printf("%s: 'locale' data path for translations not found, continuing\n", __func__);
  }
#else
#endif
}

void BLT_lang_free(void)
{
#ifdef WITH_INTERNATIONAL
  free_locales();
#else
#endif
}

#ifdef WITH_INTERNATIONAL
#  define ULANGUAGE \
    ((U.language >= ULANGUAGE_AUTO && U.language < num_locales) ? U.language : ULANGUAGE_ENGLISH)
#  define LOCALE(_id) (locales ? locales[(_id)] : "")
#endif

void BLT_lang_set(const char *str)
{
#ifdef WITH_INTERNATIONAL
  int ulang = ULANGUAGE;
  const char *short_locale = str ? str : LOCALE(ulang);
  const char *short_locale_utf8 = NULL;

  /* We want to avoid locales like '.UTF-8'! */
  if (short_locale[0]) {
    /* Hooray! Encoding needs to be placed *before* variant! */
    char *variant = strchr(short_locale, '@');
    if (variant) {
      char *locale = BLI_strdupn(short_locale, variant - short_locale);
      short_locale_utf8 = BLI_sprintfN("%s.UTF-8%s", locale, variant);
      MEM_freeN(locale);
    }
    else {
      short_locale_utf8 = BLI_sprintfN("%s.UTF-8", short_locale);
    }
    bl_locale_set(short_locale_utf8);
    MEM_freeN((void *)short_locale_utf8);
  }
  else {
    bl_locale_set(short_locale);
  }
#else
  (void)str;
#endif
  IMB_thumb_clear_translations();
}

/* Get the current locale (short code, e.g. es_ES). */
const char *BLT_lang_get(void)
{
#ifdef WITH_INTERNATIONAL
  if (BLT_translate()) {
    const char *locale = LOCALE(ULANGUAGE);
    if (locale[0] == '\0') {
      /* Default locale, we have to find which one we are actually using! */
      locale = bl_locale_get();
    }
    return locale;
  }
  return "en_US"; /* Kind of default locale in Blender when no translation enabled. */
#else
  return "";
#endif
}

#undef LOCALE
#undef ULANGUAGE

/**
 * Get locale's elements (if relevant pointer is not NULL and element actually exists, e.g.
 * if there is no variant,
 * *variant and *language_variant will always be NULL).
 * Non-null elements are always MEM_mallocN'ed, it's the caller's responsibility to free them.
 *
 * \note Keep that one always available, you never know,
 * may become useful even in no #WITH_INTERNATIONAL context.
 */
void BLT_lang_locale_explode(const char *locale,
                             char **language,
                             char **country,
                             char **variant,
                             char **language_country,
                             char **language_variant)
{
  char *m1, *m2, *_t = NULL;

  m1 = strchr(locale, '_');
  m2 = strchr(locale, '@');

  if (language || language_variant) {
    if (m1 || m2) {
      _t = m1 ? BLI_strdupn(locale, m1 - locale) : BLI_strdupn(locale, m2 - locale);
      if (language) {
        *language = _t;
      }
    }
    else if (language) {
      *language = BLI_strdup(locale);
    }
  }
  if (country) {
    if (m1) {
      *country = m2 ? BLI_strdupn(m1 + 1, m2 - (m1 + 1)) : BLI_strdup(m1 + 1);
    }
    else {
      *country = NULL;
    }
  }
  if (variant) {
    if (m2) {
      *variant = BLI_strdup(m2 + 1);
    }
    else {
      *variant = NULL;
    }
  }
  if (language_country) {
    if (m1) {
      *language_country = m2 ? BLI_strdupn(locale, m2 - locale) : BLI_strdup(locale);
    }
    else {
      *language_country = NULL;
    }
  }
  if (language_variant) {
    if (m2) {
      *language_variant = m1 ? BLI_strdupcat(_t, m2) : BLI_strdup(locale);
    }
    else {
      *language_variant = NULL;
    }
  }
  if (_t && !language) {
    MEM_freeN(_t);
  }
}

/* Note that "lang" here is the _output_ display language. We used to restrict
 * IME for keyboard _input_ language because our multilingual font was only used
 * when some output languages were selected. That font is used all the time now. */
bool BLT_lang_is_ime_supported(void)
{
#ifdef WITH_INPUT_IME
  return true;
#else
  return false;
#endif
}