translation.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /**************************************************************************/
  2. /* translation.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.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/locales.h"
  33. #include "core/os/os.h"
  34. #include "core/project_settings.h"
  35. PoolVector<String> Translation::_get_messages() const {
  36. PoolVector<String> msgs;
  37. msgs.resize(translation_map.size() * 2);
  38. int idx = 0;
  39. for (const Map<StringName, StringName>::Element *E = translation_map.front(); E; E = E->next()) {
  40. msgs.set(idx + 0, E->key());
  41. msgs.set(idx + 1, E->get());
  42. idx += 2;
  43. }
  44. return msgs;
  45. }
  46. PoolVector<String> Translation::_get_message_list() const {
  47. PoolVector<String> msgs;
  48. msgs.resize(translation_map.size());
  49. int idx = 0;
  50. for (const Map<StringName, StringName>::Element *E = translation_map.front(); E; E = E->next()) {
  51. msgs.set(idx, E->key());
  52. idx += 1;
  53. }
  54. return msgs;
  55. }
  56. void Translation::_set_messages(const PoolVector<String> &p_messages) {
  57. int msg_count = p_messages.size();
  58. ERR_FAIL_COND(msg_count % 2);
  59. PoolVector<String>::Read r = p_messages.read();
  60. for (int i = 0; i < msg_count; i += 2) {
  61. add_message(r[i + 0], r[i + 1]);
  62. }
  63. }
  64. void Translation::set_locale(const String &p_locale) {
  65. locale = TranslationServer::get_singleton()->standardize_locale(p_locale);
  66. if (OS::get_singleton()->get_main_loop()) {
  67. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  68. }
  69. }
  70. void Translation::add_context_message(const StringName &p_src_text, const StringName &p_xlated_text, const StringName &p_context) {
  71. if (p_context != StringName()) {
  72. WARN_PRINT("Translation class doesn't handle context.");
  73. }
  74. add_message(p_src_text, p_xlated_text);
  75. }
  76. StringName Translation::get_context_message(const StringName &p_src_text, const StringName &p_context) const {
  77. if (p_context != StringName()) {
  78. WARN_PRINT("Translation class doesn't handle context.");
  79. }
  80. return get_message(p_src_text);
  81. }
  82. void Translation::add_message(const StringName &p_src_text, const StringName &p_xlated_text) {
  83. translation_map[p_src_text] = p_xlated_text;
  84. }
  85. StringName Translation::get_message(const StringName &p_src_text) const {
  86. if (get_script_instance()) {
  87. return get_script_instance()->call("_get_message", p_src_text);
  88. }
  89. const Map<StringName, StringName>::Element *E = translation_map.find(p_src_text);
  90. if (!E) {
  91. return StringName();
  92. }
  93. return E->get();
  94. }
  95. void Translation::erase_message(const StringName &p_src_text) {
  96. translation_map.erase(p_src_text);
  97. }
  98. void Translation::get_message_list(List<StringName> *r_messages) const {
  99. for (const Map<StringName, StringName>::Element *E = translation_map.front(); E; E = E->next()) {
  100. r_messages->push_back(E->key());
  101. }
  102. }
  103. int Translation::get_message_count() const {
  104. return translation_map.size();
  105. };
  106. void Translation::_bind_methods() {
  107. ClassDB::bind_method(D_METHOD("set_locale", "locale"), &Translation::set_locale);
  108. ClassDB::bind_method(D_METHOD("get_locale"), &Translation::get_locale);
  109. ClassDB::bind_method(D_METHOD("add_message", "src_message", "xlated_message"), &Translation::add_message);
  110. ClassDB::bind_method(D_METHOD("get_message", "src_message"), &Translation::get_message);
  111. ClassDB::bind_method(D_METHOD("erase_message", "src_message"), &Translation::erase_message);
  112. ClassDB::bind_method(D_METHOD("get_message_list"), &Translation::_get_message_list);
  113. ClassDB::bind_method(D_METHOD("get_message_count"), &Translation::get_message_count);
  114. ClassDB::bind_method(D_METHOD("_set_messages"), &Translation::_set_messages);
  115. ClassDB::bind_method(D_METHOD("_get_messages"), &Translation::_get_messages);
  116. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_message", PropertyInfo(Variant::STRING, "src_message")));
  117. ADD_PROPERTY(PropertyInfo(Variant::POOL_STRING_ARRAY, "messages", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_messages", "_get_messages");
  118. ADD_PROPERTY(PropertyInfo(Variant::STRING, "locale"), "set_locale", "get_locale");
  119. }
  120. Translation::Translation() :
  121. locale("en") {
  122. }
  123. ///////////////////////////////////////////////
  124. void ContextTranslation::add_context_message(const StringName &p_src_text, const StringName &p_xlated_text, const StringName &p_context) {
  125. if (p_context == StringName()) {
  126. add_message(p_src_text, p_xlated_text);
  127. } else {
  128. context_translation_map[p_context][p_src_text] = p_xlated_text;
  129. }
  130. }
  131. StringName ContextTranslation::get_context_message(const StringName &p_src_text, const StringName &p_context) const {
  132. if (p_context == StringName()) {
  133. return get_message(p_src_text);
  134. }
  135. const Map<StringName, Map<StringName, StringName>>::Element *context = context_translation_map.find(p_context);
  136. if (!context) {
  137. return StringName();
  138. }
  139. const Map<StringName, StringName>::Element *message = context->get().find(p_src_text);
  140. if (!message) {
  141. return StringName();
  142. }
  143. return message->get();
  144. }
  145. ///////////////////////////////////////////////
  146. static _FORCE_INLINE_ bool is_ascii_upper_case(char32_t c) {
  147. return (c >= 'A' && c <= 'Z');
  148. }
  149. static _FORCE_INLINE_ bool is_ascii_lower_case(char32_t c) {
  150. return (c >= 'a' && c <= 'z');
  151. }
  152. Vector<TranslationServer::LocaleScriptInfo> TranslationServer::locale_script_info;
  153. Map<String, String> TranslationServer::language_map;
  154. Map<String, String> TranslationServer::script_map;
  155. Map<String, String> TranslationServer::locale_rename_map;
  156. Map<String, String> TranslationServer::country_name_map;
  157. Map<String, String> TranslationServer::variant_map;
  158. Map<String, String> TranslationServer::country_rename_map;
  159. void TranslationServer::init_locale_info() {
  160. // Init locale info.
  161. language_map.clear();
  162. int idx = 0;
  163. while (language_list[idx][0] != nullptr) {
  164. language_map[language_list[idx][0]] = String::utf8(language_list[idx][1]);
  165. idx++;
  166. }
  167. // Init locale-script map.
  168. locale_script_info.clear();
  169. idx = 0;
  170. while (locale_scripts[idx][0] != nullptr) {
  171. LocaleScriptInfo info;
  172. info.name = locale_scripts[idx][0];
  173. info.script = locale_scripts[idx][1];
  174. info.default_country = locale_scripts[idx][2];
  175. Vector<String> supported_countries = String(locale_scripts[idx][3]).split(",", false);
  176. for (int i = 0; i < supported_countries.size(); i++) {
  177. info.supported_countries.insert(supported_countries[i]);
  178. }
  179. locale_script_info.push_back(info);
  180. idx++;
  181. }
  182. // Init supported script list.
  183. script_map.clear();
  184. idx = 0;
  185. while (script_list[idx][0] != nullptr) {
  186. script_map[script_list[idx][1]] = String::utf8(script_list[idx][0]);
  187. idx++;
  188. }
  189. // Init regional variant map.
  190. variant_map.clear();
  191. idx = 0;
  192. while (locale_variants[idx][0] != nullptr) {
  193. variant_map[locale_variants[idx][0]] = locale_variants[idx][1];
  194. idx++;
  195. }
  196. // Init locale renames.
  197. locale_rename_map.clear();
  198. idx = 0;
  199. while (locale_renames[idx][0] != nullptr) {
  200. if (!String(locale_renames[idx][1]).empty()) {
  201. locale_rename_map[locale_renames[idx][0]] = locale_renames[idx][1];
  202. }
  203. idx++;
  204. }
  205. // Init country names.
  206. country_name_map.clear();
  207. idx = 0;
  208. while (country_names[idx][0] != nullptr) {
  209. country_name_map[String(country_names[idx][0])] = String::utf8(country_names[idx][1]);
  210. idx++;
  211. }
  212. // Init country renames.
  213. country_rename_map.clear();
  214. idx = 0;
  215. while (country_renames[idx][0] != nullptr) {
  216. if (!String(country_renames[idx][1]).empty()) {
  217. country_rename_map[country_renames[idx][0]] = country_renames[idx][1];
  218. }
  219. idx++;
  220. }
  221. }
  222. String TranslationServer::standardize_locale(const String &p_locale) const {
  223. return _standardize_locale(p_locale, false);
  224. }
  225. String TranslationServer::_standardize_locale(const String &p_locale, bool p_add_defaults) const {
  226. // Replaces '-' with '_' for macOS style locales.
  227. String univ_locale = p_locale.replace("-", "_");
  228. // Extract locale elements.
  229. String lang, script, country, variant;
  230. Vector<String> locale_elements = univ_locale.get_slice("@", 0).split("_");
  231. lang = locale_elements[0];
  232. if (locale_elements.size() >= 2) {
  233. 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])) {
  234. script = locale_elements[1];
  235. }
  236. if (locale_elements[1].length() == 2 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_upper_case(locale_elements[1][1])) {
  237. country = locale_elements[1];
  238. }
  239. }
  240. if (locale_elements.size() >= 3) {
  241. if (locale_elements[2].length() == 2 && is_ascii_upper_case(locale_elements[2][0]) && is_ascii_upper_case(locale_elements[2][1])) {
  242. country = locale_elements[2];
  243. } else if (variant_map.has(locale_elements[2].to_lower()) && variant_map[locale_elements[2].to_lower()] == lang) {
  244. variant = locale_elements[2].to_lower();
  245. }
  246. }
  247. if (locale_elements.size() >= 4) {
  248. if (variant_map.has(locale_elements[3].to_lower()) && variant_map[locale_elements[3].to_lower()] == lang) {
  249. variant = locale_elements[3].to_lower();
  250. }
  251. }
  252. // Try extract script and variant from the extra part.
  253. Vector<String> script_extra = univ_locale.get_slice("@", 1).split(";");
  254. for (int i = 0; i < script_extra.size(); i++) {
  255. if (script_extra[i].to_lower() == "cyrillic") {
  256. script = "Cyrl";
  257. break;
  258. } else if (script_extra[i].to_lower() == "latin") {
  259. script = "Latn";
  260. break;
  261. } else if (script_extra[i].to_lower() == "devanagari") {
  262. script = "Deva";
  263. break;
  264. } else if (variant_map.has(script_extra[i].to_lower()) && variant_map[script_extra[i].to_lower()] == lang) {
  265. variant = script_extra[i].to_lower();
  266. }
  267. }
  268. // Handles known non-ISO language names used e.g. on Windows.
  269. if (locale_rename_map.has(lang)) {
  270. lang = locale_rename_map[lang];
  271. }
  272. // Handle country renames.
  273. if (country_rename_map.has(country)) {
  274. country = country_rename_map[country];
  275. }
  276. // Remove unsupported script codes.
  277. if (!script_map.has(script)) {
  278. script = "";
  279. }
  280. // Add script code base on language and country codes for some ambiguous cases.
  281. if (p_add_defaults) {
  282. if (script.empty()) {
  283. for (int i = 0; i < locale_script_info.size(); i++) {
  284. const LocaleScriptInfo &info = locale_script_info[i];
  285. if (info.name == lang) {
  286. if (country.empty() || info.supported_countries.has(country)) {
  287. script = info.script;
  288. break;
  289. }
  290. }
  291. }
  292. }
  293. if (!script.empty() && country.empty()) {
  294. // Add conntry code based on script for some ambiguous cases.
  295. for (int i = 0; i < locale_script_info.size(); i++) {
  296. const LocaleScriptInfo &info = locale_script_info[i];
  297. if (info.name == lang && info.script == script) {
  298. country = info.default_country;
  299. break;
  300. }
  301. }
  302. }
  303. }
  304. // Combine results.
  305. String locale = lang;
  306. if (!script.empty()) {
  307. locale = locale + "_" + script;
  308. }
  309. if (!country.empty()) {
  310. locale = locale + "_" + country;
  311. }
  312. if (!variant.empty()) {
  313. locale = locale + "_" + variant;
  314. }
  315. return locale;
  316. }
  317. int TranslationServer::compare_locales(const String &p_locale_a, const String &p_locale_b) const {
  318. if (p_locale_a == p_locale_b) {
  319. // Exact match.
  320. return 10;
  321. }
  322. const String cache_key = p_locale_a + "|" + p_locale_b;
  323. const int *cached_result = locale_compare_cache.getptr(cache_key);
  324. if (cached_result) {
  325. return *cached_result;
  326. }
  327. String locale_a = _standardize_locale(p_locale_a, true);
  328. String locale_b = _standardize_locale(p_locale_b, true);
  329. if (locale_a == locale_b) {
  330. // Exact match.
  331. locale_compare_cache.set(cache_key, 10);
  332. return 10;
  333. }
  334. Vector<String> locale_a_elements = locale_a.split("_");
  335. Vector<String> locale_b_elements = locale_b.split("_");
  336. if (locale_a_elements[0] != locale_b_elements[0]) {
  337. // No match.
  338. locale_compare_cache.set(cache_key, 0);
  339. return 0;
  340. }
  341. // Matching language, both locales have extra parts.
  342. // Return number of matching elements.
  343. int matching_elements = 1;
  344. for (int i = 1; i < locale_a_elements.size(); i++) {
  345. for (int j = 1; j < locale_b_elements.size(); j++) {
  346. if (locale_a_elements[i] == locale_b_elements[j]) {
  347. matching_elements++;
  348. }
  349. }
  350. }
  351. locale_compare_cache.set(cache_key, matching_elements);
  352. return matching_elements;
  353. }
  354. String TranslationServer::get_locale_name(const String &p_locale) const {
  355. String locale = standardize_locale(p_locale);
  356. String lang, script, country;
  357. Vector<String> locale_elements = locale.split("_");
  358. lang = locale_elements[0];
  359. if (locale_elements.size() >= 2) {
  360. 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])) {
  361. script = locale_elements[1];
  362. }
  363. if (locale_elements[1].length() == 2 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_upper_case(locale_elements[1][1])) {
  364. country = locale_elements[1];
  365. }
  366. }
  367. if (locale_elements.size() >= 3) {
  368. if (locale_elements[2].length() == 2 && is_ascii_upper_case(locale_elements[2][0]) && is_ascii_upper_case(locale_elements[2][1])) {
  369. country = locale_elements[2];
  370. }
  371. }
  372. String name = language_map[lang];
  373. if (!script.empty()) {
  374. name = name + " (" + script_map[script] + ")";
  375. }
  376. if (!country.empty()) {
  377. name = name + ", " + country_name_map[country];
  378. }
  379. return name;
  380. }
  381. Vector<String> TranslationServer::get_all_languages() const {
  382. Vector<String> languages;
  383. for (Map<String, String>::Element *E = language_map.front(); E; E = E->next()) {
  384. languages.push_back(E->key());
  385. }
  386. return languages;
  387. }
  388. String TranslationServer::get_language_name(const String &p_language) const {
  389. return language_map[p_language];
  390. }
  391. Vector<String> TranslationServer::get_all_scripts() const {
  392. Vector<String> scripts;
  393. for (Map<String, String>::Element *E = script_map.front(); E; E = E->next()) {
  394. scripts.push_back(E->key());
  395. }
  396. return scripts;
  397. }
  398. String TranslationServer::get_script_name(const String &p_script) const {
  399. return script_map[p_script];
  400. }
  401. Vector<String> TranslationServer::get_all_countries() const {
  402. Vector<String> countries;
  403. for (Map<String, String>::Element *E = country_name_map.front(); E; E = E->next()) {
  404. countries.push_back(E->key());
  405. }
  406. return countries;
  407. }
  408. String TranslationServer::get_country_name(const String &p_country) const {
  409. return country_name_map[p_country];
  410. }
  411. void TranslationServer::set_locale(const String &p_locale) {
  412. locale = standardize_locale(p_locale);
  413. if (OS::get_singleton()->get_main_loop()) {
  414. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  415. }
  416. ResourceLoader::reload_translation_remaps();
  417. }
  418. String TranslationServer::get_locale() const {
  419. return locale;
  420. }
  421. Array TranslationServer::get_loaded_locales() const {
  422. Array locales;
  423. for (Set<Ref<Translation>>::Element *E = translations.front(); E; E = E->next()) {
  424. const Ref<Translation> &t = E->get();
  425. ERR_FAIL_COND_V(t.is_null(), Array());
  426. String l = t->get_locale();
  427. locales.push_back(l);
  428. }
  429. return locales;
  430. }
  431. void TranslationServer::add_translation(const Ref<Translation> &p_translation) {
  432. translations.insert(p_translation);
  433. }
  434. void TranslationServer::remove_translation(const Ref<Translation> &p_translation) {
  435. translations.erase(p_translation);
  436. }
  437. void TranslationServer::clear() {
  438. translations.clear();
  439. };
  440. StringName TranslationServer::translate(const StringName &p_message) const {
  441. // Match given message against the translation catalog for the project locale.
  442. if (!enabled) {
  443. return p_message;
  444. }
  445. StringName res;
  446. int best_score = 0;
  447. for (const Set<Ref<Translation>>::Element *E = translations.front(); E; E = E->next()) {
  448. const Ref<Translation> &t = E->get();
  449. ERR_FAIL_COND_V(t.is_null(), p_message);
  450. String l = t->get_locale();
  451. int score = compare_locales(locale, l);
  452. if (score > 0 && score >= best_score) {
  453. StringName r = t->get_message(p_message);
  454. if (!r) {
  455. continue;
  456. }
  457. res = r;
  458. best_score = score;
  459. if (score == 10) {
  460. break; // Exact match, skip the rest.
  461. }
  462. }
  463. }
  464. if (!res && fallback.length() >= 2) {
  465. best_score = 0;
  466. for (const Set<Ref<Translation>>::Element *E = translations.front(); E; E = E->next()) {
  467. const Ref<Translation> &t = E->get();
  468. ERR_FAIL_COND_V(t.is_null(), p_message);
  469. String l = t->get_locale();
  470. int score = compare_locales(fallback, l);
  471. if (score > 0 && score >= best_score) {
  472. StringName r = t->get_message(p_message);
  473. if (!r) {
  474. continue;
  475. }
  476. res = r;
  477. best_score = score;
  478. if (score == 10) {
  479. break; // Exact match, skip the rest.
  480. }
  481. }
  482. }
  483. }
  484. if (!res) {
  485. return p_message;
  486. }
  487. return res;
  488. }
  489. TranslationServer *TranslationServer::singleton = nullptr;
  490. bool TranslationServer::_load_translations(const String &p_from) {
  491. if (ProjectSettings::get_singleton()->has_setting(p_from)) {
  492. PoolVector<String> translations = ProjectSettings::get_singleton()->get(p_from);
  493. int tcount = translations.size();
  494. if (tcount) {
  495. PoolVector<String>::Read r = translations.read();
  496. for (int i = 0; i < tcount; i++) {
  497. Ref<Translation> tr = ResourceLoader::load(r[i]);
  498. if (tr.is_valid()) {
  499. add_translation(tr);
  500. }
  501. }
  502. }
  503. return true;
  504. }
  505. return false;
  506. }
  507. void TranslationServer::setup() {
  508. String test = GLOBAL_DEF("locale/test", "");
  509. test = test.strip_edges();
  510. if (test != "") {
  511. set_locale(test);
  512. } else {
  513. set_locale(OS::get_singleton()->get_locale());
  514. }
  515. fallback = GLOBAL_DEF("locale/fallback", "en");
  516. #ifdef TOOLS_ENABLED
  517. ProjectSettings::get_singleton()->set_custom_property_info("locale/fallback", PropertyInfo(Variant::STRING, "locale/fallback", PROPERTY_HINT_LOCALE_ID, ""));
  518. #endif
  519. }
  520. void TranslationServer::set_tool_translation(const Ref<Translation> &p_translation) {
  521. tool_translation = p_translation;
  522. }
  523. StringName TranslationServer::tool_translate(const StringName &p_message, const StringName &p_context) const {
  524. if (tool_translation.is_valid()) {
  525. StringName r = tool_translation->get_context_message(p_message, p_context);
  526. if (r) {
  527. return r;
  528. }
  529. }
  530. return p_message;
  531. }
  532. void TranslationServer::set_doc_translation(const Ref<Translation> &p_translation) {
  533. doc_translation = p_translation;
  534. }
  535. StringName TranslationServer::doc_translate(const StringName &p_message) const {
  536. if (doc_translation.is_valid()) {
  537. StringName r = doc_translation->get_message(p_message);
  538. if (r) {
  539. return r;
  540. }
  541. }
  542. return p_message;
  543. }
  544. void TranslationServer::_bind_methods() {
  545. ClassDB::bind_method(D_METHOD("set_locale", "locale"), &TranslationServer::set_locale);
  546. ClassDB::bind_method(D_METHOD("get_locale"), &TranslationServer::get_locale);
  547. ClassDB::bind_method(D_METHOD("compare_locales", "locale_a", "locale_b"), &TranslationServer::compare_locales);
  548. ClassDB::bind_method(D_METHOD("standardize_locale", "locale"), &TranslationServer::standardize_locale);
  549. ClassDB::bind_method(D_METHOD("get_all_languages"), &TranslationServer::get_all_languages);
  550. ClassDB::bind_method(D_METHOD("get_language_name", "language"), &TranslationServer::get_language_name);
  551. ClassDB::bind_method(D_METHOD("get_all_scripts"), &TranslationServer::get_all_scripts);
  552. ClassDB::bind_method(D_METHOD("get_script_name", "script"), &TranslationServer::get_script_name);
  553. ClassDB::bind_method(D_METHOD("get_all_countries"), &TranslationServer::get_all_countries);
  554. ClassDB::bind_method(D_METHOD("get_country_name", "country"), &TranslationServer::get_country_name);
  555. ClassDB::bind_method(D_METHOD("get_locale_name", "locale"), &TranslationServer::get_locale_name);
  556. ClassDB::bind_method(D_METHOD("translate", "message"), &TranslationServer::translate);
  557. ClassDB::bind_method(D_METHOD("add_translation", "translation"), &TranslationServer::add_translation);
  558. ClassDB::bind_method(D_METHOD("remove_translation", "translation"), &TranslationServer::remove_translation);
  559. ClassDB::bind_method(D_METHOD("clear"), &TranslationServer::clear);
  560. ClassDB::bind_method(D_METHOD("get_loaded_locales"), &TranslationServer::get_loaded_locales);
  561. }
  562. void TranslationServer::load_translations() {
  563. String locale = get_locale();
  564. _load_translations("locale/translations"); //all
  565. _load_translations("locale/translations_" + locale.substr(0, 2));
  566. if (locale.substr(0, 2) != locale) {
  567. _load_translations("locale/translations_" + locale);
  568. }
  569. }
  570. TranslationServer::TranslationServer() :
  571. locale("en"),
  572. enabled(true) {
  573. singleton = this;
  574. init_locale_info();
  575. }