1/**************************************************************************/
2/* translation.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.h"
32
33#include "core/config/project_settings.h"
34#include "core/io/resource_loader.h"
35#include "core/os/os.h"
36#include "core/string/locales.h"
37
38#ifdef TOOLS_ENABLED
39#include "main/main.h"
40#endif
41
42Dictionary Translation::_get_messages() const {
43 Dictionary d;
44 for (const KeyValue<StringName, StringName> &E : translation_map) {
45 d[E.key] = E.value;
46 }
47 return d;
48}
49
50Vector<String> Translation::_get_message_list() const {
51 Vector<String> msgs;
52 msgs.resize(translation_map.size());
53 int idx = 0;
54 for (const KeyValue<StringName, StringName> &E : translation_map) {
55 msgs.set(idx, E.key);
56 idx += 1;
57 }
58
59 return msgs;
60}
61
62Vector<String> Translation::get_translated_message_list() const {
63 Vector<String> msgs;
64 msgs.resize(translation_map.size());
65 int idx = 0;
66 for (const KeyValue<StringName, StringName> &E : translation_map) {
67 msgs.set(idx, E.value);
68 idx += 1;
69 }
70
71 return msgs;
72}
73
74void Translation::_set_messages(const Dictionary &p_messages) {
75 List<Variant> keys;
76 p_messages.get_key_list(&keys);
77 for (const Variant &E : keys) {
78 translation_map[E] = p_messages[E];
79 }
80}
81
82void Translation::set_locale(const String &p_locale) {
83 locale = TranslationServer::get_singleton()->standardize_locale(p_locale);
84
85 if (Thread::is_main_thread()) {
86 _notify_translation_changed_if_applies();
87 } else {
88 // Avoid calling non-thread-safe functions here.
89 callable_mp(this, &Translation::_notify_translation_changed_if_applies).call_deferred();
90 }
91}
92
93void Translation::_notify_translation_changed_if_applies() {
94 if (OS::get_singleton()->get_main_loop() && TranslationServer::get_singleton()->get_loaded_locales().has(get_locale())) {
95 OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
96 }
97}
98
99void Translation::add_message(const StringName &p_src_text, const StringName &p_xlated_text, const StringName &p_context) {
100 translation_map[p_src_text] = p_xlated_text;
101}
102
103void Translation::add_plural_message(const StringName &p_src_text, const Vector<String> &p_plural_xlated_texts, const StringName &p_context) {
104 WARN_PRINT("Translation class doesn't handle plural messages. Calling add_plural_message() on a Translation instance is probably a mistake. \nUse a derived Translation class that handles plurals, such as TranslationPO class");
105 ERR_FAIL_COND_MSG(p_plural_xlated_texts.is_empty(), "Parameter vector p_plural_xlated_texts passed in is empty.");
106 translation_map[p_src_text] = p_plural_xlated_texts[0];
107}
108
109StringName Translation::get_message(const StringName &p_src_text, const StringName &p_context) const {
110 StringName ret;
111 if (GDVIRTUAL_CALL(_get_message, p_src_text, p_context, ret)) {
112 return ret;
113 }
114
115 if (p_context != StringName()) {
116 WARN_PRINT("Translation class doesn't handle context. Using context in get_message() on a Translation instance is probably a mistake. \nUse a derived Translation class that handles context, such as TranslationPO class");
117 }
118
119 HashMap<StringName, StringName>::ConstIterator E = translation_map.find(p_src_text);
120 if (!E) {
121 return StringName();
122 }
123
124 return E->value;
125}
126
127StringName Translation::get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context) const {
128 StringName ret;
129 if (GDVIRTUAL_CALL(_get_plural_message, p_src_text, p_plural_text, p_n, p_context, ret)) {
130 return ret;
131 }
132
133 WARN_PRINT("Translation class doesn't handle plural messages. Calling get_plural_message() on a Translation instance is probably a mistake. \nUse a derived Translation class that handles plurals, such as TranslationPO class");
134 return get_message(p_src_text);
135}
136
137void Translation::erase_message(const StringName &p_src_text, const StringName &p_context) {
138 if (p_context != StringName()) {
139 WARN_PRINT("Translation class doesn't handle context. Using context in erase_message() on a Translation instance is probably a mistake. \nUse a derived Translation class that handles context, such as TranslationPO class");
140 }
141
142 translation_map.erase(p_src_text);
143}
144
145void Translation::get_message_list(List<StringName> *r_messages) const {
146 for (const KeyValue<StringName, StringName> &E : translation_map) {
147 r_messages->push_back(E.key);
148 }
149}
150
151int Translation::get_message_count() const {
152 return translation_map.size();
153}
154
155void Translation::_bind_methods() {
156 ClassDB::bind_method(D_METHOD("set_locale", "locale"), &Translation::set_locale);
157 ClassDB::bind_method(D_METHOD("get_locale"), &Translation::get_locale);
158 ClassDB::bind_method(D_METHOD("add_message", "src_message", "xlated_message", "context"), &Translation::add_message, DEFVAL(""));
159 ClassDB::bind_method(D_METHOD("add_plural_message", "src_message", "xlated_messages", "context"), &Translation::add_plural_message, DEFVAL(""));
160 ClassDB::bind_method(D_METHOD("get_message", "src_message", "context"), &Translation::get_message, DEFVAL(""));
161 ClassDB::bind_method(D_METHOD("get_plural_message", "src_message", "src_plural_message", "n", "context"), &Translation::get_plural_message, DEFVAL(""));
162 ClassDB::bind_method(D_METHOD("erase_message", "src_message", "context"), &Translation::erase_message, DEFVAL(""));
163 ClassDB::bind_method(D_METHOD("get_message_list"), &Translation::_get_message_list);
164 ClassDB::bind_method(D_METHOD("get_translated_message_list"), &Translation::get_translated_message_list);
165 ClassDB::bind_method(D_METHOD("get_message_count"), &Translation::get_message_count);
166 ClassDB::bind_method(D_METHOD("_set_messages", "messages"), &Translation::_set_messages);
167 ClassDB::bind_method(D_METHOD("_get_messages"), &Translation::_get_messages);
168
169 GDVIRTUAL_BIND(_get_plural_message, "src_message", "src_plural_message", "n", "context");
170 GDVIRTUAL_BIND(_get_message, "src_message", "context");
171
172 ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "messages", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_messages", "_get_messages");
173 ADD_PROPERTY(PropertyInfo(Variant::STRING, "locale"), "set_locale", "get_locale");
174}
175
176///////////////////////////////////////////////
177
178struct _character_accent_pair {
179 const char32_t character;
180 const char32_t *accented_character;
181};
182
183static _character_accent_pair _character_to_accented[] = {
184 { 'A', U"Å" },
185 { 'B', U"ß" },
186 { 'C', U"Ç" },
187 { 'D', U"Ð" },
188 { 'E', U"É" },
189 { 'F', U"F́" },
190 { 'G', U"Ĝ" },
191 { 'H', U"Ĥ" },
192 { 'I', U"Ĩ" },
193 { 'J', U"Ĵ" },
194 { 'K', U"ĸ" },
195 { 'L', U"Ł" },
196 { 'M', U"Ḿ" },
197 { 'N', U"й" },
198 { 'O', U"Ö" },
199 { 'P', U"Ṕ" },
200 { 'Q', U"Q́" },
201 { 'R', U"Ř" },
202 { 'S', U"Ŝ" },
203 { 'T', U"Ŧ" },
204 { 'U', U"Ũ" },
205 { 'V', U"Ṽ" },
206 { 'W', U"Ŵ" },
207 { 'X', U"X́" },
208 { 'Y', U"Ÿ" },
209 { 'Z', U"Ž" },
210 { 'a', U"á" },
211 { 'b', U"ḅ" },
212 { 'c', U"ć" },
213 { 'd', U"d́" },
214 { 'e', U"é" },
215 { 'f', U"f́" },
216 { 'g', U"ǵ" },
217 { 'h', U"h̀" },
218 { 'i', U"í" },
219 { 'j', U"ǰ" },
220 { 'k', U"ḱ" },
221 { 'l', U"ł" },
222 { 'm', U"m̀" },
223 { 'n', U"ή" },
224 { 'o', U"ô" },
225 { 'p', U"ṕ" },
226 { 'q', U"q́" },
227 { 'r', U"ŕ" },
228 { 's', U"š" },
229 { 't', U"ŧ" },
230 { 'u', U"ü" },
231 { 'v', U"ṽ" },
232 { 'w', U"ŵ" },
233 { 'x', U"x́" },
234 { 'y', U"ý" },
235 { 'z', U"ź" },
236};
237
238Vector<TranslationServer::LocaleScriptInfo> TranslationServer::locale_script_info;
239
240HashMap<String, String> TranslationServer::language_map;
241HashMap<String, String> TranslationServer::script_map;
242HashMap<String, String> TranslationServer::locale_rename_map;
243HashMap<String, String> TranslationServer::country_name_map;
244HashMap<String, String> TranslationServer::variant_map;
245HashMap<String, String> TranslationServer::country_rename_map;
246
247void TranslationServer::init_locale_info() {
248 // Init locale info.
249 language_map.clear();
250 int idx = 0;
251 while (language_list[idx][0] != nullptr) {
252 language_map[language_list[idx][0]] = String::utf8(language_list[idx][1]);
253 idx++;
254 }
255
256 // Init locale-script map.
257 locale_script_info.clear();
258 idx = 0;
259 while (locale_scripts[idx][0] != nullptr) {
260 LocaleScriptInfo info;
261 info.name = locale_scripts[idx][0];
262 info.script = locale_scripts[idx][1];
263 info.default_country = locale_scripts[idx][2];
264 Vector<String> supported_countries = String(locale_scripts[idx][3]).split(",", false);
265 for (int i = 0; i < supported_countries.size(); i++) {
266 info.supported_countries.insert(supported_countries[i]);
267 }
268 locale_script_info.push_back(info);
269 idx++;
270 }
271
272 // Init supported script list.
273 script_map.clear();
274 idx = 0;
275 while (script_list[idx][0] != nullptr) {
276 script_map[script_list[idx][1]] = String::utf8(script_list[idx][0]);
277 idx++;
278 }
279
280 // Init regional variant map.
281 variant_map.clear();
282 idx = 0;
283 while (locale_variants[idx][0] != nullptr) {
284 variant_map[locale_variants[idx][0]] = locale_variants[idx][1];
285 idx++;
286 }
287
288 // Init locale renames.
289 locale_rename_map.clear();
290 idx = 0;
291 while (locale_renames[idx][0] != nullptr) {
292 if (!String(locale_renames[idx][1]).is_empty()) {
293 locale_rename_map[locale_renames[idx][0]] = locale_renames[idx][1];
294 }
295 idx++;
296 }
297
298 // Init country names.
299 country_name_map.clear();
300 idx = 0;
301 while (country_names[idx][0] != nullptr) {
302 country_name_map[String(country_names[idx][0])] = String::utf8(country_names[idx][1]);
303 idx++;
304 }
305
306 // Init country renames.
307 country_rename_map.clear();
308 idx = 0;
309 while (country_renames[idx][0] != nullptr) {
310 if (!String(country_renames[idx][1]).is_empty()) {
311 country_rename_map[country_renames[idx][0]] = country_renames[idx][1];
312 }
313 idx++;
314 }
315}
316
317String TranslationServer::standardize_locale(const String &p_locale) const {
318 return _standardize_locale(p_locale, false);
319}
320
321String TranslationServer::_standardize_locale(const String &p_locale, bool p_add_defaults) const {
322 // Replaces '-' with '_' for macOS style locales.
323 String univ_locale = p_locale.replace("-", "_");
324
325 // Extract locale elements.
326 String lang_name, script_name, country_name, variant_name;
327 Vector<String> locale_elements = univ_locale.get_slice("@", 0).split("_");
328 lang_name = locale_elements[0];
329 if (locale_elements.size() >= 2) {
330 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])) {
331 script_name = locale_elements[1];
332 }
333 if (locale_elements[1].length() == 2 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_upper_case(locale_elements[1][1])) {
334 country_name = locale_elements[1];
335 }
336 }
337 if (locale_elements.size() >= 3) {
338 if (locale_elements[2].length() == 2 && is_ascii_upper_case(locale_elements[2][0]) && is_ascii_upper_case(locale_elements[2][1])) {
339 country_name = locale_elements[2];
340 } else if (variant_map.has(locale_elements[2].to_lower()) && variant_map[locale_elements[2].to_lower()] == lang_name) {
341 variant_name = locale_elements[2].to_lower();
342 }
343 }
344 if (locale_elements.size() >= 4) {
345 if (variant_map.has(locale_elements[3].to_lower()) && variant_map[locale_elements[3].to_lower()] == lang_name) {
346 variant_name = locale_elements[3].to_lower();
347 }
348 }
349
350 // Try extract script and variant from the extra part.
351 Vector<String> script_extra = univ_locale.get_slice("@", 1).split(";");
352 for (int i = 0; i < script_extra.size(); i++) {
353 if (script_extra[i].to_lower() == "cyrillic") {
354 script_name = "Cyrl";
355 break;
356 } else if (script_extra[i].to_lower() == "latin") {
357 script_name = "Latn";
358 break;
359 } else if (script_extra[i].to_lower() == "devanagari") {
360 script_name = "Deva";
361 break;
362 } else if (variant_map.has(script_extra[i].to_lower()) && variant_map[script_extra[i].to_lower()] == lang_name) {
363 variant_name = script_extra[i].to_lower();
364 }
365 }
366
367 // Handles known non-ISO language names used e.g. on Windows.
368 if (locale_rename_map.has(lang_name)) {
369 lang_name = locale_rename_map[lang_name];
370 }
371
372 // Handle country renames.
373 if (country_rename_map.has(country_name)) {
374 country_name = country_rename_map[country_name];
375 }
376
377 // Remove unsupported script codes.
378 if (!script_map.has(script_name)) {
379 script_name = "";
380 }
381
382 // Add script code base on language and country codes for some ambiguous cases.
383 if (p_add_defaults) {
384 if (script_name.is_empty()) {
385 for (int i = 0; i < locale_script_info.size(); i++) {
386 const LocaleScriptInfo &info = locale_script_info[i];
387 if (info.name == lang_name) {
388 if (country_name.is_empty() || info.supported_countries.has(country_name)) {
389 script_name = info.script;
390 break;
391 }
392 }
393 }
394 }
395 if (!script_name.is_empty() && country_name.is_empty()) {
396 // Add conntry code based on script for some ambiguous cases.
397 for (int i = 0; i < locale_script_info.size(); i++) {
398 const LocaleScriptInfo &info = locale_script_info[i];
399 if (info.name == lang_name && info.script == script_name) {
400 country_name = info.default_country;
401 break;
402 }
403 }
404 }
405 }
406
407 // Combine results.
408 String out = lang_name;
409 if (!script_name.is_empty()) {
410 out = out + "_" + script_name;
411 }
412 if (!country_name.is_empty()) {
413 out = out + "_" + country_name;
414 }
415 if (!variant_name.is_empty()) {
416 out = out + "_" + variant_name;
417 }
418 return out;
419}
420
421int TranslationServer::compare_locales(const String &p_locale_a, const String &p_locale_b) const {
422 String locale_a = _standardize_locale(p_locale_a, true);
423 String locale_b = _standardize_locale(p_locale_b, true);
424
425 if (locale_a == locale_b) {
426 // Exact match.
427 return 10;
428 }
429
430 Vector<String> locale_a_elements = locale_a.split("_");
431 Vector<String> locale_b_elements = locale_b.split("_");
432 if (locale_a_elements[0] == locale_b_elements[0]) {
433 // Matching language, both locales have extra parts.
434 // Return number of matching elements.
435 int matching_elements = 1;
436 for (int i = 1; i < locale_a_elements.size(); i++) {
437 for (int j = 1; j < locale_b_elements.size(); j++) {
438 if (locale_a_elements[i] == locale_b_elements[j]) {
439 matching_elements++;
440 }
441 }
442 }
443 return matching_elements;
444 } else {
445 // No match.
446 return 0;
447 }
448}
449
450String TranslationServer::get_locale_name(const String &p_locale) const {
451 String lang_name, script_name, country_name;
452 Vector<String> locale_elements = standardize_locale(p_locale).split("_");
453 lang_name = locale_elements[0];
454 if (locale_elements.size() >= 2) {
455 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])) {
456 script_name = locale_elements[1];
457 }
458 if (locale_elements[1].length() == 2 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_upper_case(locale_elements[1][1])) {
459 country_name = locale_elements[1];
460 }
461 }
462 if (locale_elements.size() >= 3) {
463 if (locale_elements[2].length() == 2 && is_ascii_upper_case(locale_elements[2][0]) && is_ascii_upper_case(locale_elements[2][1])) {
464 country_name = locale_elements[2];
465 }
466 }
467
468 String name = language_map[lang_name];
469 if (!script_name.is_empty()) {
470 name = name + " (" + script_map[script_name] + ")";
471 }
472 if (!country_name.is_empty()) {
473 name = name + ", " + country_name_map[country_name];
474 }
475 return name;
476}
477
478Vector<String> TranslationServer::get_all_languages() const {
479 Vector<String> languages;
480
481 for (const KeyValue<String, String> &E : language_map) {
482 languages.push_back(E.key);
483 }
484
485 return languages;
486}
487
488String TranslationServer::get_language_name(const String &p_language) const {
489 return language_map[p_language];
490}
491
492Vector<String> TranslationServer::get_all_scripts() const {
493 Vector<String> scripts;
494
495 for (const KeyValue<String, String> &E : script_map) {
496 scripts.push_back(E.key);
497 }
498
499 return scripts;
500}
501
502String TranslationServer::get_script_name(const String &p_script) const {
503 return script_map[p_script];
504}
505
506Vector<String> TranslationServer::get_all_countries() const {
507 Vector<String> countries;
508
509 for (const KeyValue<String, String> &E : country_name_map) {
510 countries.push_back(E.key);
511 }
512
513 return countries;
514}
515
516String TranslationServer::get_country_name(const String &p_country) const {
517 return country_name_map[p_country];
518}
519
520void TranslationServer::set_locale(const String &p_locale) {
521 locale = standardize_locale(p_locale);
522
523 if (OS::get_singleton()->get_main_loop()) {
524 OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
525 }
526
527 ResourceLoader::reload_translation_remaps();
528}
529
530String TranslationServer::get_locale() const {
531 return locale;
532}
533
534PackedStringArray TranslationServer::get_loaded_locales() const {
535 PackedStringArray locales;
536 for (const Ref<Translation> &E : translations) {
537 const Ref<Translation> &t = E;
538 ERR_FAIL_COND_V(t.is_null(), PackedStringArray());
539 String l = t->get_locale();
540
541 locales.push_back(l);
542 }
543
544 return locales;
545}
546
547void TranslationServer::add_translation(const Ref<Translation> &p_translation) {
548 translations.insert(p_translation);
549}
550
551void TranslationServer::remove_translation(const Ref<Translation> &p_translation) {
552 translations.erase(p_translation);
553}
554
555Ref<Translation> TranslationServer::get_translation_object(const String &p_locale) {
556 Ref<Translation> res;
557 int best_score = 0;
558
559 for (const Ref<Translation> &E : translations) {
560 const Ref<Translation> &t = E;
561 ERR_FAIL_COND_V(t.is_null(), nullptr);
562 String l = t->get_locale();
563
564 int score = compare_locales(p_locale, l);
565 if (score > 0 && score >= best_score) {
566 res = t;
567 best_score = score;
568 if (score == 10) {
569 break; // Exact match, skip the rest.
570 }
571 }
572 }
573 return res;
574}
575
576void TranslationServer::clear() {
577 translations.clear();
578}
579
580StringName TranslationServer::translate(const StringName &p_message, const StringName &p_context) const {
581 // Match given message against the translation catalog for the project locale.
582
583 if (!enabled) {
584 return p_message;
585 }
586
587 StringName res = _get_message_from_translations(p_message, p_context, locale, false);
588
589 if (!res && fallback.length() >= 2) {
590 res = _get_message_from_translations(p_message, p_context, fallback, false);
591 }
592
593 if (!res) {
594 return pseudolocalization_enabled ? pseudolocalize(p_message) : p_message;
595 }
596
597 return pseudolocalization_enabled ? pseudolocalize(res) : res;
598}
599
600StringName TranslationServer::translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
601 if (!enabled) {
602 if (p_n == 1) {
603 return p_message;
604 }
605 return p_message_plural;
606 }
607
608 StringName res = _get_message_from_translations(p_message, p_context, locale, true, p_message_plural, p_n);
609
610 if (!res && fallback.length() >= 2) {
611 res = _get_message_from_translations(p_message, p_context, fallback, true, p_message_plural, p_n);
612 }
613
614 if (!res) {
615 if (p_n == 1) {
616 return p_message;
617 }
618 return p_message_plural;
619 }
620
621 return res;
622}
623
624StringName TranslationServer::_get_message_from_translations(const StringName &p_message, const StringName &p_context, const String &p_locale, bool plural, const String &p_message_plural, int p_n) const {
625 StringName res;
626 int best_score = 0;
627
628 for (const Ref<Translation> &E : translations) {
629 const Ref<Translation> &t = E;
630 ERR_FAIL_COND_V(t.is_null(), p_message);
631 String l = t->get_locale();
632
633 int score = compare_locales(p_locale, l);
634 if (score > 0 && score >= best_score) {
635 StringName r;
636 if (!plural) {
637 r = t->get_message(p_message, p_context);
638 } else {
639 r = t->get_plural_message(p_message, p_message_plural, p_n, p_context);
640 }
641 if (!r) {
642 continue;
643 }
644 res = r;
645 best_score = score;
646 if (score == 10) {
647 break; // Exact match, skip the rest.
648 }
649 }
650 }
651
652 return res;
653}
654
655TranslationServer *TranslationServer::singleton = nullptr;
656
657bool TranslationServer::_load_translations(const String &p_from) {
658 if (ProjectSettings::get_singleton()->has_setting(p_from)) {
659 const Vector<String> &translation_names = GLOBAL_GET(p_from);
660
661 int tcount = translation_names.size();
662
663 if (tcount) {
664 const String *r = translation_names.ptr();
665
666 for (int i = 0; i < tcount; i++) {
667 Ref<Translation> tr = ResourceLoader::load(r[i]);
668 if (tr.is_valid()) {
669 add_translation(tr);
670 }
671 }
672 }
673 return true;
674 }
675
676 return false;
677}
678
679void TranslationServer::setup() {
680 String test = GLOBAL_DEF("internationalization/locale/test", "");
681 test = test.strip_edges();
682 if (!test.is_empty()) {
683 set_locale(test);
684 } else {
685 set_locale(OS::get_singleton()->get_locale());
686 }
687
688 fallback = GLOBAL_DEF("internationalization/locale/fallback", "en");
689 pseudolocalization_enabled = GLOBAL_DEF("internationalization/pseudolocalization/use_pseudolocalization", false);
690 pseudolocalization_accents_enabled = GLOBAL_DEF("internationalization/pseudolocalization/replace_with_accents", true);
691 pseudolocalization_double_vowels_enabled = GLOBAL_DEF("internationalization/pseudolocalization/double_vowels", false);
692 pseudolocalization_fake_bidi_enabled = GLOBAL_DEF("internationalization/pseudolocalization/fake_bidi", false);
693 pseudolocalization_override_enabled = GLOBAL_DEF("internationalization/pseudolocalization/override", false);
694 expansion_ratio = GLOBAL_DEF("internationalization/pseudolocalization/expansion_ratio", 0.0);
695 pseudolocalization_prefix = GLOBAL_DEF("internationalization/pseudolocalization/prefix", "[");
696 pseudolocalization_suffix = GLOBAL_DEF("internationalization/pseudolocalization/suffix", "]");
697 pseudolocalization_skip_placeholders_enabled = GLOBAL_DEF("internationalization/pseudolocalization/skip_placeholders", true);
698
699#ifdef TOOLS_ENABLED
700 ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, "internationalization/locale/fallback", PROPERTY_HINT_LOCALE_ID, ""));
701#endif
702}
703
704void TranslationServer::set_tool_translation(const Ref<Translation> &p_translation) {
705 tool_translation = p_translation;
706}
707
708Ref<Translation> TranslationServer::get_tool_translation() const {
709 return tool_translation;
710}
711
712String TranslationServer::get_tool_locale() {
713#ifdef TOOLS_ENABLED
714 if (Engine::get_singleton()->is_editor_hint() || Engine::get_singleton()->is_project_manager_hint()) {
715 if (TranslationServer::get_singleton()->get_tool_translation().is_valid()) {
716 return tool_translation->get_locale();
717 } else {
718 return "en";
719 }
720 } else {
721#else
722 {
723#endif
724 // Look for best matching loaded translation.
725 String best_locale = "en";
726 int best_score = 0;
727
728 for (const Ref<Translation> &E : translations) {
729 const Ref<Translation> &t = E;
730 ERR_FAIL_COND_V(t.is_null(), best_locale);
731 String l = t->get_locale();
732
733 int score = compare_locales(locale, l);
734 if (score > 0 && score >= best_score) {
735 best_locale = l;
736 best_score = score;
737 if (score == 10) {
738 break; // Exact match, skip the rest.
739 }
740 }
741 }
742 return best_locale;
743 }
744}
745
746StringName TranslationServer::tool_translate(const StringName &p_message, const StringName &p_context) const {
747 if (tool_translation.is_valid()) {
748 StringName r = tool_translation->get_message(p_message, p_context);
749 if (r) {
750 return editor_pseudolocalization ? tool_pseudolocalize(r) : r;
751 }
752 }
753 return editor_pseudolocalization ? tool_pseudolocalize(p_message) : p_message;
754}
755
756StringName TranslationServer::tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
757 if (tool_translation.is_valid()) {
758 StringName r = tool_translation->get_plural_message(p_message, p_message_plural, p_n, p_context);
759 if (r) {
760 return r;
761 }
762 }
763
764 if (p_n == 1) {
765 return p_message;
766 }
767 return p_message_plural;
768}
769
770void TranslationServer::set_doc_translation(const Ref<Translation> &p_translation) {
771 doc_translation = p_translation;
772}
773
774StringName TranslationServer::doc_translate(const StringName &p_message, const StringName &p_context) const {
775 if (doc_translation.is_valid()) {
776 StringName r = doc_translation->get_message(p_message, p_context);
777 if (r) {
778 return r;
779 }
780 }
781 return p_message;
782}
783
784StringName TranslationServer::doc_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
785 if (doc_translation.is_valid()) {
786 StringName r = doc_translation->get_plural_message(p_message, p_message_plural, p_n, p_context);
787 if (r) {
788 return r;
789 }
790 }
791
792 if (p_n == 1) {
793 return p_message;
794 }
795 return p_message_plural;
796}
797
798void TranslationServer::set_property_translation(const Ref<Translation> &p_translation) {
799 property_translation = p_translation;
800}
801
802StringName TranslationServer::property_translate(const StringName &p_message) const {
803 if (property_translation.is_valid()) {
804 StringName r = property_translation->get_message(p_message);
805 if (r) {
806 return r;
807 }
808 }
809 return p_message;
810}
811
812bool TranslationServer::is_pseudolocalization_enabled() const {
813 return pseudolocalization_enabled;
814}
815
816void TranslationServer::set_pseudolocalization_enabled(bool p_enabled) {
817 pseudolocalization_enabled = p_enabled;
818
819 if (OS::get_singleton()->get_main_loop()) {
820 OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
821 }
822 ResourceLoader::reload_translation_remaps();
823}
824
825void TranslationServer::set_editor_pseudolocalization(bool p_enabled) {
826 editor_pseudolocalization = p_enabled;
827}
828
829void TranslationServer::reload_pseudolocalization() {
830 pseudolocalization_accents_enabled = GLOBAL_GET("internationalization/pseudolocalization/replace_with_accents");
831 pseudolocalization_double_vowels_enabled = GLOBAL_GET("internationalization/pseudolocalization/double_vowels");
832 pseudolocalization_fake_bidi_enabled = GLOBAL_GET("internationalization/pseudolocalization/fake_bidi");
833 pseudolocalization_override_enabled = GLOBAL_GET("internationalization/pseudolocalization/override");
834 expansion_ratio = GLOBAL_GET("internationalization/pseudolocalization/expansion_ratio");
835 pseudolocalization_prefix = GLOBAL_GET("internationalization/pseudolocalization/prefix");
836 pseudolocalization_suffix = GLOBAL_GET("internationalization/pseudolocalization/suffix");
837 pseudolocalization_skip_placeholders_enabled = GLOBAL_GET("internationalization/pseudolocalization/skip_placeholders");
838
839 if (OS::get_singleton()->get_main_loop()) {
840 OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
841 }
842 ResourceLoader::reload_translation_remaps();
843}
844
845StringName TranslationServer::pseudolocalize(const StringName &p_message) const {
846 String message = p_message;
847 int length = message.length();
848 if (pseudolocalization_override_enabled) {
849 message = get_override_string(message);
850 }
851
852 if (pseudolocalization_double_vowels_enabled) {
853 message = double_vowels(message);
854 }
855
856 if (pseudolocalization_accents_enabled) {
857 message = replace_with_accented_string(message);
858 }
859
860 if (pseudolocalization_fake_bidi_enabled) {
861 message = wrap_with_fakebidi_characters(message);
862 }
863
864 StringName res = add_padding(message, length);
865 return res;
866}
867
868StringName TranslationServer::tool_pseudolocalize(const StringName &p_message) const {
869 String message = p_message;
870 message = double_vowels(message);
871 message = replace_with_accented_string(message);
872 StringName res = "[!!! " + message + " !!!]";
873 return res;
874}
875
876String TranslationServer::get_override_string(String &p_message) const {
877 String res;
878 for (int i = 0; i < p_message.size(); i++) {
879 if (pseudolocalization_skip_placeholders_enabled && is_placeholder(p_message, i)) {
880 res += p_message[i];
881 res += p_message[i + 1];
882 i++;
883 continue;
884 }
885 res += '*';
886 }
887 return res;
888}
889
890String TranslationServer::double_vowels(String &p_message) const {
891 String res;
892 for (int i = 0; i < p_message.size(); i++) {
893 if (pseudolocalization_skip_placeholders_enabled && is_placeholder(p_message, i)) {
894 res += p_message[i];
895 res += p_message[i + 1];
896 i++;
897 continue;
898 }
899 res += p_message[i];
900 if (p_message[i] == 'a' || p_message[i] == 'e' || p_message[i] == 'i' || p_message[i] == 'o' || p_message[i] == 'u' ||
901 p_message[i] == 'A' || p_message[i] == 'E' || p_message[i] == 'I' || p_message[i] == 'O' || p_message[i] == 'U') {
902 res += p_message[i];
903 }
904 }
905 return res;
906};
907
908String TranslationServer::replace_with_accented_string(String &p_message) const {
909 String res;
910 for (int i = 0; i < p_message.size(); i++) {
911 if (pseudolocalization_skip_placeholders_enabled && is_placeholder(p_message, i)) {
912 res += p_message[i];
913 res += p_message[i + 1];
914 i++;
915 continue;
916 }
917 const char32_t *accented = get_accented_version(p_message[i]);
918 if (accented) {
919 res += accented;
920 } else {
921 res += p_message[i];
922 }
923 }
924 return res;
925}
926
927String TranslationServer::wrap_with_fakebidi_characters(String &p_message) const {
928 String res;
929 char32_t fakebidiprefix = U'\u202e';
930 char32_t fakebidisuffix = U'\u202c';
931 res += fakebidiprefix;
932 // The fake bidi unicode gets popped at every newline so pushing it back at every newline.
933 for (int i = 0; i < p_message.size(); i++) {
934 if (p_message[i] == '\n') {
935 res += fakebidisuffix;
936 res += p_message[i];
937 res += fakebidiprefix;
938 } else if (pseudolocalization_skip_placeholders_enabled && is_placeholder(p_message, i)) {
939 res += fakebidisuffix;
940 res += p_message[i];
941 res += p_message[i + 1];
942 res += fakebidiprefix;
943 i++;
944 } else {
945 res += p_message[i];
946 }
947 }
948 res += fakebidisuffix;
949 return res;
950}
951
952String TranslationServer::add_padding(const String &p_message, int p_length) const {
953 String underscores = String("_").repeat(p_length * expansion_ratio / 2);
954 String prefix = pseudolocalization_prefix + underscores;
955 String suffix = underscores + pseudolocalization_suffix;
956
957 return prefix + p_message + suffix;
958}
959
960const char32_t *TranslationServer::get_accented_version(char32_t p_character) const {
961 if (!is_ascii_char(p_character)) {
962 return nullptr;
963 }
964
965 for (unsigned int i = 0; i < sizeof(_character_to_accented) / sizeof(_character_to_accented[0]); i++) {
966 if (_character_to_accented[i].character == p_character) {
967 return _character_to_accented[i].accented_character;
968 }
969 }
970
971 return nullptr;
972}
973
974bool TranslationServer::is_placeholder(String &p_message, int p_index) const {
975 return p_index < p_message.size() - 1 && p_message[p_index] == '%' &&
976 (p_message[p_index + 1] == 's' || p_message[p_index + 1] == 'c' || p_message[p_index + 1] == 'd' ||
977 p_message[p_index + 1] == 'o' || p_message[p_index + 1] == 'x' || p_message[p_index + 1] == 'X' || p_message[p_index + 1] == 'f');
978}
979
980void TranslationServer::_bind_methods() {
981 ClassDB::bind_method(D_METHOD("set_locale", "locale"), &TranslationServer::set_locale);
982 ClassDB::bind_method(D_METHOD("get_locale"), &TranslationServer::get_locale);
983 ClassDB::bind_method(D_METHOD("get_tool_locale"), &TranslationServer::get_tool_locale);
984
985 ClassDB::bind_method(D_METHOD("compare_locales", "locale_a", "locale_b"), &TranslationServer::compare_locales);
986 ClassDB::bind_method(D_METHOD("standardize_locale", "locale"), &TranslationServer::standardize_locale);
987
988 ClassDB::bind_method(D_METHOD("get_all_languages"), &TranslationServer::get_all_languages);
989 ClassDB::bind_method(D_METHOD("get_language_name", "language"), &TranslationServer::get_language_name);
990
991 ClassDB::bind_method(D_METHOD("get_all_scripts"), &TranslationServer::get_all_scripts);
992 ClassDB::bind_method(D_METHOD("get_script_name", "script"), &TranslationServer::get_script_name);
993
994 ClassDB::bind_method(D_METHOD("get_all_countries"), &TranslationServer::get_all_countries);
995 ClassDB::bind_method(D_METHOD("get_country_name", "country"), &TranslationServer::get_country_name);
996
997 ClassDB::bind_method(D_METHOD("get_locale_name", "locale"), &TranslationServer::get_locale_name);
998
999 ClassDB::bind_method(D_METHOD("translate", "message", "context"), &TranslationServer::translate, DEFVAL(""));
1000 ClassDB::bind_method(D_METHOD("translate_plural", "message", "plural_message", "n", "context"), &TranslationServer::translate_plural, DEFVAL(""));
1001
1002 ClassDB::bind_method(D_METHOD("add_translation", "translation"), &TranslationServer::add_translation);
1003 ClassDB::bind_method(D_METHOD("remove_translation", "translation"), &TranslationServer::remove_translation);
1004 ClassDB::bind_method(D_METHOD("get_translation_object", "locale"), &TranslationServer::get_translation_object);
1005
1006 ClassDB::bind_method(D_METHOD("clear"), &TranslationServer::clear);
1007
1008 ClassDB::bind_method(D_METHOD("get_loaded_locales"), &TranslationServer::get_loaded_locales);
1009
1010 ClassDB::bind_method(D_METHOD("is_pseudolocalization_enabled"), &TranslationServer::is_pseudolocalization_enabled);
1011 ClassDB::bind_method(D_METHOD("set_pseudolocalization_enabled", "enabled"), &TranslationServer::set_pseudolocalization_enabled);
1012 ClassDB::bind_method(D_METHOD("reload_pseudolocalization"), &TranslationServer::reload_pseudolocalization);
1013 ClassDB::bind_method(D_METHOD("pseudolocalize", "message"), &TranslationServer::pseudolocalize);
1014 ADD_PROPERTY(PropertyInfo(Variant::Type::BOOL, "pseudolocalization_enabled"), "set_pseudolocalization_enabled", "is_pseudolocalization_enabled");
1015}
1016
1017void TranslationServer::load_translations() {
1018 _load_translations("internationalization/locale/translations"); //all
1019 _load_translations("internationalization/locale/translations_" + locale.substr(0, 2));
1020
1021 if (locale.substr(0, 2) != locale) {
1022 _load_translations("internationalization/locale/translations_" + locale);
1023 }
1024}
1025
1026TranslationServer::TranslationServer() {
1027 singleton = this;
1028 init_locale_info();
1029}
1030