translation_server.cpp 28 KB

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