translation_server.cpp 23 KB

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