translation_server.cpp 24 KB

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