translation_server.cpp 24 KB

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