Blender V5.3
blt_lang.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstdlib>
12#include <cstring>
13#include <string>
14
15#ifndef _WIN32
16# include <clocale>
17#endif
18
19#include "RNA_types.hh"
20
21#include "BLT_lang.hh" /* own include */
22#include "BLT_translation.hh"
23
24#include "BLI_path_utils.hh"
25#include "BLI_string.hh"
26#include "BLI_utildefines.hh"
27
28#include "BKE_appdir.hh"
29
30#include "DNA_userdef_types.h"
31
32#include "MEM_guardedalloc.h"
33
34#include "CLG_log.h"
35
36#ifdef WITH_INTERNATIONAL
37# include "BLI_fileops.hh"
38# include "BLI_linklist.hh"
39# include "messages.hh"
40#endif
41
42namespace blender {
43
44static CLG_LogRef LOG = {"translation"};
45
46#ifdef WITH_INTERNATIONAL
47
48/* Locale options. */
49static const char **locales = nullptr;
50static int num_locales = 0;
51static EnumPropertyItem *locales_menu = nullptr;
52static int num_locales_menu = 0;
53
54static void free_locales()
55{
56 if (locales_menu) {
57 int idx = num_locales_menu - 1; /* Last item does not need to be freed! */
58 while (idx--) {
59 MEM_delete(locales_menu[idx].identifier); /* Also frees locales's relevant value! */
60 MEM_delete(locales_menu[idx].name);
61 MEM_delete(locales_menu[idx].description);
62 }
63 }
64 MEM_SAFE_DELETE(locales_menu);
65 /* Allocated strings in #locales are shared with #locales_menu[idx].identifier, which are already
66 * freed above, or are static strings. */
67 MEM_SAFE_DELETE(locales);
68 num_locales = num_locales_menu = 0;
69}
70
71static void fill_locales()
72{
73 std::optional<std::string> languages_path = BKE_appdir_folder_id(BLENDER_DATAFILES, "locale");
74 if (!languages_path.has_value()) {
75 CLOG_WARN(&LOG, "'locale' data path for translations not found");
76 return;
77 }
78
79 free_locales();
80
81 char languages[FILE_MAX];
82 BLI_path_join(languages, FILE_MAX, languages_path->c_str(), "languages");
83
85 LinkNode *line = lines;
86 int idx = 0;
87
88 /* This whole "parsing" code is a bit weak, in that it expects strictly formatted input file...
89 * Should not be a problem, though, as this file is script-generated! */
90
91 /* First loop to find highest locale ID */
92 while (line) {
93 int t;
94 char *str = (char *)line->link;
95 if (ELEM(str[0], '#', '\0')) {
96 line = line->next;
97 continue; /* Comment or void... */
98 }
99 t = atoi(str);
100 if (t >= num_locales) {
101 num_locales = t + 1;
102 }
103 num_locales_menu++;
104 line = line->next;
105 }
106 num_locales_menu++; /* The "closing" void item... */
107
108 /* And now, build locales and locale_menu! */
109 locales_menu = MEM_new_array_zeroed<EnumPropertyItem>(num_locales_menu, __func__);
110 line = lines;
111 /* Do not allocate locales with zero-sized mem,
112 * as LOCALE macro uses nullptr locales as invalid marker! */
113 if (num_locales > 0) {
114 locales = MEM_new_array_zeroed<const char *>(num_locales, __func__);
115 while (line) {
116 const char *loc, *desc, *sep1, *sep2, *sep3;
117
118 char *str = (char *)line->link;
119 if (ELEM(str[0], '#', '\0')) {
120 line = line->next;
121 continue;
122 }
123
124 const int id = atoi(str);
125 sep1 = strchr(str, ':');
126 if (sep1) {
127 sep1++;
128 sep2 = strchr(sep1, ':');
129 if (sep2) {
130 locales_menu[idx].value = id;
131 locales_menu[idx].icon = 0;
132 locales_menu[idx].name = BLI_strdupn(sep1, sep2 - sep1);
133
134 sep2++;
135 sep3 = strchr(sep2, ':');
136
137 if (sep3) {
138 locales_menu[idx].identifier = loc = BLI_strdupn(sep2, sep3 - sep2);
139
140 sep3++;
141 desc = BLI_sprintfN("Locale code: %s. Translation progress: %s", loc, sep3);
142 }
143 else {
144 locales_menu[idx].identifier = loc = BLI_strdup(sep2);
145 desc = BLI_strdup(sep2);
146 }
147
148 if (id == 0) {
149 /* The DEFAULT/Automatic item... */
150 if (loc[0] != '\0') {
151 MEM_delete(desc); /* Not used here. */
152 locales[id] = "";
153 /* Keep this tip in sync with the one in rna_userdef
154 * (rna_enum_language_default_items). */
155 locales_menu[idx].description = BLI_strdup(
156 "Automatically choose the system-defined language if available, or fall-back to "
157 "English (US)");
158 }
159 /* Menu "label", not to be stored in locales!
160 * NOTE: Not used since Blender 4.5. */
161 else {
162 locales_menu[idx].description = desc;
163 }
164 }
165 else {
166 locales[id] = loc;
167 locales_menu[idx].description = desc;
168 }
169 idx++;
170 }
171 }
172
173 line = line->next;
174 }
175 }
176
177 /* Add closing item to menu! */
178 locales_menu[idx].identifier = nullptr;
179 locales_menu[idx].value = locales_menu[idx].icon = 0;
180 locales_menu[idx].name = locales_menu[idx].description = "";
181
182 BLI_file_free_lines(lines);
183}
184#endif /* WITH_INTERNATIONAL */
185
187{
188#ifdef WITH_INTERNATIONAL
189 return locales_menu;
190#else
191 return nullptr;
192#endif
193}
194
196{
197/* Make sure LANG is correct and wouldn't cause #std::runtime_error. */
198#ifndef _WIN32
199 /* TODO(sergey): This code only ensures LANG is set properly, so later when
200 * Cycles will try to use file system API from boost there will be no runtime
201 * exception generated by #std::locale() which _requires_ having proper LANG
202 * set in the environment.
203 *
204 * Ideally we also need to ensure LC_ALL, LC_MESSAGES and others are also
205 * set to a proper value, but currently it's not a huge deal and doesn't
206 * cause any headache.
207 *
208 * Would also be good to find nicer way to check if LANG is correct.
209 */
210 const char *lang = BLI_getenv("LANG");
211 if (lang != nullptr) {
212 char *old_locale = setlocale(LC_ALL, nullptr);
213 /* Make a copy so subsequent #setlocale() doesn't interfere. */
214 old_locale = BLI_strdup(old_locale);
215 if (setlocale(LC_ALL, lang) == nullptr) {
216 setenv("LANG", "C", 1);
217 CLOG_WARN(&LOG, "Falling back to standard locale (\"C\")");
218 }
219 setlocale(LC_ALL, old_locale);
220 MEM_delete(old_locale);
221 }
222#endif
223
224#ifdef WITH_INTERNATIONAL
225 fill_locales();
226#endif
227}
228
230{
231#ifdef WITH_INTERNATIONAL
232 locale::free();
233 free_locales();
234#endif
235}
236
237#ifdef WITH_INTERNATIONAL
238static uint lang_from_userdef()
239{
240 const uint language = uint(U.language);
241 if ((language >= ULANGUAGE_AUTO) && (language < num_locales)) {
242 return language;
243 }
244 return uint(ULANGUAGE_ENGLISH);
245}
246#endif
247
248#ifdef WITH_INTERNATIONAL
249# define ULANGUAGE lang_from_userdef()
250# define LOCALE(_id) (locales ? locales[(_id)] : "")
251#endif
252
253void BLT_lang_set(const char *str)
254{
255#ifdef WITH_INTERNATIONAL
256 int ulang = ULANGUAGE;
257 std::string locale_name = str ? str : LOCALE(ulang);
258
259 /* #locale assumes UTF8, no need to put it in the name. */
260 const std::optional<std::string> messagepath = BKE_appdir_folder_id(BLENDER_DATAFILES, "locale");
261 locale::init(locale_name, {TEXT_DOMAIN_NAME}, {messagepath.value_or("")});
262
263#else
264 (void)str;
265#endif
266}
267
268const char *BLT_lang_get()
269{
270#ifdef WITH_INTERNATIONAL
271 if (BLT_translate()) {
272 const char *locale = LOCALE(ULANGUAGE);
273 if (locale[0] == '\0') {
274 /* Default locale, we have to find which one we are actually using! */
276 }
277 return locale;
278 }
279 return "en_US"; /* Kind of default locale in Blender when no translation enabled. */
280#else
281 return "";
282#endif
283}
284
285#undef LOCALE
286#undef ULANGUAGE
287
289 char **language,
290 char **country,
291 char **variant,
292 char **language_country,
293 char **language_variant)
294{
295 const char *m1, *m2;
296 char *_t = nullptr;
297
298 m1 = strchr(locale, '_');
299 m2 = strchr(locale, '@');
300
301 if (language || language_variant) {
302 if (m1 || m2) {
303 _t = m1 ? BLI_strdupn(locale, m1 - locale) : BLI_strdupn(locale, m2 - locale);
304 if (language) {
305 *language = _t;
306 }
307 }
308 else if (language) {
309 *language = BLI_strdup(locale);
310 }
311 }
312 if (country) {
313 if (m1) {
314 *country = m2 ? BLI_strdupn(m1 + 1, m2 - (m1 + 1)) : BLI_strdup(m1 + 1);
315 }
316 else {
317 *country = nullptr;
318 }
319 }
320 if (variant) {
321 if (m2) {
322 *variant = BLI_strdup(m2 + 1);
323 }
324 else {
325 *variant = nullptr;
326 }
327 }
328 if (language_country) {
329 if (m1) {
330 *language_country = m2 ? BLI_strdupn(locale, m2 - locale) : BLI_strdup(locale);
331 }
332 else {
333 *language_country = nullptr;
334 }
335 }
336 if (language_variant) {
337 if (m2) {
338 *language_variant = m1 ? BLI_strdupcat(_t, m2) : BLI_strdup(locale);
339 }
340 else {
341 *language_variant = nullptr;
342 }
343 }
344 if (_t && !language) {
345 MEM_delete(_t);
346 }
347}
348
349} // namespace blender
File and directory operations.
#define FILE_MAX
#define BLI_path_join(...)
#define TEXT_DOMAIN_NAME
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
Read Blender memory allocation and freeing.
#define U
#define ELEM(...)
#define str(s)
#define LOG(level)
Definition log.h:97
const char * full_name()
Definition messages.cc:638
void init(const StringRef locale_full_name, const Vector< std::string > &domains, const Vector< std::string > &paths)
Definition messages.cc:599
BMesh Iterators.
unsigned int uint
Definition BKE_text.h:55
void BLT_lang_set(const char *)
Definition blt_lang.cc:253
char * BLI_strdupcat(const char *__restrict str1, const char *__restrict str2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
char * BLI_sprintfN(const char *__restrict format,...) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
struct LinkNode * BLI_file_read_as_lines(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:591
std::optional< std::string > BKE_appdir_folder_id(int folder_id, const char *subfolder) ATTR_WARN_UNUSED_RESULT
Definition appdir.cc:762
void BLT_lang_init()
Definition blt_lang.cc:195
void BLT_lang_free()
Definition blt_lang.cc:229
bool BLT_translate()
void BLT_lang_locale_explode(const char *locale, char **language, char **country, char **variant, char **language_country, char **language_variant)
Definition blt_lang.cc:288
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
const char * BLT_lang_get()
Definition blt_lang.cc:268
static CLG_LogRef LOG
Definition logging.cpp:12
const EnumPropertyItem * BLT_lang_RNA_enum_properties()
Definition blt_lang.cc:186
@ BLENDER_DATAFILES
void BLI_file_free_lines(struct LinkNode *lines)
Definition storage.cc:637
const char * BLI_getenv(const char *env) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
PipelineCompute lines(lines_main)