translation.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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 "translation.compat.inc"
  32. #include "core/config/project_settings.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/os/os.h"
  35. #include "core/string/locales.h"
  36. #ifdef TOOLS_ENABLED
  37. #include "main/main.h"
  38. #endif
  39. Dictionary Translation::_get_messages() const {
  40. Dictionary d;
  41. for (const KeyValue<StringName, StringName> &E : translation_map) {
  42. d[E.key] = E.value;
  43. }
  44. return d;
  45. }
  46. Vector<String> Translation::_get_message_list() const {
  47. Vector<String> msgs;
  48. msgs.resize(translation_map.size());
  49. int idx = 0;
  50. for (const KeyValue<StringName, StringName> &E : translation_map) {
  51. msgs.set(idx, E.key);
  52. idx += 1;
  53. }
  54. return msgs;
  55. }
  56. Vector<String> Translation::get_translated_message_list() const {
  57. Vector<String> msgs;
  58. msgs.resize(translation_map.size());
  59. int idx = 0;
  60. for (const KeyValue<StringName, StringName> &E : translation_map) {
  61. msgs.set(idx, E.value);
  62. idx += 1;
  63. }
  64. return msgs;
  65. }
  66. void Translation::_set_messages(const Dictionary &p_messages) {
  67. List<Variant> keys;
  68. p_messages.get_key_list(&keys);
  69. for (const Variant &E : keys) {
  70. translation_map[E] = p_messages[E];
  71. }
  72. }
  73. void Translation::set_locale(const String &p_locale) {
  74. locale = TranslationServer::get_singleton()->standardize_locale(p_locale);
  75. if (Thread::is_main_thread()) {
  76. _notify_translation_changed_if_applies();
  77. } else {
  78. // Avoid calling non-thread-safe functions here.
  79. callable_mp(this, &Translation::_notify_translation_changed_if_applies).call_deferred();
  80. }
  81. }
  82. void Translation::_notify_translation_changed_if_applies() {
  83. if (OS::get_singleton()->get_main_loop() && TranslationServer::get_singleton()->get_loaded_locales().has(get_locale())) {
  84. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  85. }
  86. }
  87. void Translation::add_message(const StringName &p_src_text, const StringName &p_xlated_text, const StringName &p_context) {
  88. translation_map[p_src_text] = p_xlated_text;
  89. }
  90. void Translation::add_plural_message(const StringName &p_src_text, const Vector<String> &p_plural_xlated_texts, const StringName &p_context) {
  91. WARN_PRINT("Translation class doesn't handle plural messages. Calling add_plural_message() on a Translation instance is probably a mistake. \nUse a derived Translation class that handles plurals, such as TranslationPO class");
  92. ERR_FAIL_COND_MSG(p_plural_xlated_texts.is_empty(), "Parameter vector p_plural_xlated_texts passed in is empty.");
  93. translation_map[p_src_text] = p_plural_xlated_texts[0];
  94. }
  95. StringName Translation::get_message(const StringName &p_src_text, const StringName &p_context) const {
  96. StringName ret;
  97. if (GDVIRTUAL_CALL(_get_message, p_src_text, p_context, ret)) {
  98. return ret;
  99. }
  100. if (p_context != StringName()) {
  101. WARN_PRINT("Translation class doesn't handle context. Using context in get_message() on a Translation instance is probably a mistake. \nUse a derived Translation class that handles context, such as TranslationPO class");
  102. }
  103. HashMap<StringName, StringName>::ConstIterator E = translation_map.find(p_src_text);
  104. if (!E) {
  105. return StringName();
  106. }
  107. return E->value;
  108. }
  109. StringName Translation::get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context) const {
  110. StringName ret;
  111. if (GDVIRTUAL_CALL(_get_plural_message, p_src_text, p_plural_text, p_n, p_context, ret)) {
  112. return ret;
  113. }
  114. WARN_PRINT("Translation class doesn't handle plural messages. Calling get_plural_message() on a Translation instance is probably a mistake. \nUse a derived Translation class that handles plurals, such as TranslationPO class");
  115. return get_message(p_src_text);
  116. }
  117. void Translation::erase_message(const StringName &p_src_text, const StringName &p_context) {
  118. if (p_context != StringName()) {
  119. WARN_PRINT("Translation class doesn't handle context. Using context in erase_message() on a Translation instance is probably a mistake. \nUse a derived Translation class that handles context, such as TranslationPO class");
  120. }
  121. translation_map.erase(p_src_text);
  122. }
  123. void Translation::get_message_list(List<StringName> *r_messages) const {
  124. for (const KeyValue<StringName, StringName> &E : translation_map) {
  125. r_messages->push_back(E.key);
  126. }
  127. }
  128. int Translation::get_message_count() const {
  129. return translation_map.size();
  130. }
  131. void Translation::_bind_methods() {
  132. ClassDB::bind_method(D_METHOD("set_locale", "locale"), &Translation::set_locale);
  133. ClassDB::bind_method(D_METHOD("get_locale"), &Translation::get_locale);
  134. ClassDB::bind_method(D_METHOD("add_message", "src_message", "xlated_message", "context"), &Translation::add_message, DEFVAL(StringName()));
  135. ClassDB::bind_method(D_METHOD("add_plural_message", "src_message", "xlated_messages", "context"), &Translation::add_plural_message, DEFVAL(StringName()));
  136. ClassDB::bind_method(D_METHOD("get_message", "src_message", "context"), &Translation::get_message, DEFVAL(StringName()));
  137. ClassDB::bind_method(D_METHOD("get_plural_message", "src_message", "src_plural_message", "n", "context"), &Translation::get_plural_message, DEFVAL(StringName()));
  138. ClassDB::bind_method(D_METHOD("erase_message", "src_message", "context"), &Translation::erase_message, DEFVAL(StringName()));
  139. ClassDB::bind_method(D_METHOD("get_message_list"), &Translation::_get_message_list);
  140. ClassDB::bind_method(D_METHOD("get_translated_message_list"), &Translation::get_translated_message_list);
  141. ClassDB::bind_method(D_METHOD("get_message_count"), &Translation::get_message_count);
  142. ClassDB::bind_method(D_METHOD("_set_messages", "messages"), &Translation::_set_messages);
  143. ClassDB::bind_method(D_METHOD("_get_messages"), &Translation::_get_messages);
  144. GDVIRTUAL_BIND(_get_plural_message, "src_message", "src_plural_message", "n", "context");
  145. GDVIRTUAL_BIND(_get_message, "src_message", "context");
  146. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "messages", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_messages", "_get_messages");
  147. ADD_PROPERTY(PropertyInfo(Variant::STRING, "locale"), "set_locale", "get_locale");
  148. }
  149. ///////////////////////////////////////////////
  150. struct _character_accent_pair {
  151. const char32_t character;
  152. const char32_t *accented_character;
  153. };
  154. static _character_accent_pair _character_to_accented[] = {
  155. { 'A', U"Å" },
  156. { 'B', U"ß" },
  157. { 'C', U"Ç" },
  158. { 'D', U"Ð" },
  159. { 'E', U"É" },
  160. { 'F', U"F́" },
  161. { 'G', U"Ĝ" },
  162. { 'H', U"Ĥ" },
  163. { 'I', U"Ĩ" },
  164. { 'J', U"Ĵ" },
  165. { 'K', U"ĸ" },
  166. { 'L', U"Ł" },
  167. { 'M', U"Ḿ" },
  168. { 'N', U"й" },
  169. { 'O', U"Ö" },
  170. { 'P', U"Ṕ" },
  171. { 'Q', U"Q́" },
  172. { 'R', U"Ř" },
  173. { 'S', U"Ŝ" },
  174. { 'T', U"Ŧ" },
  175. { 'U', U"Ũ" },
  176. { 'V', U"Ṽ" },
  177. { 'W', U"Ŵ" },
  178. { 'X', U"X́" },
  179. { 'Y', U"Ÿ" },
  180. { 'Z', U"Ž" },
  181. { 'a', U"á" },
  182. { 'b', U"ḅ" },
  183. { 'c', U"ć" },
  184. { 'd', U"d́" },
  185. { 'e', U"é" },
  186. { 'f', U"f́" },
  187. { 'g', U"ǵ" },
  188. { 'h', U"h̀" },
  189. { 'i', U"í" },
  190. { 'j', U"ǰ" },
  191. { 'k', U"ḱ" },
  192. { 'l', U"ł" },
  193. { 'm', U"m̀" },
  194. { 'n', U"ή" },
  195. { 'o', U"ô" },
  196. { 'p', U"ṕ" },
  197. { 'q', U"q́" },
  198. { 'r', U"ŕ" },
  199. { 's', U"š" },
  200. { 't', U"ŧ" },
  201. { 'u', U"ü" },
  202. { 'v', U"ṽ" },
  203. { 'w', U"ŵ" },
  204. { 'x', U"x́" },
  205. { 'y', U"ý" },
  206. { 'z', U"ź" },
  207. };
  208. Vector<TranslationServer::LocaleScriptInfo> TranslationServer::locale_script_info;
  209. HashMap<String, String> TranslationServer::language_map;
  210. HashMap<String, String> TranslationServer::script_map;
  211. HashMap<String, String> TranslationServer::locale_rename_map;
  212. HashMap<String, String> TranslationServer::country_name_map;
  213. HashMap<String, String> TranslationServer::variant_map;
  214. HashMap<String, String> TranslationServer::country_rename_map;
  215. void TranslationServer::init_locale_info() {
  216. // Init locale info.
  217. language_map.clear();
  218. int idx = 0;
  219. while (language_list[idx][0] != nullptr) {
  220. language_map[language_list[idx][0]] = String::utf8(language_list[idx][1]);
  221. idx++;
  222. }
  223. // Init locale-script map.
  224. locale_script_info.clear();
  225. idx = 0;
  226. while (locale_scripts[idx][0] != nullptr) {
  227. LocaleScriptInfo info;
  228. info.name = locale_scripts[idx][0];
  229. info.script = locale_scripts[idx][1];
  230. info.default_country = locale_scripts[idx][2];
  231. Vector<String> supported_countries = String(locale_scripts[idx][3]).split(",", false);
  232. for (int i = 0; i < supported_countries.size(); i++) {
  233. info.supported_countries.insert(supported_countries[i]);
  234. }
  235. locale_script_info.push_back(info);
  236. idx++;
  237. }
  238. // Init supported script list.
  239. script_map.clear();
  240. idx = 0;
  241. while (script_list[idx][0] != nullptr) {
  242. script_map[script_list[idx][1]] = String::utf8(script_list[idx][0]);
  243. idx++;
  244. }
  245. // Init regional variant map.
  246. variant_map.clear();
  247. idx = 0;
  248. while (locale_variants[idx][0] != nullptr) {
  249. variant_map[locale_variants[idx][0]] = locale_variants[idx][1];
  250. idx++;
  251. }
  252. // Init locale renames.
  253. locale_rename_map.clear();
  254. idx = 0;
  255. while (locale_renames[idx][0] != nullptr) {
  256. if (!String(locale_renames[idx][1]).is_empty()) {
  257. locale_rename_map[locale_renames[idx][0]] = locale_renames[idx][1];
  258. }
  259. idx++;
  260. }
  261. // Init country names.
  262. country_name_map.clear();
  263. idx = 0;
  264. while (country_names[idx][0] != nullptr) {
  265. country_name_map[String(country_names[idx][0])] = String::utf8(country_names[idx][1]);
  266. idx++;
  267. }
  268. // Init country renames.
  269. country_rename_map.clear();
  270. idx = 0;
  271. while (country_renames[idx][0] != nullptr) {
  272. if (!String(country_renames[idx][1]).is_empty()) {
  273. country_rename_map[country_renames[idx][0]] = country_renames[idx][1];
  274. }
  275. idx++;
  276. }
  277. }
  278. String TranslationServer::standardize_locale(const String &p_locale) const {
  279. return _standardize_locale(p_locale, false);
  280. }
  281. String TranslationServer::_standardize_locale(const String &p_locale, bool p_add_defaults) const {
  282. // Replaces '-' with '_' for macOS style locales.
  283. String univ_locale = p_locale.replace("-", "_");
  284. // Extract locale elements.
  285. String lang_name, script_name, country_name, variant_name;
  286. Vector<String> locale_elements = univ_locale.get_slice("@", 0).split("_");
  287. lang_name = locale_elements[0];
  288. if (locale_elements.size() >= 2) {
  289. 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])) {
  290. script_name = locale_elements[1];
  291. }
  292. if (locale_elements[1].length() == 2 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_upper_case(locale_elements[1][1])) {
  293. country_name = locale_elements[1];
  294. }
  295. }
  296. if (locale_elements.size() >= 3) {
  297. if (locale_elements[2].length() == 2 && is_ascii_upper_case(locale_elements[2][0]) && is_ascii_upper_case(locale_elements[2][1])) {
  298. country_name = locale_elements[2];
  299. } else if (variant_map.has(locale_elements[2].to_lower()) && variant_map[locale_elements[2].to_lower()] == lang_name) {
  300. variant_name = locale_elements[2].to_lower();
  301. }
  302. }
  303. if (locale_elements.size() >= 4) {
  304. if (variant_map.has(locale_elements[3].to_lower()) && variant_map[locale_elements[3].to_lower()] == lang_name) {
  305. variant_name = locale_elements[3].to_lower();
  306. }
  307. }
  308. // Try extract script and variant from the extra part.
  309. Vector<String> script_extra = univ_locale.get_slice("@", 1).split(";");
  310. for (int i = 0; i < script_extra.size(); i++) {
  311. if (script_extra[i].to_lower() == "cyrillic") {
  312. script_name = "Cyrl";
  313. break;
  314. } else if (script_extra[i].to_lower() == "latin") {
  315. script_name = "Latn";
  316. break;
  317. } else if (script_extra[i].to_lower() == "devanagari") {
  318. script_name = "Deva";
  319. break;
  320. } else if (variant_map.has(script_extra[i].to_lower()) && variant_map[script_extra[i].to_lower()] == lang_name) {
  321. variant_name = script_extra[i].to_lower();
  322. }
  323. }
  324. // Handles known non-ISO language names used e.g. on Windows.
  325. if (locale_rename_map.has(lang_name)) {
  326. lang_name = locale_rename_map[lang_name];
  327. }
  328. // Handle country renames.
  329. if (country_rename_map.has(country_name)) {
  330. country_name = country_rename_map[country_name];
  331. }
  332. // Remove unsupported script codes.
  333. if (!script_map.has(script_name)) {
  334. script_name = "";
  335. }
  336. // Add script code base on language and country codes for some ambiguous cases.
  337. if (p_add_defaults) {
  338. if (script_name.is_empty()) {
  339. for (int i = 0; i < locale_script_info.size(); i++) {
  340. const LocaleScriptInfo &info = locale_script_info[i];
  341. if (info.name == lang_name) {
  342. if (country_name.is_empty() || info.supported_countries.has(country_name)) {
  343. script_name = info.script;
  344. break;
  345. }
  346. }
  347. }
  348. }
  349. if (!script_name.is_empty() && country_name.is_empty()) {
  350. // Add conntry code based on script for some ambiguous cases.
  351. for (int i = 0; i < locale_script_info.size(); i++) {
  352. const LocaleScriptInfo &info = locale_script_info[i];
  353. if (info.name == lang_name && info.script == script_name) {
  354. country_name = info.default_country;
  355. break;
  356. }
  357. }
  358. }
  359. }
  360. // Combine results.
  361. String out = lang_name;
  362. if (!script_name.is_empty()) {
  363. out = out + "_" + script_name;
  364. }
  365. if (!country_name.is_empty()) {
  366. out = out + "_" + country_name;
  367. }
  368. if (!variant_name.is_empty()) {
  369. out = out + "_" + variant_name;
  370. }
  371. return out;
  372. }
  373. int TranslationServer::compare_locales(const String &p_locale_a, const String &p_locale_b) const {
  374. String locale_a = _standardize_locale(p_locale_a, true);
  375. String locale_b = _standardize_locale(p_locale_b, true);
  376. if (locale_a == locale_b) {
  377. // Exact match.
  378. return 10;
  379. }
  380. Vector<String> locale_a_elements = locale_a.split("_");
  381. Vector<String> locale_b_elements = locale_b.split("_");
  382. if (locale_a_elements[0] == locale_b_elements[0]) {
  383. // Matching language, both locales have extra parts.
  384. // Return number of matching elements.
  385. int matching_elements = 1;
  386. for (int i = 1; i < locale_a_elements.size(); i++) {
  387. for (int j = 1; j < locale_b_elements.size(); j++) {
  388. if (locale_a_elements[i] == locale_b_elements[j]) {
  389. matching_elements++;
  390. }
  391. }
  392. }
  393. return matching_elements;
  394. } else {
  395. // No match.
  396. return 0;
  397. }
  398. }
  399. String TranslationServer::get_locale_name(const String &p_locale) const {
  400. String lang_name, script_name, country_name;
  401. Vector<String> locale_elements = standardize_locale(p_locale).split("_");
  402. lang_name = locale_elements[0];
  403. if (locale_elements.size() >= 2) {
  404. 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])) {
  405. script_name = locale_elements[1];
  406. }
  407. if (locale_elements[1].length() == 2 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_upper_case(locale_elements[1][1])) {
  408. country_name = locale_elements[1];
  409. }
  410. }
  411. if (locale_elements.size() >= 3) {
  412. if (locale_elements[2].length() == 2 && is_ascii_upper_case(locale_elements[2][0]) && is_ascii_upper_case(locale_elements[2][1])) {
  413. country_name = locale_elements[2];
  414. }
  415. }
  416. String name = language_map[lang_name];
  417. if (!script_name.is_empty()) {
  418. name = name + " (" + script_map[script_name] + ")";
  419. }
  420. if (!country_name.is_empty()) {
  421. name = name + ", " + country_name_map[country_name];
  422. }
  423. return name;
  424. }
  425. Vector<String> TranslationServer::get_all_languages() const {
  426. Vector<String> languages;
  427. for (const KeyValue<String, String> &E : language_map) {
  428. languages.push_back(E.key);
  429. }
  430. return languages;
  431. }
  432. String TranslationServer::get_language_name(const String &p_language) const {
  433. return language_map[p_language];
  434. }
  435. Vector<String> TranslationServer::get_all_scripts() const {
  436. Vector<String> scripts;
  437. for (const KeyValue<String, String> &E : script_map) {
  438. scripts.push_back(E.key);
  439. }
  440. return scripts;
  441. }
  442. String TranslationServer::get_script_name(const String &p_script) const {
  443. return script_map[p_script];
  444. }
  445. Vector<String> TranslationServer::get_all_countries() const {
  446. Vector<String> countries;
  447. for (const KeyValue<String, String> &E : country_name_map) {
  448. countries.push_back(E.key);
  449. }
  450. return countries;
  451. }
  452. String TranslationServer::get_country_name(const String &p_country) const {
  453. return country_name_map[p_country];
  454. }
  455. void TranslationServer::set_locale(const String &p_locale) {
  456. String new_locale = standardize_locale(p_locale);
  457. if (locale == new_locale) {
  458. return;
  459. }
  460. locale = new_locale;
  461. ResourceLoader::reload_translation_remaps();
  462. if (OS::get_singleton()->get_main_loop()) {
  463. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  464. }
  465. }
  466. String TranslationServer::get_locale() const {
  467. return locale;
  468. }
  469. PackedStringArray TranslationServer::get_loaded_locales() const {
  470. PackedStringArray locales;
  471. for (const Ref<Translation> &E : translations) {
  472. const Ref<Translation> &t = E;
  473. ERR_FAIL_COND_V(t.is_null(), PackedStringArray());
  474. String l = t->get_locale();
  475. locales.push_back(l);
  476. }
  477. return locales;
  478. }
  479. void TranslationServer::add_translation(const Ref<Translation> &p_translation) {
  480. translations.insert(p_translation);
  481. }
  482. void TranslationServer::remove_translation(const Ref<Translation> &p_translation) {
  483. translations.erase(p_translation);
  484. }
  485. Ref<Translation> TranslationServer::get_translation_object(const String &p_locale) {
  486. Ref<Translation> res;
  487. int best_score = 0;
  488. for (const Ref<Translation> &E : translations) {
  489. const Ref<Translation> &t = E;
  490. ERR_FAIL_COND_V(t.is_null(), nullptr);
  491. String l = t->get_locale();
  492. int score = compare_locales(p_locale, l);
  493. if (score > 0 && score >= best_score) {
  494. res = t;
  495. best_score = score;
  496. if (score == 10) {
  497. break; // Exact match, skip the rest.
  498. }
  499. }
  500. }
  501. return res;
  502. }
  503. void TranslationServer::clear() {
  504. translations.clear();
  505. }
  506. StringName TranslationServer::translate(const StringName &p_message, const StringName &p_context) const {
  507. // Match given message against the translation catalog for the project locale.
  508. if (!enabled) {
  509. return p_message;
  510. }
  511. StringName res = _get_message_from_translations(p_message, p_context, locale, false);
  512. if (!res && fallback.length() >= 2) {
  513. res = _get_message_from_translations(p_message, p_context, fallback, false);
  514. }
  515. if (!res) {
  516. return pseudolocalization_enabled ? pseudolocalize(p_message) : p_message;
  517. }
  518. return pseudolocalization_enabled ? pseudolocalize(res) : res;
  519. }
  520. StringName TranslationServer::translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  521. if (!enabled) {
  522. if (p_n == 1) {
  523. return p_message;
  524. }
  525. return p_message_plural;
  526. }
  527. StringName res = _get_message_from_translations(p_message, p_context, locale, true, p_message_plural, p_n);
  528. if (!res && fallback.length() >= 2) {
  529. res = _get_message_from_translations(p_message, p_context, fallback, true, p_message_plural, p_n);
  530. }
  531. if (!res) {
  532. if (p_n == 1) {
  533. return p_message;
  534. }
  535. return p_message_plural;
  536. }
  537. return res;
  538. }
  539. StringName TranslationServer::_get_message_from_translations(const StringName &p_message, const StringName &p_context, const String &p_locale, bool plural, const String &p_message_plural, int p_n) const {
  540. StringName res;
  541. int best_score = 0;
  542. for (const Ref<Translation> &E : translations) {
  543. const Ref<Translation> &t = E;
  544. ERR_FAIL_COND_V(t.is_null(), p_message);
  545. String l = t->get_locale();
  546. int score = compare_locales(p_locale, l);
  547. if (score > 0 && score >= best_score) {
  548. StringName r;
  549. if (!plural) {
  550. r = t->get_message(p_message, p_context);
  551. } else {
  552. r = t->get_plural_message(p_message, p_message_plural, p_n, p_context);
  553. }
  554. if (!r) {
  555. continue;
  556. }
  557. res = r;
  558. best_score = score;
  559. if (score == 10) {
  560. break; // Exact match, skip the rest.
  561. }
  562. }
  563. }
  564. return res;
  565. }
  566. TranslationServer *TranslationServer::singleton = nullptr;
  567. bool TranslationServer::_load_translations(const String &p_from) {
  568. if (ProjectSettings::get_singleton()->has_setting(p_from)) {
  569. const Vector<String> &translation_names = GLOBAL_GET(p_from);
  570. int tcount = translation_names.size();
  571. if (tcount) {
  572. const String *r = translation_names.ptr();
  573. for (int i = 0; i < tcount; i++) {
  574. Ref<Translation> tr = ResourceLoader::load(r[i]);
  575. if (tr.is_valid()) {
  576. add_translation(tr);
  577. }
  578. }
  579. }
  580. return true;
  581. }
  582. return false;
  583. }
  584. void TranslationServer::setup() {
  585. String test = GLOBAL_DEF("internationalization/locale/test", "");
  586. test = test.strip_edges();
  587. if (!test.is_empty()) {
  588. set_locale(test);
  589. } else {
  590. set_locale(OS::get_singleton()->get_locale());
  591. }
  592. fallback = GLOBAL_DEF("internationalization/locale/fallback", "en");
  593. pseudolocalization_enabled = GLOBAL_DEF("internationalization/pseudolocalization/use_pseudolocalization", false);
  594. pseudolocalization_accents_enabled = GLOBAL_DEF("internationalization/pseudolocalization/replace_with_accents", true);
  595. pseudolocalization_double_vowels_enabled = GLOBAL_DEF("internationalization/pseudolocalization/double_vowels", false);
  596. pseudolocalization_fake_bidi_enabled = GLOBAL_DEF("internationalization/pseudolocalization/fake_bidi", false);
  597. pseudolocalization_override_enabled = GLOBAL_DEF("internationalization/pseudolocalization/override", false);
  598. expansion_ratio = GLOBAL_DEF("internationalization/pseudolocalization/expansion_ratio", 0.0);
  599. pseudolocalization_prefix = GLOBAL_DEF("internationalization/pseudolocalization/prefix", "[");
  600. pseudolocalization_suffix = GLOBAL_DEF("internationalization/pseudolocalization/suffix", "]");
  601. pseudolocalization_skip_placeholders_enabled = GLOBAL_DEF("internationalization/pseudolocalization/skip_placeholders", true);
  602. #ifdef TOOLS_ENABLED
  603. ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, "internationalization/locale/fallback", PROPERTY_HINT_LOCALE_ID, ""));
  604. #endif
  605. }
  606. void TranslationServer::set_tool_translation(const Ref<Translation> &p_translation) {
  607. tool_translation = p_translation;
  608. }
  609. Ref<Translation> TranslationServer::get_tool_translation() const {
  610. return tool_translation;
  611. }
  612. String TranslationServer::get_tool_locale() {
  613. #ifdef TOOLS_ENABLED
  614. if (Engine::get_singleton()->is_editor_hint() || Engine::get_singleton()->is_project_manager_hint()) {
  615. if (TranslationServer::get_singleton()->get_tool_translation().is_valid()) {
  616. return tool_translation->get_locale();
  617. } else {
  618. return "en";
  619. }
  620. } else {
  621. #else
  622. {
  623. #endif
  624. // Look for best matching loaded translation.
  625. String best_locale = "en";
  626. int best_score = 0;
  627. for (const Ref<Translation> &E : translations) {
  628. const Ref<Translation> &t = E;
  629. ERR_FAIL_COND_V(t.is_null(), best_locale);
  630. String l = t->get_locale();
  631. int score = compare_locales(locale, l);
  632. if (score > 0 && score >= best_score) {
  633. best_locale = l;
  634. best_score = score;
  635. if (score == 10) {
  636. break; // Exact match, skip the rest.
  637. }
  638. }
  639. }
  640. return best_locale;
  641. }
  642. }
  643. StringName TranslationServer::tool_translate(const StringName &p_message, const StringName &p_context) const {
  644. if (tool_translation.is_valid()) {
  645. StringName r = tool_translation->get_message(p_message, p_context);
  646. if (r) {
  647. return editor_pseudolocalization ? tool_pseudolocalize(r) : r;
  648. }
  649. }
  650. return editor_pseudolocalization ? tool_pseudolocalize(p_message) : p_message;
  651. }
  652. StringName TranslationServer::tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  653. if (tool_translation.is_valid()) {
  654. StringName r = tool_translation->get_plural_message(p_message, p_message_plural, p_n, p_context);
  655. if (r) {
  656. return r;
  657. }
  658. }
  659. if (p_n == 1) {
  660. return p_message;
  661. }
  662. return p_message_plural;
  663. }
  664. void TranslationServer::set_property_translation(const Ref<Translation> &p_translation) {
  665. property_translation = p_translation;
  666. }
  667. StringName TranslationServer::property_translate(const StringName &p_message, const StringName &p_context) const {
  668. if (property_translation.is_valid()) {
  669. StringName r = property_translation->get_message(p_message, p_context);
  670. if (r) {
  671. return r;
  672. }
  673. }
  674. return p_message;
  675. }
  676. void TranslationServer::set_doc_translation(const Ref<Translation> &p_translation) {
  677. doc_translation = p_translation;
  678. }
  679. StringName TranslationServer::doc_translate(const StringName &p_message, const StringName &p_context) const {
  680. if (doc_translation.is_valid()) {
  681. StringName r = doc_translation->get_message(p_message, p_context);
  682. if (r) {
  683. return r;
  684. }
  685. }
  686. return p_message;
  687. }
  688. StringName TranslationServer::doc_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  689. if (doc_translation.is_valid()) {
  690. StringName r = doc_translation->get_plural_message(p_message, p_message_plural, p_n, p_context);
  691. if (r) {
  692. return r;
  693. }
  694. }
  695. if (p_n == 1) {
  696. return p_message;
  697. }
  698. return p_message_plural;
  699. }
  700. void TranslationServer::set_extractable_translation(const Ref<Translation> &p_translation) {
  701. extractable_translation = p_translation;
  702. }
  703. StringName TranslationServer::extractable_translate(const StringName &p_message, const StringName &p_context) const {
  704. if (extractable_translation.is_valid()) {
  705. StringName r = extractable_translation->get_message(p_message, p_context);
  706. if (r) {
  707. return r;
  708. }
  709. }
  710. return p_message;
  711. }
  712. StringName TranslationServer::extractable_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  713. if (extractable_translation.is_valid()) {
  714. StringName r = extractable_translation->get_plural_message(p_message, p_message_plural, p_n, p_context);
  715. if (r) {
  716. return r;
  717. }
  718. }
  719. if (p_n == 1) {
  720. return p_message;
  721. }
  722. return p_message_plural;
  723. }
  724. bool TranslationServer::is_pseudolocalization_enabled() const {
  725. return pseudolocalization_enabled;
  726. }
  727. void TranslationServer::set_pseudolocalization_enabled(bool p_enabled) {
  728. pseudolocalization_enabled = p_enabled;
  729. ResourceLoader::reload_translation_remaps();
  730. if (OS::get_singleton()->get_main_loop()) {
  731. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  732. }
  733. }
  734. void TranslationServer::set_editor_pseudolocalization(bool p_enabled) {
  735. editor_pseudolocalization = p_enabled;
  736. }
  737. void TranslationServer::reload_pseudolocalization() {
  738. pseudolocalization_accents_enabled = GLOBAL_GET("internationalization/pseudolocalization/replace_with_accents");
  739. pseudolocalization_double_vowels_enabled = GLOBAL_GET("internationalization/pseudolocalization/double_vowels");
  740. pseudolocalization_fake_bidi_enabled = GLOBAL_GET("internationalization/pseudolocalization/fake_bidi");
  741. pseudolocalization_override_enabled = GLOBAL_GET("internationalization/pseudolocalization/override");
  742. expansion_ratio = GLOBAL_GET("internationalization/pseudolocalization/expansion_ratio");
  743. pseudolocalization_prefix = GLOBAL_GET("internationalization/pseudolocalization/prefix");
  744. pseudolocalization_suffix = GLOBAL_GET("internationalization/pseudolocalization/suffix");
  745. pseudolocalization_skip_placeholders_enabled = GLOBAL_GET("internationalization/pseudolocalization/skip_placeholders");
  746. ResourceLoader::reload_translation_remaps();
  747. if (OS::get_singleton()->get_main_loop()) {
  748. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  749. }
  750. }
  751. StringName TranslationServer::pseudolocalize(const StringName &p_message) const {
  752. String message = p_message;
  753. int length = message.length();
  754. if (pseudolocalization_override_enabled) {
  755. message = get_override_string(message);
  756. }
  757. if (pseudolocalization_double_vowels_enabled) {
  758. message = double_vowels(message);
  759. }
  760. if (pseudolocalization_accents_enabled) {
  761. message = replace_with_accented_string(message);
  762. }
  763. if (pseudolocalization_fake_bidi_enabled) {
  764. message = wrap_with_fakebidi_characters(message);
  765. }
  766. StringName res = add_padding(message, length);
  767. return res;
  768. }
  769. StringName TranslationServer::tool_pseudolocalize(const StringName &p_message) const {
  770. String message = p_message;
  771. message = double_vowels(message);
  772. message = replace_with_accented_string(message);
  773. StringName res = "[!!! " + message + " !!!]";
  774. return res;
  775. }
  776. String TranslationServer::get_override_string(String &p_message) const {
  777. String res;
  778. for (int i = 0; i < p_message.length(); i++) {
  779. if (pseudolocalization_skip_placeholders_enabled && is_placeholder(p_message, i)) {
  780. res += p_message[i];
  781. res += p_message[i + 1];
  782. i++;
  783. continue;
  784. }
  785. res += '*';
  786. }
  787. return res;
  788. }
  789. String TranslationServer::double_vowels(String &p_message) const {
  790. String res;
  791. for (int i = 0; i < p_message.length(); i++) {
  792. if (pseudolocalization_skip_placeholders_enabled && is_placeholder(p_message, i)) {
  793. res += p_message[i];
  794. res += p_message[i + 1];
  795. i++;
  796. continue;
  797. }
  798. res += p_message[i];
  799. if (p_message[i] == 'a' || p_message[i] == 'e' || p_message[i] == 'i' || p_message[i] == 'o' || p_message[i] == 'u' ||
  800. p_message[i] == 'A' || p_message[i] == 'E' || p_message[i] == 'I' || p_message[i] == 'O' || p_message[i] == 'U') {
  801. res += p_message[i];
  802. }
  803. }
  804. return res;
  805. };
  806. String TranslationServer::replace_with_accented_string(String &p_message) const {
  807. String res;
  808. for (int i = 0; i < p_message.length(); i++) {
  809. if (pseudolocalization_skip_placeholders_enabled && is_placeholder(p_message, i)) {
  810. res += p_message[i];
  811. res += p_message[i + 1];
  812. i++;
  813. continue;
  814. }
  815. const char32_t *accented = get_accented_version(p_message[i]);
  816. if (accented) {
  817. res += accented;
  818. } else {
  819. res += p_message[i];
  820. }
  821. }
  822. return res;
  823. }
  824. String TranslationServer::wrap_with_fakebidi_characters(String &p_message) const {
  825. String res;
  826. char32_t fakebidiprefix = U'\u202e';
  827. char32_t fakebidisuffix = U'\u202c';
  828. res += fakebidiprefix;
  829. // The fake bidi unicode gets popped at every newline so pushing it back at every newline.
  830. for (int i = 0; i < p_message.length(); i++) {
  831. if (p_message[i] == '\n') {
  832. res += fakebidisuffix;
  833. res += p_message[i];
  834. res += fakebidiprefix;
  835. } else if (pseudolocalization_skip_placeholders_enabled && is_placeholder(p_message, i)) {
  836. res += fakebidisuffix;
  837. res += p_message[i];
  838. res += p_message[i + 1];
  839. res += fakebidiprefix;
  840. i++;
  841. } else {
  842. res += p_message[i];
  843. }
  844. }
  845. res += fakebidisuffix;
  846. return res;
  847. }
  848. String TranslationServer::add_padding(const String &p_message, int p_length) const {
  849. String underscores = String("_").repeat(p_length * expansion_ratio / 2);
  850. String prefix = pseudolocalization_prefix + underscores;
  851. String suffix = underscores + pseudolocalization_suffix;
  852. return prefix + p_message + suffix;
  853. }
  854. const char32_t *TranslationServer::get_accented_version(char32_t p_character) const {
  855. if (!is_ascii_alphabet_char(p_character)) {
  856. return nullptr;
  857. }
  858. for (unsigned int i = 0; i < sizeof(_character_to_accented) / sizeof(_character_to_accented[0]); i++) {
  859. if (_character_to_accented[i].character == p_character) {
  860. return _character_to_accented[i].accented_character;
  861. }
  862. }
  863. return nullptr;
  864. }
  865. bool TranslationServer::is_placeholder(String &p_message, int p_index) const {
  866. return p_index < p_message.length() - 1 && p_message[p_index] == '%' &&
  867. (p_message[p_index + 1] == 's' || p_message[p_index + 1] == 'c' || p_message[p_index + 1] == 'd' ||
  868. p_message[p_index + 1] == 'o' || p_message[p_index + 1] == 'x' || p_message[p_index + 1] == 'X' || p_message[p_index + 1] == 'f');
  869. }
  870. #ifdef TOOLS_ENABLED
  871. void TranslationServer::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  872. const String pf = p_function;
  873. if (p_idx == 0) {
  874. HashMap<String, String> *target_hash_map = nullptr;
  875. if (pf == "get_language_name") {
  876. target_hash_map = &language_map;
  877. } else if (pf == "get_script_name") {
  878. target_hash_map = &script_map;
  879. } else if (pf == "get_country_name") {
  880. target_hash_map = &country_name_map;
  881. }
  882. if (target_hash_map) {
  883. for (const KeyValue<String, String> &E : *target_hash_map) {
  884. r_options->push_back(E.key.quote());
  885. }
  886. }
  887. }
  888. Object::get_argument_options(p_function, p_idx, r_options);
  889. }
  890. #endif // TOOLS_ENABLED
  891. void TranslationServer::_bind_methods() {
  892. ClassDB::bind_method(D_METHOD("set_locale", "locale"), &TranslationServer::set_locale);
  893. ClassDB::bind_method(D_METHOD("get_locale"), &TranslationServer::get_locale);
  894. ClassDB::bind_method(D_METHOD("get_tool_locale"), &TranslationServer::get_tool_locale);
  895. ClassDB::bind_method(D_METHOD("compare_locales", "locale_a", "locale_b"), &TranslationServer::compare_locales);
  896. ClassDB::bind_method(D_METHOD("standardize_locale", "locale"), &TranslationServer::standardize_locale);
  897. ClassDB::bind_method(D_METHOD("get_all_languages"), &TranslationServer::get_all_languages);
  898. ClassDB::bind_method(D_METHOD("get_language_name", "language"), &TranslationServer::get_language_name);
  899. ClassDB::bind_method(D_METHOD("get_all_scripts"), &TranslationServer::get_all_scripts);
  900. ClassDB::bind_method(D_METHOD("get_script_name", "script"), &TranslationServer::get_script_name);
  901. ClassDB::bind_method(D_METHOD("get_all_countries"), &TranslationServer::get_all_countries);
  902. ClassDB::bind_method(D_METHOD("get_country_name", "country"), &TranslationServer::get_country_name);
  903. ClassDB::bind_method(D_METHOD("get_locale_name", "locale"), &TranslationServer::get_locale_name);
  904. ClassDB::bind_method(D_METHOD("translate", "message", "context"), &TranslationServer::translate, DEFVAL(StringName()));
  905. ClassDB::bind_method(D_METHOD("translate_plural", "message", "plural_message", "n", "context"), &TranslationServer::translate_plural, DEFVAL(StringName()));
  906. ClassDB::bind_method(D_METHOD("add_translation", "translation"), &TranslationServer::add_translation);
  907. ClassDB::bind_method(D_METHOD("remove_translation", "translation"), &TranslationServer::remove_translation);
  908. ClassDB::bind_method(D_METHOD("get_translation_object", "locale"), &TranslationServer::get_translation_object);
  909. ClassDB::bind_method(D_METHOD("clear"), &TranslationServer::clear);
  910. ClassDB::bind_method(D_METHOD("get_loaded_locales"), &TranslationServer::get_loaded_locales);
  911. ClassDB::bind_method(D_METHOD("is_pseudolocalization_enabled"), &TranslationServer::is_pseudolocalization_enabled);
  912. ClassDB::bind_method(D_METHOD("set_pseudolocalization_enabled", "enabled"), &TranslationServer::set_pseudolocalization_enabled);
  913. ClassDB::bind_method(D_METHOD("reload_pseudolocalization"), &TranslationServer::reload_pseudolocalization);
  914. ClassDB::bind_method(D_METHOD("pseudolocalize", "message"), &TranslationServer::pseudolocalize);
  915. ADD_PROPERTY(PropertyInfo(Variant::Type::BOOL, "pseudolocalization_enabled"), "set_pseudolocalization_enabled", "is_pseudolocalization_enabled");
  916. }
  917. void TranslationServer::load_translations() {
  918. _load_translations("internationalization/locale/translations"); //all
  919. _load_translations("internationalization/locale/translations_" + locale.substr(0, 2));
  920. if (locale.substr(0, 2) != locale) {
  921. _load_translations("internationalization/locale/translations_" + locale);
  922. }
  923. }
  924. TranslationServer::TranslationServer() {
  925. singleton = this;
  926. init_locale_info();
  927. }