translation_server.cpp 27 KB

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