translation_server.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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 = language_map[lang_name];
  273. if (!script_name.is_empty()) {
  274. name = name + " (" + script_map[script_name] + ")";
  275. }
  276. if (!country_name.is_empty()) {
  277. name = name + ", " + country_name_map[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. return language_map[p_language];
  290. }
  291. Vector<String> TranslationServer::get_all_scripts() const {
  292. Vector<String> scripts;
  293. for (const KeyValue<String, String> &E : script_map) {
  294. scripts.push_back(E.key);
  295. }
  296. return scripts;
  297. }
  298. String TranslationServer::get_script_name(const String &p_script) const {
  299. return script_map[p_script];
  300. }
  301. Vector<String> TranslationServer::get_all_countries() const {
  302. Vector<String> countries;
  303. for (const KeyValue<String, String> &E : country_name_map) {
  304. countries.push_back(E.key);
  305. }
  306. return countries;
  307. }
  308. String TranslationServer::get_country_name(const String &p_country) const {
  309. return country_name_map[p_country];
  310. }
  311. void TranslationServer::set_locale(const String &p_locale) {
  312. String new_locale = standardize_locale(p_locale);
  313. if (locale == new_locale) {
  314. return;
  315. }
  316. locale = new_locale;
  317. ResourceLoader::reload_translation_remaps();
  318. if (OS::get_singleton()->get_main_loop()) {
  319. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  320. }
  321. }
  322. String TranslationServer::get_locale() const {
  323. return locale;
  324. }
  325. String TranslationServer::get_fallback_locale() const {
  326. return fallback;
  327. }
  328. PackedStringArray TranslationServer::get_loaded_locales() const {
  329. return main_domain->get_loaded_locales();
  330. }
  331. void TranslationServer::add_translation(const Ref<Translation> &p_translation) {
  332. main_domain->add_translation(p_translation);
  333. }
  334. void TranslationServer::remove_translation(const Ref<Translation> &p_translation) {
  335. main_domain->remove_translation(p_translation);
  336. }
  337. Ref<Translation> TranslationServer::get_translation_object(const String &p_locale) {
  338. return main_domain->get_translation_object(p_locale);
  339. }
  340. void TranslationServer::clear() {
  341. main_domain->clear();
  342. }
  343. StringName TranslationServer::translate(const StringName &p_message, const StringName &p_context) const {
  344. if (!enabled) {
  345. return p_message;
  346. }
  347. return main_domain->translate(p_message, p_context);
  348. }
  349. StringName TranslationServer::translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  350. if (!enabled) {
  351. if (p_n == 1) {
  352. return p_message;
  353. }
  354. return p_message_plural;
  355. }
  356. return main_domain->translate_plural(p_message, p_message_plural, p_n, p_context);
  357. }
  358. bool TranslationServer::_load_translations(const String &p_from) {
  359. if (ProjectSettings::get_singleton()->has_setting(p_from)) {
  360. const Vector<String> &translation_names = GLOBAL_GET(p_from);
  361. int tcount = translation_names.size();
  362. if (tcount) {
  363. const String *r = translation_names.ptr();
  364. for (int i = 0; i < tcount; i++) {
  365. Ref<Translation> tr = ResourceLoader::load(r[i]);
  366. if (tr.is_valid()) {
  367. add_translation(tr);
  368. }
  369. }
  370. }
  371. return true;
  372. }
  373. return false;
  374. }
  375. bool TranslationServer::has_domain(const StringName &p_domain) const {
  376. if (p_domain == StringName()) {
  377. return true;
  378. }
  379. return custom_domains.has(p_domain);
  380. }
  381. Ref<TranslationDomain> TranslationServer::get_or_add_domain(const StringName &p_domain) {
  382. if (p_domain == StringName()) {
  383. return main_domain;
  384. }
  385. const Ref<TranslationDomain> *domain = custom_domains.getptr(p_domain);
  386. if (domain) {
  387. if (domain->is_valid()) {
  388. return *domain;
  389. }
  390. ERR_PRINT("Bug (please report): Found invalid translation domain.");
  391. }
  392. Ref<TranslationDomain> new_domain = memnew(TranslationDomain);
  393. custom_domains[p_domain] = new_domain;
  394. return new_domain;
  395. }
  396. void TranslationServer::remove_domain(const StringName &p_domain) {
  397. ERR_FAIL_COND_MSG(p_domain == StringName(), "Cannot remove main translation domain.");
  398. custom_domains.erase(p_domain);
  399. }
  400. void TranslationServer::setup() {
  401. String test = GLOBAL_DEF("internationalization/locale/test", "");
  402. test = test.strip_edges();
  403. if (!test.is_empty()) {
  404. set_locale(test);
  405. } else {
  406. set_locale(OS::get_singleton()->get_locale());
  407. }
  408. fallback = GLOBAL_DEF("internationalization/locale/fallback", "en");
  409. main_domain->set_pseudolocalization_enabled(GLOBAL_DEF("internationalization/pseudolocalization/use_pseudolocalization", false));
  410. main_domain->set_pseudolocalization_accents_enabled(GLOBAL_DEF("internationalization/pseudolocalization/replace_with_accents", true));
  411. main_domain->set_pseudolocalization_double_vowels_enabled(GLOBAL_DEF("internationalization/pseudolocalization/double_vowels", false));
  412. main_domain->set_pseudolocalization_fake_bidi_enabled(GLOBAL_DEF("internationalization/pseudolocalization/fake_bidi", false));
  413. main_domain->set_pseudolocalization_override_enabled(GLOBAL_DEF("internationalization/pseudolocalization/override", false));
  414. main_domain->set_pseudolocalization_expansion_ratio(GLOBAL_DEF("internationalization/pseudolocalization/expansion_ratio", 0.0));
  415. main_domain->set_pseudolocalization_prefix(GLOBAL_DEF("internationalization/pseudolocalization/prefix", "["));
  416. main_domain->set_pseudolocalization_suffix(GLOBAL_DEF("internationalization/pseudolocalization/suffix", "]"));
  417. main_domain->set_pseudolocalization_skip_placeholders_enabled(GLOBAL_DEF("internationalization/pseudolocalization/skip_placeholders", true));
  418. #ifdef TOOLS_ENABLED
  419. ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, "internationalization/locale/test", PROPERTY_HINT_LOCALE_ID, ""));
  420. ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, "internationalization/locale/fallback", PROPERTY_HINT_LOCALE_ID, ""));
  421. #endif
  422. }
  423. String TranslationServer::get_tool_locale() {
  424. #ifdef TOOLS_ENABLED
  425. if (Engine::get_singleton()->is_editor_hint() || Engine::get_singleton()->is_project_manager_hint()) {
  426. const PackedStringArray &locales = editor_domain->get_loaded_locales();
  427. if (locales.is_empty()) {
  428. return "en";
  429. }
  430. return locales[0];
  431. } else {
  432. #else
  433. {
  434. #endif
  435. // Look for best matching loaded translation.
  436. Ref<Translation> t = main_domain->get_translation_object(locale);
  437. if (t.is_null()) {
  438. return fallback;
  439. }
  440. return t->get_locale();
  441. }
  442. }
  443. StringName TranslationServer::tool_translate(const StringName &p_message, const StringName &p_context) const {
  444. return editor_domain->translate(p_message, p_context);
  445. }
  446. StringName TranslationServer::tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  447. return editor_domain->translate_plural(p_message, p_message_plural, p_n, p_context);
  448. }
  449. StringName TranslationServer::property_translate(const StringName &p_message, const StringName &p_context) const {
  450. return property_domain->translate(p_message, p_context);
  451. }
  452. StringName TranslationServer::doc_translate(const StringName &p_message, const StringName &p_context) const {
  453. return doc_domain->translate(p_message, p_context);
  454. }
  455. StringName TranslationServer::doc_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  456. return doc_domain->translate_plural(p_message, p_message_plural, p_n, p_context);
  457. }
  458. bool TranslationServer::is_pseudolocalization_enabled() const {
  459. return main_domain->is_pseudolocalization_enabled();
  460. }
  461. void TranslationServer::set_pseudolocalization_enabled(bool p_enabled) {
  462. main_domain->set_pseudolocalization_enabled(p_enabled);
  463. ResourceLoader::reload_translation_remaps();
  464. if (OS::get_singleton()->get_main_loop()) {
  465. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  466. }
  467. }
  468. void TranslationServer::reload_pseudolocalization() {
  469. main_domain->set_pseudolocalization_accents_enabled(GLOBAL_GET("internationalization/pseudolocalization/replace_with_accents"));
  470. main_domain->set_pseudolocalization_double_vowels_enabled(GLOBAL_GET("internationalization/pseudolocalization/double_vowels"));
  471. main_domain->set_pseudolocalization_fake_bidi_enabled(GLOBAL_GET("internationalization/pseudolocalization/fake_bidi"));
  472. main_domain->set_pseudolocalization_override_enabled(GLOBAL_GET("internationalization/pseudolocalization/override"));
  473. main_domain->set_pseudolocalization_expansion_ratio(GLOBAL_GET("internationalization/pseudolocalization/expansion_ratio"));
  474. main_domain->set_pseudolocalization_prefix(GLOBAL_GET("internationalization/pseudolocalization/prefix"));
  475. main_domain->set_pseudolocalization_suffix(GLOBAL_GET("internationalization/pseudolocalization/suffix"));
  476. main_domain->set_pseudolocalization_skip_placeholders_enabled(GLOBAL_GET("internationalization/pseudolocalization/skip_placeholders"));
  477. ResourceLoader::reload_translation_remaps();
  478. if (OS::get_singleton()->get_main_loop()) {
  479. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  480. }
  481. }
  482. StringName TranslationServer::pseudolocalize(const StringName &p_message) const {
  483. return main_domain->pseudolocalize(p_message);
  484. }
  485. #ifdef TOOLS_ENABLED
  486. void TranslationServer::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  487. const String pf = p_function;
  488. if (p_idx == 0) {
  489. HashMap<String, String> *target_hash_map = nullptr;
  490. if (pf == "get_language_name") {
  491. target_hash_map = &language_map;
  492. } else if (pf == "get_script_name") {
  493. target_hash_map = &script_map;
  494. } else if (pf == "get_country_name") {
  495. target_hash_map = &country_name_map;
  496. }
  497. if (target_hash_map) {
  498. for (const KeyValue<String, String> &E : *target_hash_map) {
  499. r_options->push_back(E.key.quote());
  500. }
  501. }
  502. }
  503. Object::get_argument_options(p_function, p_idx, r_options);
  504. }
  505. #endif // TOOLS_ENABLED
  506. void TranslationServer::_bind_methods() {
  507. ClassDB::bind_method(D_METHOD("set_locale", "locale"), &TranslationServer::set_locale);
  508. ClassDB::bind_method(D_METHOD("get_locale"), &TranslationServer::get_locale);
  509. ClassDB::bind_method(D_METHOD("get_tool_locale"), &TranslationServer::get_tool_locale);
  510. ClassDB::bind_method(D_METHOD("compare_locales", "locale_a", "locale_b"), &TranslationServer::compare_locales);
  511. ClassDB::bind_method(D_METHOD("standardize_locale", "locale", "add_defaults"), &TranslationServer::standardize_locale, DEFVAL(false));
  512. ClassDB::bind_method(D_METHOD("get_all_languages"), &TranslationServer::get_all_languages);
  513. ClassDB::bind_method(D_METHOD("get_language_name", "language"), &TranslationServer::get_language_name);
  514. ClassDB::bind_method(D_METHOD("get_all_scripts"), &TranslationServer::get_all_scripts);
  515. ClassDB::bind_method(D_METHOD("get_script_name", "script"), &TranslationServer::get_script_name);
  516. ClassDB::bind_method(D_METHOD("get_all_countries"), &TranslationServer::get_all_countries);
  517. ClassDB::bind_method(D_METHOD("get_country_name", "country"), &TranslationServer::get_country_name);
  518. ClassDB::bind_method(D_METHOD("get_locale_name", "locale"), &TranslationServer::get_locale_name);
  519. ClassDB::bind_method(D_METHOD("translate", "message", "context"), &TranslationServer::translate, DEFVAL(StringName()));
  520. ClassDB::bind_method(D_METHOD("translate_plural", "message", "plural_message", "n", "context"), &TranslationServer::translate_plural, DEFVAL(StringName()));
  521. ClassDB::bind_method(D_METHOD("add_translation", "translation"), &TranslationServer::add_translation);
  522. ClassDB::bind_method(D_METHOD("remove_translation", "translation"), &TranslationServer::remove_translation);
  523. ClassDB::bind_method(D_METHOD("get_translation_object", "locale"), &TranslationServer::get_translation_object);
  524. ClassDB::bind_method(D_METHOD("has_domain", "domain"), &TranslationServer::has_domain);
  525. ClassDB::bind_method(D_METHOD("get_or_add_domain", "domain"), &TranslationServer::get_or_add_domain);
  526. ClassDB::bind_method(D_METHOD("remove_domain", "domain"), &TranslationServer::remove_domain);
  527. ClassDB::bind_method(D_METHOD("clear"), &TranslationServer::clear);
  528. ClassDB::bind_method(D_METHOD("get_loaded_locales"), &TranslationServer::get_loaded_locales);
  529. ClassDB::bind_method(D_METHOD("is_pseudolocalization_enabled"), &TranslationServer::is_pseudolocalization_enabled);
  530. ClassDB::bind_method(D_METHOD("set_pseudolocalization_enabled", "enabled"), &TranslationServer::set_pseudolocalization_enabled);
  531. ClassDB::bind_method(D_METHOD("reload_pseudolocalization"), &TranslationServer::reload_pseudolocalization);
  532. ClassDB::bind_method(D_METHOD("pseudolocalize", "message"), &TranslationServer::pseudolocalize);
  533. ADD_PROPERTY(PropertyInfo(Variant::Type::BOOL, "pseudolocalization_enabled"), "set_pseudolocalization_enabled", "is_pseudolocalization_enabled");
  534. }
  535. void TranslationServer::load_translations() {
  536. _load_translations("internationalization/locale/translations"); //all
  537. _load_translations("internationalization/locale/translations_" + locale.substr(0, 2));
  538. if (locale.substr(0, 2) != locale) {
  539. _load_translations("internationalization/locale/translations_" + locale);
  540. }
  541. }
  542. TranslationServer::TranslationServer() {
  543. singleton = this;
  544. main_domain.instantiate();
  545. editor_domain = get_or_add_domain("godot.editor");
  546. property_domain = get_or_add_domain("godot.properties");
  547. doc_domain = get_or_add_domain("godot.documentation");
  548. init_locale_info();
  549. }