translation_server.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. #include "translation_server.h"
  31. #include "translation_server.compat.inc"
  32. #include "core/config/project_settings.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/os/os.h"
  35. #include "core/string/locales.h"
  36. #ifdef TOOLS_ENABLED
  37. #include "main/main.h"
  38. #endif
  39. Vector<TranslationServer::LocaleScriptInfo> TranslationServer::locale_script_info;
  40. HashMap<String, String> TranslationServer::language_map;
  41. HashMap<String, String> TranslationServer::script_map;
  42. HashMap<String, String> TranslationServer::locale_rename_map;
  43. HashMap<String, String> TranslationServer::country_name_map;
  44. HashMap<String, String> TranslationServer::variant_map;
  45. HashMap<String, String> TranslationServer::country_rename_map;
  46. void TranslationServer::init_locale_info() {
  47. // Init locale info.
  48. language_map.clear();
  49. int idx = 0;
  50. while (language_list[idx][0] != nullptr) {
  51. language_map[language_list[idx][0]] = String::utf8(language_list[idx][1]);
  52. idx++;
  53. }
  54. // Init locale-script map.
  55. locale_script_info.clear();
  56. idx = 0;
  57. while (locale_scripts[idx][0] != nullptr) {
  58. LocaleScriptInfo info;
  59. info.name = locale_scripts[idx][0];
  60. info.script = locale_scripts[idx][1];
  61. info.default_country = locale_scripts[idx][2];
  62. Vector<String> supported_countries = String(locale_scripts[idx][3]).split(",", false);
  63. for (int i = 0; i < supported_countries.size(); i++) {
  64. info.supported_countries.insert(supported_countries[i]);
  65. }
  66. locale_script_info.push_back(info);
  67. idx++;
  68. }
  69. // Init supported script list.
  70. script_map.clear();
  71. idx = 0;
  72. while (script_list[idx][0] != nullptr) {
  73. script_map[script_list[idx][1]] = String::utf8(script_list[idx][0]);
  74. idx++;
  75. }
  76. // Init regional variant map.
  77. variant_map.clear();
  78. idx = 0;
  79. while (locale_variants[idx][0] != nullptr) {
  80. variant_map[locale_variants[idx][0]] = locale_variants[idx][1];
  81. idx++;
  82. }
  83. // Init locale renames.
  84. locale_rename_map.clear();
  85. idx = 0;
  86. while (locale_renames[idx][0] != nullptr) {
  87. if (!String(locale_renames[idx][1]).is_empty()) {
  88. locale_rename_map[locale_renames[idx][0]] = locale_renames[idx][1];
  89. }
  90. idx++;
  91. }
  92. // Init country names.
  93. country_name_map.clear();
  94. idx = 0;
  95. while (country_names[idx][0] != nullptr) {
  96. country_name_map[String(country_names[idx][0])] = String::utf8(country_names[idx][1]);
  97. idx++;
  98. }
  99. // Init country renames.
  100. country_rename_map.clear();
  101. idx = 0;
  102. while (country_renames[idx][0] != nullptr) {
  103. if (!String(country_renames[idx][1]).is_empty()) {
  104. country_rename_map[country_renames[idx][0]] = country_renames[idx][1];
  105. }
  106. idx++;
  107. }
  108. }
  109. TranslationServer::Locale::operator String() const {
  110. String out = language;
  111. if (!script.is_empty()) {
  112. out = out + "_" + script;
  113. }
  114. if (!country.is_empty()) {
  115. out = out + "_" + country;
  116. }
  117. if (!variant.is_empty()) {
  118. out = out + "_" + variant;
  119. }
  120. return out;
  121. }
  122. TranslationServer::Locale::Locale(const TranslationServer &p_server, const String &p_locale, bool p_add_defaults) {
  123. // Replaces '-' with '_' for macOS style locales.
  124. String univ_locale = p_locale.replace("-", "_");
  125. // Extract locale elements.
  126. Vector<String> locale_elements = univ_locale.get_slice("@", 0).split("_");
  127. language = locale_elements[0];
  128. if (locale_elements.size() >= 2) {
  129. 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])) {
  130. script = locale_elements[1];
  131. }
  132. if (locale_elements[1].length() == 2 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_upper_case(locale_elements[1][1])) {
  133. country = locale_elements[1];
  134. }
  135. }
  136. if (locale_elements.size() >= 3) {
  137. if (locale_elements[2].length() == 2 && is_ascii_upper_case(locale_elements[2][0]) && is_ascii_upper_case(locale_elements[2][1])) {
  138. country = locale_elements[2];
  139. } else if (p_server.variant_map.has(locale_elements[2].to_lower()) && p_server.variant_map[locale_elements[2].to_lower()] == language) {
  140. variant = locale_elements[2].to_lower();
  141. }
  142. }
  143. if (locale_elements.size() >= 4) {
  144. if (p_server.variant_map.has(locale_elements[3].to_lower()) && p_server.variant_map[locale_elements[3].to_lower()] == language) {
  145. variant = locale_elements[3].to_lower();
  146. }
  147. }
  148. // Try extract script and variant from the extra part.
  149. Vector<String> script_extra = univ_locale.get_slice("@", 1).split(";");
  150. for (int i = 0; i < script_extra.size(); i++) {
  151. if (script_extra[i].to_lower() == "cyrillic") {
  152. script = "Cyrl";
  153. break;
  154. } else if (script_extra[i].to_lower() == "latin") {
  155. script = "Latn";
  156. break;
  157. } else if (script_extra[i].to_lower() == "devanagari") {
  158. script = "Deva";
  159. break;
  160. } else if (p_server.variant_map.has(script_extra[i].to_lower()) && p_server.variant_map[script_extra[i].to_lower()] == language) {
  161. variant = script_extra[i].to_lower();
  162. }
  163. }
  164. // Handles known non-ISO language names used e.g. on Windows.
  165. if (p_server.locale_rename_map.has(language)) {
  166. language = p_server.locale_rename_map[language];
  167. }
  168. // Handle country renames.
  169. if (p_server.country_rename_map.has(country)) {
  170. country = p_server.country_rename_map[country];
  171. }
  172. // Remove unsupported script codes.
  173. if (!p_server.script_map.has(script)) {
  174. script = "";
  175. }
  176. // Add script code base on language and country codes for some ambiguous cases.
  177. if (p_add_defaults) {
  178. if (script.is_empty()) {
  179. for (int i = 0; i < p_server.locale_script_info.size(); i++) {
  180. const LocaleScriptInfo &info = p_server.locale_script_info[i];
  181. if (info.name == language) {
  182. if (country.is_empty() || info.supported_countries.has(country)) {
  183. script = info.script;
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. if (!script.is_empty() && country.is_empty()) {
  190. // Add conntry code based on script for some ambiguous cases.
  191. for (int i = 0; i < p_server.locale_script_info.size(); i++) {
  192. const LocaleScriptInfo &info = p_server.locale_script_info[i];
  193. if (info.name == language && info.script == script) {
  194. country = info.default_country;
  195. break;
  196. }
  197. }
  198. }
  199. }
  200. }
  201. String TranslationServer::standardize_locale(const String &p_locale, bool p_add_defaults) const {
  202. return Locale(*this, p_locale, p_add_defaults).operator String();
  203. }
  204. int TranslationServer::compare_locales(const String &p_locale_a, const String &p_locale_b) const {
  205. if (p_locale_a == p_locale_b) {
  206. // Exact match.
  207. return 10;
  208. }
  209. const String cache_key = p_locale_a + "|" + p_locale_b;
  210. const int *cached_result = locale_compare_cache.getptr(cache_key);
  211. if (cached_result) {
  212. return *cached_result;
  213. }
  214. Locale locale_a = Locale(*this, p_locale_a, true);
  215. Locale locale_b = Locale(*this, p_locale_b, true);
  216. if (locale_a == locale_b) {
  217. // Exact match.
  218. locale_compare_cache.insert(cache_key, 10);
  219. return 10;
  220. }
  221. if (locale_a.language != locale_b.language) {
  222. // No match.
  223. locale_compare_cache.insert(cache_key, 0);
  224. return 0;
  225. }
  226. // Matching language, both locales have extra parts. Compare the
  227. // remaining elements. If both elements are non-empty, check the
  228. // match to increase or decrease the score. If either element or
  229. // both are empty, leave the score as is.
  230. int score = 5;
  231. if (!locale_a.script.is_empty() && !locale_b.script.is_empty()) {
  232. if (locale_a.script == locale_b.script) {
  233. score++;
  234. } else {
  235. score--;
  236. }
  237. }
  238. if (!locale_a.country.is_empty() && !locale_b.country.is_empty()) {
  239. if (locale_a.country == locale_b.country) {
  240. score++;
  241. } else {
  242. score--;
  243. }
  244. }
  245. if (!locale_a.variant.is_empty() && !locale_b.variant.is_empty()) {
  246. if (locale_a.variant == locale_b.variant) {
  247. score++;
  248. } else {
  249. score--;
  250. }
  251. }
  252. locale_compare_cache.insert(cache_key, score);
  253. return score;
  254. }
  255. String TranslationServer::get_locale_name(const String &p_locale) const {
  256. String lang_name, script_name, country_name;
  257. Vector<String> locale_elements = standardize_locale(p_locale).split("_");
  258. lang_name = locale_elements[0];
  259. if (locale_elements.size() >= 2) {
  260. 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])) {
  261. script_name = locale_elements[1];
  262. }
  263. if (locale_elements[1].length() == 2 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_upper_case(locale_elements[1][1])) {
  264. country_name = locale_elements[1];
  265. }
  266. }
  267. if (locale_elements.size() >= 3) {
  268. if (locale_elements[2].length() == 2 && is_ascii_upper_case(locale_elements[2][0]) && is_ascii_upper_case(locale_elements[2][1])) {
  269. country_name = locale_elements[2];
  270. }
  271. }
  272. String name = get_language_name(lang_name);
  273. if (!script_name.is_empty()) {
  274. name = name + " (" + get_script_name(script_name) + ")";
  275. }
  276. if (!country_name.is_empty()) {
  277. name = name + ", " + get_country_name(country_name);
  278. }
  279. return name;
  280. }
  281. Vector<String> TranslationServer::get_all_languages() const {
  282. Vector<String> languages;
  283. for (const KeyValue<String, String> &E : language_map) {
  284. languages.push_back(E.key);
  285. }
  286. return languages;
  287. }
  288. String TranslationServer::get_language_name(const String &p_language) const {
  289. if (language_map.has(p_language)) {
  290. return language_map[p_language];
  291. } else {
  292. return p_language;
  293. }
  294. }
  295. Vector<String> TranslationServer::get_all_scripts() const {
  296. Vector<String> scripts;
  297. for (const KeyValue<String, String> &E : script_map) {
  298. scripts.push_back(E.key);
  299. }
  300. return scripts;
  301. }
  302. String TranslationServer::get_script_name(const String &p_script) const {
  303. if (script_map.has(p_script)) {
  304. return script_map[p_script];
  305. } else {
  306. return p_script;
  307. }
  308. }
  309. Vector<String> TranslationServer::get_all_countries() const {
  310. Vector<String> countries;
  311. for (const KeyValue<String, String> &E : country_name_map) {
  312. countries.push_back(E.key);
  313. }
  314. return countries;
  315. }
  316. String TranslationServer::get_country_name(const String &p_country) const {
  317. if (country_name_map.has(p_country)) {
  318. return country_name_map[p_country];
  319. } else {
  320. return p_country;
  321. }
  322. }
  323. void TranslationServer::set_locale(const String &p_locale) {
  324. String new_locale = standardize_locale(p_locale);
  325. if (locale == new_locale) {
  326. return;
  327. }
  328. locale = new_locale;
  329. ResourceLoader::reload_translation_remaps();
  330. if (OS::get_singleton()->get_main_loop()) {
  331. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  332. }
  333. }
  334. String TranslationServer::get_locale() const {
  335. return locale;
  336. }
  337. String TranslationServer::get_fallback_locale() const {
  338. return fallback;
  339. }
  340. PackedStringArray TranslationServer::get_loaded_locales() const {
  341. return main_domain->get_loaded_locales();
  342. }
  343. void TranslationServer::add_translation(const Ref<Translation> &p_translation) {
  344. main_domain->add_translation(p_translation);
  345. }
  346. void TranslationServer::remove_translation(const Ref<Translation> &p_translation) {
  347. main_domain->remove_translation(p_translation);
  348. }
  349. Ref<Translation> TranslationServer::get_translation_object(const String &p_locale) {
  350. return main_domain->get_translation_object(p_locale);
  351. }
  352. void TranslationServer::clear() {
  353. main_domain->clear();
  354. }
  355. StringName TranslationServer::translate(const StringName &p_message, const StringName &p_context) const {
  356. if (!enabled) {
  357. return p_message;
  358. }
  359. return main_domain->translate(p_message, p_context);
  360. }
  361. StringName TranslationServer::translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  362. if (!enabled) {
  363. if (p_n == 1) {
  364. return p_message;
  365. }
  366. return p_message_plural;
  367. }
  368. return main_domain->translate_plural(p_message, p_message_plural, p_n, p_context);
  369. }
  370. bool TranslationServer::_load_translations(const String &p_from) {
  371. if (ProjectSettings::get_singleton()->has_setting(p_from)) {
  372. const Vector<String> &translation_names = GLOBAL_GET(p_from);
  373. int tcount = translation_names.size();
  374. if (tcount) {
  375. const String *r = translation_names.ptr();
  376. for (int i = 0; i < tcount; i++) {
  377. Ref<Translation> tr = ResourceLoader::load(r[i]);
  378. if (tr.is_valid()) {
  379. add_translation(tr);
  380. }
  381. }
  382. }
  383. return true;
  384. }
  385. return false;
  386. }
  387. bool TranslationServer::has_domain(const StringName &p_domain) const {
  388. if (p_domain == StringName()) {
  389. return true;
  390. }
  391. return custom_domains.has(p_domain);
  392. }
  393. Ref<TranslationDomain> TranslationServer::get_or_add_domain(const StringName &p_domain) {
  394. if (p_domain == StringName()) {
  395. return main_domain;
  396. }
  397. const Ref<TranslationDomain> *domain = custom_domains.getptr(p_domain);
  398. if (domain) {
  399. if (domain->is_valid()) {
  400. return *domain;
  401. }
  402. ERR_PRINT("Bug (please report): Found invalid translation domain.");
  403. }
  404. Ref<TranslationDomain> new_domain = memnew(TranslationDomain);
  405. custom_domains[p_domain] = new_domain;
  406. return new_domain;
  407. }
  408. void TranslationServer::remove_domain(const StringName &p_domain) {
  409. ERR_FAIL_COND_MSG(p_domain == StringName(), "Cannot remove main translation domain.");
  410. custom_domains.erase(p_domain);
  411. }
  412. void TranslationServer::setup() {
  413. String test = GLOBAL_DEF("internationalization/locale/test", "");
  414. test = test.strip_edges();
  415. if (!test.is_empty()) {
  416. set_locale(test);
  417. } else {
  418. set_locale(OS::get_singleton()->get_locale());
  419. }
  420. fallback = GLOBAL_DEF("internationalization/locale/fallback", "en");
  421. main_domain->set_pseudolocalization_enabled(GLOBAL_DEF("internationalization/pseudolocalization/use_pseudolocalization", false));
  422. main_domain->set_pseudolocalization_accents_enabled(GLOBAL_DEF("internationalization/pseudolocalization/replace_with_accents", true));
  423. main_domain->set_pseudolocalization_double_vowels_enabled(GLOBAL_DEF("internationalization/pseudolocalization/double_vowels", false));
  424. main_domain->set_pseudolocalization_fake_bidi_enabled(GLOBAL_DEF("internationalization/pseudolocalization/fake_bidi", false));
  425. main_domain->set_pseudolocalization_override_enabled(GLOBAL_DEF("internationalization/pseudolocalization/override", false));
  426. main_domain->set_pseudolocalization_expansion_ratio(GLOBAL_DEF("internationalization/pseudolocalization/expansion_ratio", 0.0));
  427. main_domain->set_pseudolocalization_prefix(GLOBAL_DEF("internationalization/pseudolocalization/prefix", "["));
  428. main_domain->set_pseudolocalization_suffix(GLOBAL_DEF("internationalization/pseudolocalization/suffix", "]"));
  429. main_domain->set_pseudolocalization_skip_placeholders_enabled(GLOBAL_DEF("internationalization/pseudolocalization/skip_placeholders", true));
  430. #ifdef TOOLS_ENABLED
  431. ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, "internationalization/locale/test", PROPERTY_HINT_LOCALE_ID, ""));
  432. ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, "internationalization/locale/fallback", PROPERTY_HINT_LOCALE_ID, ""));
  433. #endif
  434. }
  435. String TranslationServer::get_tool_locale() {
  436. #ifdef TOOLS_ENABLED
  437. if (Engine::get_singleton()->is_editor_hint() || Engine::get_singleton()->is_project_manager_hint()) {
  438. const PackedStringArray &locales = editor_domain->get_loaded_locales();
  439. if (locales.is_empty()) {
  440. return "en";
  441. }
  442. return locales[0];
  443. } else {
  444. #else
  445. {
  446. #endif
  447. // Look for best matching loaded translation.
  448. Ref<Translation> t = main_domain->get_translation_object(locale);
  449. if (t.is_null()) {
  450. return fallback;
  451. }
  452. return t->get_locale();
  453. }
  454. }
  455. StringName TranslationServer::tool_translate(const StringName &p_message, const StringName &p_context) const {
  456. return editor_domain->translate(p_message, p_context);
  457. }
  458. StringName TranslationServer::tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  459. return editor_domain->translate_plural(p_message, p_message_plural, p_n, p_context);
  460. }
  461. StringName TranslationServer::property_translate(const StringName &p_message, const StringName &p_context) const {
  462. return property_domain->translate(p_message, p_context);
  463. }
  464. StringName TranslationServer::doc_translate(const StringName &p_message, const StringName &p_context) const {
  465. return doc_domain->translate(p_message, p_context);
  466. }
  467. StringName TranslationServer::doc_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  468. return doc_domain->translate_plural(p_message, p_message_plural, p_n, p_context);
  469. }
  470. bool TranslationServer::is_pseudolocalization_enabled() const {
  471. return main_domain->is_pseudolocalization_enabled();
  472. }
  473. void TranslationServer::set_pseudolocalization_enabled(bool p_enabled) {
  474. main_domain->set_pseudolocalization_enabled(p_enabled);
  475. ResourceLoader::reload_translation_remaps();
  476. if (OS::get_singleton()->get_main_loop()) {
  477. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  478. }
  479. }
  480. void TranslationServer::reload_pseudolocalization() {
  481. main_domain->set_pseudolocalization_accents_enabled(GLOBAL_GET("internationalization/pseudolocalization/replace_with_accents"));
  482. main_domain->set_pseudolocalization_double_vowels_enabled(GLOBAL_GET("internationalization/pseudolocalization/double_vowels"));
  483. main_domain->set_pseudolocalization_fake_bidi_enabled(GLOBAL_GET("internationalization/pseudolocalization/fake_bidi"));
  484. main_domain->set_pseudolocalization_override_enabled(GLOBAL_GET("internationalization/pseudolocalization/override"));
  485. main_domain->set_pseudolocalization_expansion_ratio(GLOBAL_GET("internationalization/pseudolocalization/expansion_ratio"));
  486. main_domain->set_pseudolocalization_prefix(GLOBAL_GET("internationalization/pseudolocalization/prefix"));
  487. main_domain->set_pseudolocalization_suffix(GLOBAL_GET("internationalization/pseudolocalization/suffix"));
  488. main_domain->set_pseudolocalization_skip_placeholders_enabled(GLOBAL_GET("internationalization/pseudolocalization/skip_placeholders"));
  489. ResourceLoader::reload_translation_remaps();
  490. if (OS::get_singleton()->get_main_loop()) {
  491. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  492. }
  493. }
  494. StringName TranslationServer::pseudolocalize(const StringName &p_message) const {
  495. return main_domain->pseudolocalize(p_message);
  496. }
  497. #ifdef TOOLS_ENABLED
  498. void TranslationServer::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  499. const String pf = p_function;
  500. if (p_idx == 0) {
  501. HashMap<String, String> *target_hash_map = nullptr;
  502. if (pf == "get_language_name") {
  503. target_hash_map = &language_map;
  504. } else if (pf == "get_script_name") {
  505. target_hash_map = &script_map;
  506. } else if (pf == "get_country_name") {
  507. target_hash_map = &country_name_map;
  508. }
  509. if (target_hash_map) {
  510. for (const KeyValue<String, String> &E : *target_hash_map) {
  511. r_options->push_back(E.key.quote());
  512. }
  513. }
  514. }
  515. Object::get_argument_options(p_function, p_idx, r_options);
  516. }
  517. #endif // TOOLS_ENABLED
  518. void TranslationServer::_bind_methods() {
  519. ClassDB::bind_method(D_METHOD("set_locale", "locale"), &TranslationServer::set_locale);
  520. ClassDB::bind_method(D_METHOD("get_locale"), &TranslationServer::get_locale);
  521. ClassDB::bind_method(D_METHOD("get_tool_locale"), &TranslationServer::get_tool_locale);
  522. ClassDB::bind_method(D_METHOD("compare_locales", "locale_a", "locale_b"), &TranslationServer::compare_locales);
  523. ClassDB::bind_method(D_METHOD("standardize_locale", "locale", "add_defaults"), &TranslationServer::standardize_locale, DEFVAL(false));
  524. ClassDB::bind_method(D_METHOD("get_all_languages"), &TranslationServer::get_all_languages);
  525. ClassDB::bind_method(D_METHOD("get_language_name", "language"), &TranslationServer::get_language_name);
  526. ClassDB::bind_method(D_METHOD("get_all_scripts"), &TranslationServer::get_all_scripts);
  527. ClassDB::bind_method(D_METHOD("get_script_name", "script"), &TranslationServer::get_script_name);
  528. ClassDB::bind_method(D_METHOD("get_all_countries"), &TranslationServer::get_all_countries);
  529. ClassDB::bind_method(D_METHOD("get_country_name", "country"), &TranslationServer::get_country_name);
  530. ClassDB::bind_method(D_METHOD("get_locale_name", "locale"), &TranslationServer::get_locale_name);
  531. ClassDB::bind_method(D_METHOD("translate", "message", "context"), &TranslationServer::translate, DEFVAL(StringName()));
  532. ClassDB::bind_method(D_METHOD("translate_plural", "message", "plural_message", "n", "context"), &TranslationServer::translate_plural, DEFVAL(StringName()));
  533. ClassDB::bind_method(D_METHOD("add_translation", "translation"), &TranslationServer::add_translation);
  534. ClassDB::bind_method(D_METHOD("remove_translation", "translation"), &TranslationServer::remove_translation);
  535. ClassDB::bind_method(D_METHOD("get_translation_object", "locale"), &TranslationServer::get_translation_object);
  536. ClassDB::bind_method(D_METHOD("has_domain", "domain"), &TranslationServer::has_domain);
  537. ClassDB::bind_method(D_METHOD("get_or_add_domain", "domain"), &TranslationServer::get_or_add_domain);
  538. ClassDB::bind_method(D_METHOD("remove_domain", "domain"), &TranslationServer::remove_domain);
  539. ClassDB::bind_method(D_METHOD("clear"), &TranslationServer::clear);
  540. ClassDB::bind_method(D_METHOD("get_loaded_locales"), &TranslationServer::get_loaded_locales);
  541. ClassDB::bind_method(D_METHOD("is_pseudolocalization_enabled"), &TranslationServer::is_pseudolocalization_enabled);
  542. ClassDB::bind_method(D_METHOD("set_pseudolocalization_enabled", "enabled"), &TranslationServer::set_pseudolocalization_enabled);
  543. ClassDB::bind_method(D_METHOD("reload_pseudolocalization"), &TranslationServer::reload_pseudolocalization);
  544. ClassDB::bind_method(D_METHOD("pseudolocalize", "message"), &TranslationServer::pseudolocalize);
  545. ADD_PROPERTY(PropertyInfo(Variant::Type::BOOL, "pseudolocalization_enabled"), "set_pseudolocalization_enabled", "is_pseudolocalization_enabled");
  546. }
  547. void TranslationServer::load_translations() {
  548. _load_translations("internationalization/locale/translations"); //all
  549. _load_translations("internationalization/locale/translations_" + locale.substr(0, 2));
  550. if (locale.substr(0, 2) != locale) {
  551. _load_translations("internationalization/locale/translations_" + locale);
  552. }
  553. }
  554. TranslationServer::TranslationServer() {
  555. singleton = this;
  556. main_domain.instantiate();
  557. editor_domain = get_or_add_domain("godot.editor");
  558. property_domain = get_or_add_domain("godot.properties");
  559. doc_domain = get_or_add_domain("godot.documentation");
  560. init_locale_info();
  561. }