translation_server.cpp 24 KB

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