translation_server.cpp 23 KB

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