Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/string/translation_server.cpp
9903 views
1
/**************************************************************************/
2
/* translation_server.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "translation_server.h"
32
#include "translation_server.compat.inc"
33
34
#include "core/config/project_settings.h"
35
#include "core/io/resource_loader.h"
36
#include "core/os/os.h"
37
#include "core/string/locales.h"
38
39
Vector<TranslationServer::LocaleScriptInfo> TranslationServer::locale_script_info;
40
41
HashMap<String, String> TranslationServer::language_map;
42
HashMap<String, String> TranslationServer::script_map;
43
HashMap<String, String> TranslationServer::locale_rename_map;
44
HashMap<String, String> TranslationServer::country_name_map;
45
HashMap<String, String> TranslationServer::variant_map;
46
HashMap<String, String> TranslationServer::country_rename_map;
47
48
void TranslationServer::init_locale_info() {
49
// Init locale info.
50
language_map.clear();
51
int idx = 0;
52
while (language_list[idx][0] != nullptr) {
53
language_map[language_list[idx][0]] = String::utf8(language_list[idx][1]);
54
idx++;
55
}
56
57
// Init locale-script map.
58
locale_script_info.clear();
59
idx = 0;
60
while (locale_scripts[idx][0] != nullptr) {
61
LocaleScriptInfo info;
62
info.name = locale_scripts[idx][0];
63
info.script = locale_scripts[idx][1];
64
info.default_country = locale_scripts[idx][2];
65
Vector<String> supported_countries = String(locale_scripts[idx][3]).split(",", false);
66
for (int i = 0; i < supported_countries.size(); i++) {
67
info.supported_countries.insert(supported_countries[i]);
68
}
69
locale_script_info.push_back(info);
70
idx++;
71
}
72
73
// Init supported script list.
74
script_map.clear();
75
idx = 0;
76
while (script_list[idx][0] != nullptr) {
77
script_map[script_list[idx][1]] = String::utf8(script_list[idx][0]);
78
idx++;
79
}
80
81
// Init regional variant map.
82
variant_map.clear();
83
idx = 0;
84
while (locale_variants[idx][0] != nullptr) {
85
variant_map[locale_variants[idx][0]] = locale_variants[idx][1];
86
idx++;
87
}
88
89
// Init locale renames.
90
locale_rename_map.clear();
91
idx = 0;
92
while (locale_renames[idx][0] != nullptr) {
93
if (!String(locale_renames[idx][1]).is_empty()) {
94
locale_rename_map[locale_renames[idx][0]] = locale_renames[idx][1];
95
}
96
idx++;
97
}
98
99
// Init country names.
100
country_name_map.clear();
101
idx = 0;
102
while (country_names[idx][0] != nullptr) {
103
country_name_map[String(country_names[idx][0])] = String::utf8(country_names[idx][1]);
104
idx++;
105
}
106
107
// Init country renames.
108
country_rename_map.clear();
109
idx = 0;
110
while (country_renames[idx][0] != nullptr) {
111
if (!String(country_renames[idx][1]).is_empty()) {
112
country_rename_map[country_renames[idx][0]] = country_renames[idx][1];
113
}
114
idx++;
115
}
116
}
117
118
TranslationServer::Locale::operator String() const {
119
String out = language;
120
if (!script.is_empty()) {
121
out = out + "_" + script;
122
}
123
if (!country.is_empty()) {
124
out = out + "_" + country;
125
}
126
if (!variant.is_empty()) {
127
out = out + "_" + variant;
128
}
129
return out;
130
}
131
132
TranslationServer::Locale::Locale(const TranslationServer &p_server, const String &p_locale, bool p_add_defaults) {
133
// Replaces '-' with '_' for macOS style locales.
134
String univ_locale = p_locale.replace_char('-', '_');
135
136
// Extract locale elements.
137
Vector<String> locale_elements = univ_locale.get_slicec('@', 0).split("_");
138
language = locale_elements[0];
139
if (locale_elements.size() >= 2) {
140
if (locale_elements[1].length() == 4 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_lower_case(locale_elements[1][1]) && is_ascii_lower_case(locale_elements[1][2]) && is_ascii_lower_case(locale_elements[1][3])) {
141
script = locale_elements[1];
142
}
143
if (locale_elements[1].length() == 2 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_upper_case(locale_elements[1][1])) {
144
country = locale_elements[1];
145
}
146
}
147
if (locale_elements.size() >= 3) {
148
if (locale_elements[2].length() == 2 && is_ascii_upper_case(locale_elements[2][0]) && is_ascii_upper_case(locale_elements[2][1])) {
149
country = locale_elements[2];
150
} else if (p_server.variant_map.has(locale_elements[2].to_lower()) && p_server.variant_map[locale_elements[2].to_lower()] == language) {
151
variant = locale_elements[2].to_lower();
152
}
153
}
154
if (locale_elements.size() >= 4) {
155
if (p_server.variant_map.has(locale_elements[3].to_lower()) && p_server.variant_map[locale_elements[3].to_lower()] == language) {
156
variant = locale_elements[3].to_lower();
157
}
158
}
159
160
// Try extract script and variant from the extra part.
161
Vector<String> script_extra = univ_locale.get_slicec('@', 1).split(";");
162
for (int i = 0; i < script_extra.size(); i++) {
163
if (script_extra[i].to_lower() == "cyrillic") {
164
script = "Cyrl";
165
break;
166
} else if (script_extra[i].to_lower() == "latin") {
167
script = "Latn";
168
break;
169
} else if (script_extra[i].to_lower() == "devanagari") {
170
script = "Deva";
171
break;
172
} else if (p_server.variant_map.has(script_extra[i].to_lower()) && p_server.variant_map[script_extra[i].to_lower()] == language) {
173
variant = script_extra[i].to_lower();
174
}
175
}
176
177
// Handles known non-ISO language names used e.g. on Windows.
178
if (p_server.locale_rename_map.has(language)) {
179
language = p_server.locale_rename_map[language];
180
}
181
182
// Handle country renames.
183
if (p_server.country_rename_map.has(country)) {
184
country = p_server.country_rename_map[country];
185
}
186
187
// Remove unsupported script codes.
188
if (!p_server.script_map.has(script)) {
189
script = "";
190
}
191
192
// Add script code base on language and country codes for some ambiguous cases.
193
if (p_add_defaults) {
194
if (script.is_empty()) {
195
for (int i = 0; i < p_server.locale_script_info.size(); i++) {
196
const LocaleScriptInfo &info = p_server.locale_script_info[i];
197
if (info.name == language) {
198
if (country.is_empty() || info.supported_countries.has(country)) {
199
script = info.script;
200
break;
201
}
202
}
203
}
204
}
205
if (!script.is_empty() && country.is_empty()) {
206
// Add conntry code based on script for some ambiguous cases.
207
for (int i = 0; i < p_server.locale_script_info.size(); i++) {
208
const LocaleScriptInfo &info = p_server.locale_script_info[i];
209
if (info.name == language && info.script == script) {
210
country = info.default_country;
211
break;
212
}
213
}
214
}
215
}
216
}
217
218
String TranslationServer::standardize_locale(const String &p_locale, bool p_add_defaults) const {
219
return Locale(*this, p_locale, p_add_defaults).operator String();
220
}
221
222
int TranslationServer::compare_locales(const String &p_locale_a, const String &p_locale_b) const {
223
if (p_locale_a == p_locale_b) {
224
// Exact match.
225
return 10;
226
}
227
228
const String cache_key = p_locale_a + "|" + p_locale_b;
229
const int *cached_result = locale_compare_cache.getptr(cache_key);
230
if (cached_result) {
231
return *cached_result;
232
}
233
234
Locale locale_a = Locale(*this, p_locale_a, true);
235
Locale locale_b = Locale(*this, p_locale_b, true);
236
237
if (locale_a == locale_b) {
238
// Exact match.
239
locale_compare_cache.insert(cache_key, 10);
240
return 10;
241
}
242
243
if (locale_a.language != locale_b.language) {
244
// No match.
245
locale_compare_cache.insert(cache_key, 0);
246
return 0;
247
}
248
249
// Matching language, both locales have extra parts. Compare the
250
// remaining elements. If both elements are non-empty, check the
251
// match to increase or decrease the score. If either element or
252
// both are empty, leave the score as is.
253
int score = 5;
254
if (!locale_a.script.is_empty() && !locale_b.script.is_empty()) {
255
if (locale_a.script == locale_b.script) {
256
score++;
257
} else {
258
score--;
259
}
260
}
261
if (!locale_a.country.is_empty() && !locale_b.country.is_empty()) {
262
if (locale_a.country == locale_b.country) {
263
score++;
264
} else {
265
score--;
266
}
267
}
268
if (!locale_a.variant.is_empty() && !locale_b.variant.is_empty()) {
269
if (locale_a.variant == locale_b.variant) {
270
score++;
271
} else {
272
score--;
273
}
274
}
275
276
locale_compare_cache.insert(cache_key, score);
277
return score;
278
}
279
280
String TranslationServer::get_locale_name(const String &p_locale) const {
281
String lang_name, script_name, country_name;
282
Vector<String> locale_elements = standardize_locale(p_locale).split("_");
283
lang_name = locale_elements[0];
284
if (locale_elements.size() >= 2) {
285
if (locale_elements[1].length() == 4 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_lower_case(locale_elements[1][1]) && is_ascii_lower_case(locale_elements[1][2]) && is_ascii_lower_case(locale_elements[1][3])) {
286
script_name = locale_elements[1];
287
}
288
if (locale_elements[1].length() == 2 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_upper_case(locale_elements[1][1])) {
289
country_name = locale_elements[1];
290
}
291
}
292
if (locale_elements.size() >= 3) {
293
if (locale_elements[2].length() == 2 && is_ascii_upper_case(locale_elements[2][0]) && is_ascii_upper_case(locale_elements[2][1])) {
294
country_name = locale_elements[2];
295
}
296
}
297
298
String name = get_language_name(lang_name);
299
if (!script_name.is_empty()) {
300
name = name + " (" + get_script_name(script_name) + ")";
301
}
302
if (!country_name.is_empty()) {
303
name = name + ", " + get_country_name(country_name);
304
}
305
return name;
306
}
307
308
Vector<String> TranslationServer::get_all_languages() const {
309
Vector<String> languages;
310
311
for (const KeyValue<String, String> &E : language_map) {
312
languages.push_back(E.key);
313
}
314
315
return languages;
316
}
317
318
String TranslationServer::get_language_name(const String &p_language) const {
319
if (language_map.has(p_language)) {
320
return language_map[p_language];
321
} else {
322
return p_language;
323
}
324
}
325
326
Vector<String> TranslationServer::get_all_scripts() const {
327
Vector<String> scripts;
328
329
for (const KeyValue<String, String> &E : script_map) {
330
scripts.push_back(E.key);
331
}
332
333
return scripts;
334
}
335
336
String TranslationServer::get_script_name(const String &p_script) const {
337
if (script_map.has(p_script)) {
338
return script_map[p_script];
339
} else {
340
return p_script;
341
}
342
}
343
344
Vector<String> TranslationServer::get_all_countries() const {
345
Vector<String> countries;
346
347
for (const KeyValue<String, String> &E : country_name_map) {
348
countries.push_back(E.key);
349
}
350
351
return countries;
352
}
353
354
String TranslationServer::get_country_name(const String &p_country) const {
355
if (country_name_map.has(p_country)) {
356
return country_name_map[p_country];
357
} else {
358
return p_country;
359
}
360
}
361
362
void TranslationServer::set_locale(const String &p_locale) {
363
String new_locale = standardize_locale(p_locale);
364
if (locale == new_locale) {
365
return;
366
}
367
368
locale = new_locale;
369
ResourceLoader::reload_translation_remaps();
370
371
if (OS::get_singleton()->get_main_loop()) {
372
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
373
}
374
}
375
376
String TranslationServer::get_locale() const {
377
return locale;
378
}
379
380
void TranslationServer::set_fallback_locale(const String &p_locale) {
381
fallback = p_locale;
382
}
383
384
String TranslationServer::get_fallback_locale() const {
385
return fallback;
386
}
387
388
PackedStringArray TranslationServer::get_loaded_locales() const {
389
return main_domain->get_loaded_locales();
390
}
391
392
void TranslationServer::add_translation(const Ref<Translation> &p_translation) {
393
main_domain->add_translation(p_translation);
394
}
395
396
void TranslationServer::remove_translation(const Ref<Translation> &p_translation) {
397
main_domain->remove_translation(p_translation);
398
}
399
400
Ref<Translation> TranslationServer::get_translation_object(const String &p_locale) {
401
return main_domain->get_translation_object(p_locale);
402
}
403
404
void TranslationServer::clear() {
405
main_domain->clear();
406
}
407
408
StringName TranslationServer::translate(const StringName &p_message, const StringName &p_context) const {
409
return main_domain->translate(p_message, p_context);
410
}
411
412
StringName TranslationServer::translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
413
return main_domain->translate_plural(p_message, p_message_plural, p_n, p_context);
414
}
415
416
bool TranslationServer::has_domain(const StringName &p_domain) const {
417
if (p_domain == StringName()) {
418
return true;
419
}
420
return custom_domains.has(p_domain);
421
}
422
423
Ref<TranslationDomain> TranslationServer::get_or_add_domain(const StringName &p_domain) {
424
if (p_domain == StringName()) {
425
return main_domain;
426
}
427
const Ref<TranslationDomain> *domain = custom_domains.getptr(p_domain);
428
if (domain) {
429
if (domain->is_valid()) {
430
return *domain;
431
}
432
ERR_PRINT("Bug (please report): Found invalid translation domain.");
433
}
434
Ref<TranslationDomain> new_domain = memnew(TranslationDomain);
435
custom_domains[p_domain] = new_domain;
436
return new_domain;
437
}
438
439
void TranslationServer::remove_domain(const StringName &p_domain) {
440
ERR_FAIL_COND_MSG(p_domain == StringName(), "Cannot remove main translation domain.");
441
custom_domains.erase(p_domain);
442
}
443
444
void TranslationServer::setup() {
445
String test = GLOBAL_DEF("internationalization/locale/test", "");
446
test = test.strip_edges();
447
if (!test.is_empty()) {
448
set_locale(test);
449
} else {
450
set_locale(OS::get_singleton()->get_locale());
451
}
452
453
fallback = GLOBAL_DEF("internationalization/locale/fallback", "en");
454
main_domain->set_pseudolocalization_enabled(GLOBAL_DEF("internationalization/pseudolocalization/use_pseudolocalization", false));
455
main_domain->set_pseudolocalization_accents_enabled(GLOBAL_DEF("internationalization/pseudolocalization/replace_with_accents", true));
456
main_domain->set_pseudolocalization_double_vowels_enabled(GLOBAL_DEF("internationalization/pseudolocalization/double_vowels", false));
457
main_domain->set_pseudolocalization_fake_bidi_enabled(GLOBAL_DEF("internationalization/pseudolocalization/fake_bidi", false));
458
main_domain->set_pseudolocalization_override_enabled(GLOBAL_DEF("internationalization/pseudolocalization/override", false));
459
main_domain->set_pseudolocalization_expansion_ratio(GLOBAL_DEF("internationalization/pseudolocalization/expansion_ratio", 0.0));
460
main_domain->set_pseudolocalization_prefix(GLOBAL_DEF("internationalization/pseudolocalization/prefix", "["));
461
main_domain->set_pseudolocalization_suffix(GLOBAL_DEF("internationalization/pseudolocalization/suffix", "]"));
462
main_domain->set_pseudolocalization_skip_placeholders_enabled(GLOBAL_DEF("internationalization/pseudolocalization/skip_placeholders", true));
463
464
#ifdef TOOLS_ENABLED
465
ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, "internationalization/locale/test", PROPERTY_HINT_LOCALE_ID, ""));
466
ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, "internationalization/locale/fallback", PROPERTY_HINT_LOCALE_ID, ""));
467
#endif
468
}
469
470
String TranslationServer::get_tool_locale() {
471
#ifdef TOOLS_ENABLED
472
if (Engine::get_singleton()->is_editor_hint() || Engine::get_singleton()->is_project_manager_hint()) {
473
const PackedStringArray &locales = editor_domain->get_loaded_locales();
474
if (locales.has(locale)) {
475
return locale;
476
}
477
return "en";
478
} else {
479
#else
480
{
481
#endif
482
// Look for best matching loaded translation.
483
Ref<Translation> t = main_domain->get_translation_object(locale);
484
if (t.is_null()) {
485
return fallback;
486
}
487
return t->get_locale();
488
}
489
}
490
491
StringName TranslationServer::tool_translate(const StringName &p_message, const StringName &p_context) const {
492
return editor_domain->translate(p_message, p_context);
493
}
494
495
StringName TranslationServer::tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
496
return editor_domain->translate_plural(p_message, p_message_plural, p_n, p_context);
497
}
498
499
StringName TranslationServer::property_translate(const StringName &p_message, const StringName &p_context) const {
500
return property_domain->translate(p_message, p_context);
501
}
502
503
StringName TranslationServer::doc_translate(const StringName &p_message, const StringName &p_context) const {
504
return doc_domain->translate(p_message, p_context);
505
}
506
507
StringName TranslationServer::doc_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
508
return doc_domain->translate_plural(p_message, p_message_plural, p_n, p_context);
509
}
510
511
bool TranslationServer::is_pseudolocalization_enabled() const {
512
return main_domain->is_pseudolocalization_enabled();
513
}
514
515
void TranslationServer::set_pseudolocalization_enabled(bool p_enabled) {
516
main_domain->set_pseudolocalization_enabled(p_enabled);
517
518
ResourceLoader::reload_translation_remaps();
519
520
if (OS::get_singleton()->get_main_loop()) {
521
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
522
}
523
}
524
525
void TranslationServer::reload_pseudolocalization() {
526
main_domain->set_pseudolocalization_accents_enabled(GLOBAL_GET("internationalization/pseudolocalization/replace_with_accents"));
527
main_domain->set_pseudolocalization_double_vowels_enabled(GLOBAL_GET("internationalization/pseudolocalization/double_vowels"));
528
main_domain->set_pseudolocalization_fake_bidi_enabled(GLOBAL_GET("internationalization/pseudolocalization/fake_bidi"));
529
main_domain->set_pseudolocalization_override_enabled(GLOBAL_GET("internationalization/pseudolocalization/override"));
530
main_domain->set_pseudolocalization_expansion_ratio(GLOBAL_GET("internationalization/pseudolocalization/expansion_ratio"));
531
main_domain->set_pseudolocalization_prefix(GLOBAL_GET("internationalization/pseudolocalization/prefix"));
532
main_domain->set_pseudolocalization_suffix(GLOBAL_GET("internationalization/pseudolocalization/suffix"));
533
main_domain->set_pseudolocalization_skip_placeholders_enabled(GLOBAL_GET("internationalization/pseudolocalization/skip_placeholders"));
534
535
ResourceLoader::reload_translation_remaps();
536
537
if (OS::get_singleton()->get_main_loop()) {
538
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
539
}
540
}
541
542
StringName TranslationServer::pseudolocalize(const StringName &p_message) const {
543
return main_domain->pseudolocalize(p_message);
544
}
545
546
#ifdef TOOLS_ENABLED
547
void TranslationServer::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
548
const String pf = p_function;
549
if (p_idx == 0) {
550
HashMap<String, String> *target_hash_map = nullptr;
551
if (pf == "get_language_name") {
552
target_hash_map = &language_map;
553
} else if (pf == "get_script_name") {
554
target_hash_map = &script_map;
555
} else if (pf == "get_country_name") {
556
target_hash_map = &country_name_map;
557
}
558
559
if (target_hash_map) {
560
for (const KeyValue<String, String> &E : *target_hash_map) {
561
r_options->push_back(E.key.quote());
562
}
563
}
564
}
565
Object::get_argument_options(p_function, p_idx, r_options);
566
}
567
#endif // TOOLS_ENABLED
568
569
void TranslationServer::_bind_methods() {
570
ClassDB::bind_method(D_METHOD("set_locale", "locale"), &TranslationServer::set_locale);
571
ClassDB::bind_method(D_METHOD("get_locale"), &TranslationServer::get_locale);
572
ClassDB::bind_method(D_METHOD("get_tool_locale"), &TranslationServer::get_tool_locale);
573
574
ClassDB::bind_method(D_METHOD("compare_locales", "locale_a", "locale_b"), &TranslationServer::compare_locales);
575
ClassDB::bind_method(D_METHOD("standardize_locale", "locale", "add_defaults"), &TranslationServer::standardize_locale, DEFVAL(false));
576
577
ClassDB::bind_method(D_METHOD("get_all_languages"), &TranslationServer::get_all_languages);
578
ClassDB::bind_method(D_METHOD("get_language_name", "language"), &TranslationServer::get_language_name);
579
580
ClassDB::bind_method(D_METHOD("get_all_scripts"), &TranslationServer::get_all_scripts);
581
ClassDB::bind_method(D_METHOD("get_script_name", "script"), &TranslationServer::get_script_name);
582
583
ClassDB::bind_method(D_METHOD("get_all_countries"), &TranslationServer::get_all_countries);
584
ClassDB::bind_method(D_METHOD("get_country_name", "country"), &TranslationServer::get_country_name);
585
586
ClassDB::bind_method(D_METHOD("get_locale_name", "locale"), &TranslationServer::get_locale_name);
587
588
ClassDB::bind_method(D_METHOD("translate", "message", "context"), &TranslationServer::translate, DEFVAL(StringName()));
589
ClassDB::bind_method(D_METHOD("translate_plural", "message", "plural_message", "n", "context"), &TranslationServer::translate_plural, DEFVAL(StringName()));
590
591
ClassDB::bind_method(D_METHOD("add_translation", "translation"), &TranslationServer::add_translation);
592
ClassDB::bind_method(D_METHOD("remove_translation", "translation"), &TranslationServer::remove_translation);
593
ClassDB::bind_method(D_METHOD("get_translation_object", "locale"), &TranslationServer::get_translation_object);
594
595
ClassDB::bind_method(D_METHOD("has_domain", "domain"), &TranslationServer::has_domain);
596
ClassDB::bind_method(D_METHOD("get_or_add_domain", "domain"), &TranslationServer::get_or_add_domain);
597
ClassDB::bind_method(D_METHOD("remove_domain", "domain"), &TranslationServer::remove_domain);
598
599
ClassDB::bind_method(D_METHOD("clear"), &TranslationServer::clear);
600
601
ClassDB::bind_method(D_METHOD("get_loaded_locales"), &TranslationServer::get_loaded_locales);
602
603
ClassDB::bind_method(D_METHOD("is_pseudolocalization_enabled"), &TranslationServer::is_pseudolocalization_enabled);
604
ClassDB::bind_method(D_METHOD("set_pseudolocalization_enabled", "enabled"), &TranslationServer::set_pseudolocalization_enabled);
605
ClassDB::bind_method(D_METHOD("reload_pseudolocalization"), &TranslationServer::reload_pseudolocalization);
606
ClassDB::bind_method(D_METHOD("pseudolocalize", "message"), &TranslationServer::pseudolocalize);
607
ADD_PROPERTY(PropertyInfo(Variant::Type::BOOL, "pseudolocalization_enabled"), "set_pseudolocalization_enabled", "is_pseudolocalization_enabled");
608
}
609
610
void TranslationServer::load_translations() {
611
const String prop = "internationalization/locale/translations";
612
if (!ProjectSettings::get_singleton()->has_setting(prop)) {
613
return;
614
}
615
const Vector<String> &translations = GLOBAL_GET(prop);
616
for (const String &path : translations) {
617
Ref<Translation> tr = ResourceLoader::load(path);
618
if (tr.is_valid()) {
619
add_translation(tr);
620
}
621
}
622
}
623
624
TranslationServer::TranslationServer() {
625
singleton = this;
626
main_domain.instantiate();
627
editor_domain = get_or_add_domain("godot.editor");
628
property_domain = get_or_add_domain("godot.properties");
629
doc_domain = get_or_add_domain("godot.documentation");
630
init_locale_info();
631
}
632
633