translation.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. /*************************************************************************/
  2. /* translation.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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/config/project_settings.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/os/os.h"
  34. #ifdef TOOLS_ENABLED
  35. #include "editor/editor_settings.h"
  36. #include "main/main.h"
  37. #endif
  38. // ISO 639-1 language codes, with the addition of glibc locales with their
  39. // regional identifiers. This list must match the language names (in English)
  40. // of locale_names.
  41. //
  42. // References:
  43. // - https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
  44. // - https://lh.2xlibre.net/locales/
  45. static const char *locale_list[] = {
  46. "aa", // Afar
  47. "aa_DJ", // Afar (Djibouti)
  48. "aa_ER", // Afar (Eritrea)
  49. "aa_ET", // Afar (Ethiopia)
  50. "af", // Afrikaans
  51. "af_ZA", // Afrikaans (South Africa)
  52. "agr_PE", // Aguaruna (Peru)
  53. "ak_GH", // Akan (Ghana)
  54. "am_ET", // Amharic (Ethiopia)
  55. "an_ES", // Aragonese (Spain)
  56. "anp_IN", // Angika (India)
  57. "ar", // Arabic
  58. "ar_AE", // Arabic (United Arab Emirates)
  59. "ar_BH", // Arabic (Bahrain)
  60. "ar_DZ", // Arabic (Algeria)
  61. "ar_EG", // Arabic (Egypt)
  62. "ar_IN", // Arabic (India)
  63. "ar_IQ", // Arabic (Iraq)
  64. "ar_JO", // Arabic (Jordan)
  65. "ar_KW", // Arabic (Kuwait)
  66. "ar_LB", // Arabic (Lebanon)
  67. "ar_LY", // Arabic (Libya)
  68. "ar_MA", // Arabic (Morocco)
  69. "ar_OM", // Arabic (Oman)
  70. "ar_QA", // Arabic (Qatar)
  71. "ar_SA", // Arabic (Saudi Arabia)
  72. "ar_SD", // Arabic (Sudan)
  73. "ar_SS", // Arabic (South Soudan)
  74. "ar_SY", // Arabic (Syria)
  75. "ar_TN", // Arabic (Tunisia)
  76. "ar_YE", // Arabic (Yemen)
  77. "as_IN", // Assamese (India)
  78. "ast_ES", // Asturian (Spain)
  79. "ayc_PE", // Southern Aymara (Peru)
  80. "ay_PE", // Aymara (Peru)
  81. "az_AZ", // Azerbaijani (Azerbaijan)
  82. "be", // Belarusian
  83. "be_BY", // Belarusian (Belarus)
  84. "bem_ZM", // Bemba (Zambia)
  85. "ber_DZ", // Berber languages (Algeria)
  86. "ber_MA", // Berber languages (Morocco)
  87. "bg", // Bulgarian
  88. "bg_BG", // Bulgarian (Bulgaria)
  89. "bhb_IN", // Bhili (India)
  90. "bho_IN", // Bhojpuri (India)
  91. "bi_TV", // Bislama (Tuvalu)
  92. "bn", // Bengali
  93. "bn_BD", // Bengali (Bangladesh)
  94. "bn_IN", // Bengali (India)
  95. "bo", // Tibetan
  96. "bo_CN", // Tibetan (China)
  97. "bo_IN", // Tibetan (India)
  98. "br_FR", // Breton (France)
  99. "brx_IN", // Bodo (India)
  100. "bs_BA", // Bosnian (Bosnia and Herzegovina)
  101. "byn_ER", // Bilin (Eritrea)
  102. "ca", // Catalan
  103. "ca_AD", // Catalan (Andorra)
  104. "ca_ES", // Catalan (Spain)
  105. "ca_FR", // Catalan (France)
  106. "ca_IT", // Catalan (Italy)
  107. "ce_RU", // Chechen (Russia)
  108. "chr_US", // Cherokee (United States)
  109. "cmn_TW", // Mandarin Chinese (Taiwan)
  110. "crh_UA", // Crimean Tatar (Ukraine)
  111. "csb_PL", // Kashubian (Poland)
  112. "cs", // Czech
  113. "cs_CZ", // Czech (Czech Republic)
  114. "cv_RU", // Chuvash (Russia)
  115. "cy_GB", // Welsh (United Kingdom)
  116. "da", // Danish
  117. "da_DK", // Danish (Denmark)
  118. "de", // German
  119. "de_AT", // German (Austria)
  120. "de_BE", // German (Belgium)
  121. "de_CH", // German (Switzerland)
  122. "de_DE", // German (Germany)
  123. "de_IT", // German (Italy)
  124. "de_LU", // German (Luxembourg)
  125. "doi_IN", // Dogri (India)
  126. "dv_MV", // Dhivehi (Maldives)
  127. "dz_BT", // Dzongkha (Bhutan)
  128. "el", // Greek
  129. "el_CY", // Greek (Cyprus)
  130. "el_GR", // Greek (Greece)
  131. "en", // English
  132. "en_AG", // English (Antigua and Barbuda)
  133. "en_AU", // English (Australia)
  134. "en_BW", // English (Botswana)
  135. "en_CA", // English (Canada)
  136. "en_DK", // English (Denmark)
  137. "en_GB", // English (United Kingdom)
  138. "en_HK", // English (Hong Kong)
  139. "en_IE", // English (Ireland)
  140. "en_IL", // English (Israel)
  141. "en_IN", // English (India)
  142. "en_NG", // English (Nigeria)
  143. "en_NZ", // English (New Zealand)
  144. "en_PH", // English (Philippines)
  145. "en_SG", // English (Singapore)
  146. "en_US", // English (United States)
  147. "en_ZA", // English (South Africa)
  148. "en_ZM", // English (Zambia)
  149. "en_ZW", // English (Zimbabwe)
  150. "eo", // Esperanto
  151. "es", // Spanish
  152. "es_AR", // Spanish (Argentina)
  153. "es_BO", // Spanish (Bolivia)
  154. "es_CL", // Spanish (Chile)
  155. "es_CO", // Spanish (Colombia)
  156. "es_CR", // Spanish (Costa Rica)
  157. "es_CU", // Spanish (Cuba)
  158. "es_DO", // Spanish (Dominican Republic)
  159. "es_EC", // Spanish (Ecuador)
  160. "es_ES", // Spanish (Spain)
  161. "es_GT", // Spanish (Guatemala)
  162. "es_HN", // Spanish (Honduras)
  163. "es_MX", // Spanish (Mexico)
  164. "es_NI", // Spanish (Nicaragua)
  165. "es_PA", // Spanish (Panama)
  166. "es_PE", // Spanish (Peru)
  167. "es_PR", // Spanish (Puerto Rico)
  168. "es_PY", // Spanish (Paraguay)
  169. "es_SV", // Spanish (El Salvador)
  170. "es_US", // Spanish (United States)
  171. "es_UY", // Spanish (Uruguay)
  172. "es_VE", // Spanish (Venezuela)
  173. "et", // Estonian
  174. "et_EE", // Estonian (Estonia)
  175. "eu", // Basque
  176. "eu_ES", // Basque (Spain)
  177. "fa", // Persian
  178. "fa_IR", // Persian (Iran)
  179. "ff_SN", // Fulah (Senegal)
  180. "fi", // Finnish
  181. "fi_FI", // Finnish (Finland)
  182. "fil", // Filipino
  183. "fil_PH", // Filipino (Philippines)
  184. "fo_FO", // Faroese (Faroe Islands)
  185. "fr", // French
  186. "fr_BE", // French (Belgium)
  187. "fr_CA", // French (Canada)
  188. "fr_CH", // French (Switzerland)
  189. "fr_FR", // French (France)
  190. "fr_LU", // French (Luxembourg)
  191. "fur_IT", // Friulian (Italy)
  192. "fy_DE", // Western Frisian (Germany)
  193. "fy_NL", // Western Frisian (Netherlands)
  194. "ga", // Irish
  195. "ga_IE", // Irish (Ireland)
  196. "gd_GB", // Scottish Gaelic (United Kingdom)
  197. "gez_ER", // Geez (Eritrea)
  198. "gez_ET", // Geez (Ethiopia)
  199. "gl_ES", // Galician (Spain)
  200. "gu_IN", // Gujarati (India)
  201. "gv_GB", // Manx (United Kingdom)
  202. "hak_TW", // Hakka Chinese (Taiwan)
  203. "ha_NG", // Hausa (Nigeria)
  204. "he", // Hebrew
  205. "he_IL", // Hebrew (Israel)
  206. "hi", // Hindi
  207. "hi_IN", // Hindi (India)
  208. "hne_IN", // Chhattisgarhi (India)
  209. "hr", // Croatian
  210. "hr_HR", // Croatian (Croatia)
  211. "hsb_DE", // Upper Sorbian (Germany)
  212. "ht_HT", // Haitian (Haiti)
  213. "hu", // Hungarian
  214. "hu_HU", // Hungarian (Hungary)
  215. "hus_MX", // Huastec (Mexico)
  216. "hy_AM", // Armenian (Armenia)
  217. "ia_FR", // Interlingua (France)
  218. "id", // Indonesian
  219. "id_ID", // Indonesian (Indonesia)
  220. "ig_NG", // Igbo (Nigeria)
  221. "ik_CA", // Inupiaq (Canada)
  222. "is", // Icelandic
  223. "is_IS", // Icelandic (Iceland)
  224. "it", // Italian
  225. "it_CH", // Italian (Switzerland)
  226. "it_IT", // Italian (Italy)
  227. "iu_CA", // Inuktitut (Canada)
  228. "ja", // Japanese
  229. "ja_JP", // Japanese (Japan)
  230. "kab_DZ", // Kabyle (Algeria)
  231. "ka", // Georgian
  232. "ka_GE", // Georgian (Georgia)
  233. "kk_KZ", // Kazakh (Kazakhstan)
  234. "kl_GL", // Kalaallisut (Greenland)
  235. "km_KH", // Central Khmer (Cambodia)
  236. "kn_IN", // Kannada (India)
  237. "kok_IN", // Konkani (India)
  238. "ko", // Korean
  239. "ko_KR", // Korean (South Korea)
  240. "ks_IN", // Kashmiri (India)
  241. "ku", // Kurdish
  242. "ku_TR", // Kurdish (Turkey)
  243. "kw_GB", // Cornish (United Kingdom)
  244. "ky_KG", // Kirghiz (Kyrgyzstan)
  245. "lb_LU", // Luxembourgish (Luxembourg)
  246. "lg_UG", // Ganda (Uganda)
  247. "li_BE", // Limburgan (Belgium)
  248. "li_NL", // Limburgan (Netherlands)
  249. "lij_IT", // Ligurian (Italy)
  250. "ln_CD", // Lingala (Congo)
  251. "lo_LA", // Lao (Laos)
  252. "lt", // Lithuanian
  253. "lt_LT", // Lithuanian (Lithuania)
  254. "lv", // Latvian
  255. "lv_LV", // Latvian (Latvia)
  256. "lzh_TW", // Literary Chinese (Taiwan)
  257. "mag_IN", // Magahi (India)
  258. "mai_IN", // Maithili (India)
  259. "mg_MG", // Malagasy (Madagascar)
  260. "mh_MH", // Marshallese (Marshall Islands)
  261. "mhr_RU", // Eastern Mari (Russia)
  262. "mi", // Māori
  263. "mi_NZ", // Māori (New Zealand)
  264. "miq_NI", // Mískito (Nicaragua)
  265. "mk", // Macedonian
  266. "mk_MK", // Macedonian (Macedonia)
  267. "ml", // Malayalam
  268. "ml_IN", // Malayalam (India)
  269. "mni_IN", // Manipuri (India)
  270. "mn_MN", // Mongolian (Mongolia)
  271. "mr_IN", // Marathi (India)
  272. "ms", // Malay
  273. "ms_MY", // Malay (Malaysia)
  274. "mt", // Maltese
  275. "mt_MT", // Maltese (Malta)
  276. "my_MM", // Burmese (Myanmar)
  277. "myv_RU", // Erzya (Russia)
  278. "nah_MX", // Nahuatl languages (Mexico)
  279. "nan_TW", // Min Nan Chinese (Taiwan)
  280. "nb", // Norwegian Bokmål
  281. "nb_NO", // Norwegian Bokmål (Norway)
  282. "nds_DE", // Low German (Germany)
  283. "nds_NL", // Low German (Netherlands)
  284. "ne_NP", // Nepali (Nepal)
  285. "nhn_MX", // Central Nahuatl (Mexico)
  286. "niu_NU", // Niuean (Niue)
  287. "niu_NZ", // Niuean (New Zealand)
  288. "nl", // Dutch
  289. "nl_AW", // Dutch (Aruba)
  290. "nl_BE", // Dutch (Belgium)
  291. "nl_NL", // Dutch (Netherlands)
  292. "nn", // Norwegian Nynorsk
  293. "nn_NO", // Norwegian Nynorsk (Norway)
  294. "nr_ZA", // South Ndebele (South Africa)
  295. "nso_ZA", // Pedi (South Africa)
  296. "oc_FR", // Occitan (France)
  297. "om", // Oromo
  298. "om_ET", // Oromo (Ethiopia)
  299. "om_KE", // Oromo (Kenya)
  300. "or_IN", // Oriya (India)
  301. "os_RU", // Ossetian (Russia)
  302. "pa_IN", // Panjabi (India)
  303. "pap", // Papiamento
  304. "pap_AN", // Papiamento (Netherlands Antilles)
  305. "pap_AW", // Papiamento (Aruba)
  306. "pap_CW", // Papiamento (Curaçao)
  307. "pa_PK", // Panjabi (Pakistan)
  308. "pl", // Polish
  309. "pl_PL", // Polish (Poland)
  310. "pr", // Pirate
  311. "ps_AF", // Pushto (Afghanistan)
  312. "pt", // Portuguese
  313. "pt_BR", // Portuguese (Brazil)
  314. "pt_PT", // Portuguese (Portugal)
  315. "quy_PE", // Ayacucho Quechua (Peru)
  316. "quz_PE", // Cusco Quechua (Peru)
  317. "raj_IN", // Rajasthani (India)
  318. "ro", // Romanian
  319. "ro_RO", // Romanian (Romania)
  320. "ru", // Russian
  321. "ru_RU", // Russian (Russia)
  322. "ru_UA", // Russian (Ukraine)
  323. "rw_RW", // Kinyarwanda (Rwanda)
  324. "sa_IN", // Sanskrit (India)
  325. "sat_IN", // Santali (India)
  326. "sc_IT", // Sardinian (Italy)
  327. "sco", // Scots
  328. "sd_IN", // Sindhi (India)
  329. "se_NO", // Northern Sami (Norway)
  330. "sgs_LT", // Samogitian (Lithuania)
  331. "shs_CA", // Shuswap (Canada)
  332. "sid_ET", // Sidamo (Ethiopia)
  333. "si", // Sinhala
  334. "si_LK", // Sinhala (Sri Lanka)
  335. "sk", // Slovak
  336. "sk_SK", // Slovak (Slovakia)
  337. "sl", // Slovenian
  338. "sl_SI", // Slovenian (Slovenia)
  339. "so", // Somali
  340. "so_DJ", // Somali (Djibouti)
  341. "so_ET", // Somali (Ethiopia)
  342. "so_KE", // Somali (Kenya)
  343. "so_SO", // Somali (Somalia)
  344. "son_ML", // Songhai languages (Mali)
  345. "sq", // Albanian
  346. "sq_AL", // Albanian (Albania)
  347. "sq_KV", // Albanian (Kosovo)
  348. "sq_MK", // Albanian (Macedonia)
  349. "sr", // Serbian
  350. "sr_Cyrl", // Serbian (Cyrillic)
  351. "sr_Latn", // Serbian (Latin)
  352. "sr_ME", // Serbian (Montenegro)
  353. "sr_RS", // Serbian (Serbia)
  354. "ss_ZA", // Swati (South Africa)
  355. "st_ZA", // Southern Sotho (South Africa)
  356. "sv", // Swedish
  357. "sv_FI", // Swedish (Finland)
  358. "sv_SE", // Swedish (Sweden)
  359. "sw_KE", // Swahili (Kenya)
  360. "sw_TZ", // Swahili (Tanzania)
  361. "szl_PL", // Silesian (Poland)
  362. "ta", // Tamil
  363. "ta_IN", // Tamil (India)
  364. "ta_LK", // Tamil (Sri Lanka)
  365. "tcy_IN", // Tulu (India)
  366. "te", // Telugu
  367. "te_IN", // Telugu (India)
  368. "tg_TJ", // Tajik (Tajikistan)
  369. "the_NP", // Chitwania Tharu (Nepal)
  370. "th", // Thai
  371. "th_TH", // Thai (Thailand)
  372. "ti", // Tigrinya
  373. "ti_ER", // Tigrinya (Eritrea)
  374. "ti_ET", // Tigrinya (Ethiopia)
  375. "tig_ER", // Tigre (Eritrea)
  376. "tk_TM", // Turkmen (Turkmenistan)
  377. "tl_PH", // Tagalog (Philippines)
  378. "tn_ZA", // Tswana (South Africa)
  379. "tr", // Turkish
  380. "tr_CY", // Turkish (Cyprus)
  381. "tr_TR", // Turkish (Turkey)
  382. "ts_ZA", // Tsonga (South Africa)
  383. "tt_RU", // Tatar (Russia)
  384. "ug_CN", // Uighur (China)
  385. "uk", // Ukrainian
  386. "uk_UA", // Ukrainian (Ukraine)
  387. "unm_US", // Unami (United States)
  388. "ur", // Urdu
  389. "ur_IN", // Urdu (India)
  390. "ur_PK", // Urdu (Pakistan)
  391. "uz", // Uzbek
  392. "uz_UZ", // Uzbek (Uzbekistan)
  393. "ve_ZA", // Venda (South Africa)
  394. "vi", // Vietnamese
  395. "vi_VN", // Vietnamese (Vietnam)
  396. "wa_BE", // Walloon (Belgium)
  397. "wae_CH", // Walser (Switzerland)
  398. "wal_ET", // Wolaytta (Ethiopia)
  399. "wo_SN", // Wolof (Senegal)
  400. "xh_ZA", // Xhosa (South Africa)
  401. "yi_US", // Yiddish (United States)
  402. "yo_NG", // Yoruba (Nigeria)
  403. "yue_HK", // Yue Chinese (Hong Kong)
  404. "zh", // Chinese
  405. "zh_CN", // Chinese (China)
  406. "zh_HK", // Chinese (Hong Kong)
  407. "zh_SG", // Chinese (Singapore)
  408. "zh_TW", // Chinese (Taiwan)
  409. "zu_ZA", // Zulu (South Africa)
  410. nullptr
  411. };
  412. static const char *locale_names[] = {
  413. "Afar",
  414. "Afar (Djibouti)",
  415. "Afar (Eritrea)",
  416. "Afar (Ethiopia)",
  417. "Afrikaans",
  418. "Afrikaans (South Africa)",
  419. "Aguaruna (Peru)",
  420. "Akan (Ghana)",
  421. "Amharic (Ethiopia)",
  422. "Aragonese (Spain)",
  423. "Angika (India)",
  424. "Arabic",
  425. "Arabic (United Arab Emirates)",
  426. "Arabic (Bahrain)",
  427. "Arabic (Algeria)",
  428. "Arabic (Egypt)",
  429. "Arabic (India)",
  430. "Arabic (Iraq)",
  431. "Arabic (Jordan)",
  432. "Arabic (Kuwait)",
  433. "Arabic (Lebanon)",
  434. "Arabic (Libya)",
  435. "Arabic (Morocco)",
  436. "Arabic (Oman)",
  437. "Arabic (Qatar)",
  438. "Arabic (Saudi Arabia)",
  439. "Arabic (Sudan)",
  440. "Arabic (South Soudan)",
  441. "Arabic (Syria)",
  442. "Arabic (Tunisia)",
  443. "Arabic (Yemen)",
  444. "Assamese (India)",
  445. "Asturian (Spain)",
  446. "Southern Aymara (Peru)",
  447. "Aymara (Peru)",
  448. "Azerbaijani (Azerbaijan)",
  449. "Belarusian",
  450. "Belarusian (Belarus)",
  451. "Bemba (Zambia)",
  452. "Berber languages (Algeria)",
  453. "Berber languages (Morocco)",
  454. "Bulgarian",
  455. "Bulgarian (Bulgaria)",
  456. "Bhili (India)",
  457. "Bhojpuri (India)",
  458. "Bislama (Tuvalu)",
  459. "Bengali",
  460. "Bengali (Bangladesh)",
  461. "Bengali (India)",
  462. "Tibetan",
  463. "Tibetan (China)",
  464. "Tibetan (India)",
  465. "Breton (France)",
  466. "Bodo (India)",
  467. "Bosnian (Bosnia and Herzegovina)",
  468. "Bilin (Eritrea)",
  469. "Catalan",
  470. "Catalan (Andorra)",
  471. "Catalan (Spain)",
  472. "Catalan (France)",
  473. "Catalan (Italy)",
  474. "Chechen (Russia)",
  475. "Cherokee (United States)",
  476. "Mandarin Chinese (Taiwan)",
  477. "Crimean Tatar (Ukraine)",
  478. "Kashubian (Poland)",
  479. "Czech",
  480. "Czech (Czech Republic)",
  481. "Chuvash (Russia)",
  482. "Welsh (United Kingdom)",
  483. "Danish",
  484. "Danish (Denmark)",
  485. "German",
  486. "German (Austria)",
  487. "German (Belgium)",
  488. "German (Switzerland)",
  489. "German (Germany)",
  490. "German (Italy)",
  491. "German (Luxembourg)",
  492. "Dogri (India)",
  493. "Dhivehi (Maldives)",
  494. "Dzongkha (Bhutan)",
  495. "Greek",
  496. "Greek (Cyprus)",
  497. "Greek (Greece)",
  498. "English",
  499. "English (Antigua and Barbuda)",
  500. "English (Australia)",
  501. "English (Botswana)",
  502. "English (Canada)",
  503. "English (Denmark)",
  504. "English (United Kingdom)",
  505. "English (Hong Kong)",
  506. "English (Ireland)",
  507. "English (Israel)",
  508. "English (India)",
  509. "English (Nigeria)",
  510. "English (New Zealand)",
  511. "English (Philippines)",
  512. "English (Singapore)",
  513. "English (United States)",
  514. "English (South Africa)",
  515. "English (Zambia)",
  516. "English (Zimbabwe)",
  517. "Esperanto",
  518. "Spanish",
  519. "Spanish (Argentina)",
  520. "Spanish (Bolivia)",
  521. "Spanish (Chile)",
  522. "Spanish (Colombia)",
  523. "Spanish (Costa Rica)",
  524. "Spanish (Cuba)",
  525. "Spanish (Dominican Republic)",
  526. "Spanish (Ecuador)",
  527. "Spanish (Spain)",
  528. "Spanish (Guatemala)",
  529. "Spanish (Honduras)",
  530. "Spanish (Mexico)",
  531. "Spanish (Nicaragua)",
  532. "Spanish (Panama)",
  533. "Spanish (Peru)",
  534. "Spanish (Puerto Rico)",
  535. "Spanish (Paraguay)",
  536. "Spanish (El Salvador)",
  537. "Spanish (United States)",
  538. "Spanish (Uruguay)",
  539. "Spanish (Venezuela)",
  540. "Estonian",
  541. "Estonian (Estonia)",
  542. "Basque",
  543. "Basque (Spain)",
  544. "Persian",
  545. "Persian (Iran)",
  546. "Fulah (Senegal)",
  547. "Finnish",
  548. "Finnish (Finland)",
  549. "Filipino",
  550. "Filipino (Philippines)",
  551. "Faroese (Faroe Islands)",
  552. "French",
  553. "French (Belgium)",
  554. "French (Canada)",
  555. "French (Switzerland)",
  556. "French (France)",
  557. "French (Luxembourg)",
  558. "Friulian (Italy)",
  559. "Western Frisian (Germany)",
  560. "Western Frisian (Netherlands)",
  561. "Irish",
  562. "Irish (Ireland)",
  563. "Scottish Gaelic (United Kingdom)",
  564. "Geez (Eritrea)",
  565. "Geez (Ethiopia)",
  566. "Galician (Spain)",
  567. "Gujarati (India)",
  568. "Manx (United Kingdom)",
  569. "Hakka Chinese (Taiwan)",
  570. "Hausa (Nigeria)",
  571. "Hebrew",
  572. "Hebrew (Israel)",
  573. "Hindi",
  574. "Hindi (India)",
  575. "Chhattisgarhi (India)",
  576. "Croatian",
  577. "Croatian (Croatia)",
  578. "Upper Sorbian (Germany)",
  579. "Haitian (Haiti)",
  580. "Hungarian",
  581. "Hungarian (Hungary)",
  582. "Huastec (Mexico)",
  583. "Armenian (Armenia)",
  584. "Interlingua (France)",
  585. "Indonesian",
  586. "Indonesian (Indonesia)",
  587. "Igbo (Nigeria)",
  588. "Inupiaq (Canada)",
  589. "Icelandic",
  590. "Icelandic (Iceland)",
  591. "Italian",
  592. "Italian (Switzerland)",
  593. "Italian (Italy)",
  594. "Inuktitut (Canada)",
  595. "Japanese",
  596. "Japanese (Japan)",
  597. "Kabyle (Algeria)",
  598. "Georgian",
  599. "Georgian (Georgia)",
  600. "Kazakh (Kazakhstan)",
  601. "Kalaallisut (Greenland)",
  602. "Central Khmer (Cambodia)",
  603. "Kannada (India)",
  604. "Konkani (India)",
  605. "Korean",
  606. "Korean (South Korea)",
  607. "Kashmiri (India)",
  608. "Kurdish",
  609. "Kurdish (Turkey)",
  610. "Cornish (United Kingdom)",
  611. "Kirghiz (Kyrgyzstan)",
  612. "Luxembourgish (Luxembourg)",
  613. "Ganda (Uganda)",
  614. "Limburgan (Belgium)",
  615. "Limburgan (Netherlands)",
  616. "Ligurian (Italy)",
  617. "Lingala (Congo)",
  618. "Lao (Laos)",
  619. "Lithuanian",
  620. "Lithuanian (Lithuania)",
  621. "Latvian",
  622. "Latvian (Latvia)",
  623. "Literary Chinese (Taiwan)",
  624. "Magahi (India)",
  625. "Maithili (India)",
  626. "Malagasy (Madagascar)",
  627. "Marshallese (Marshall Islands)",
  628. "Eastern Mari (Russia)",
  629. "Māori",
  630. "Māori (New Zealand)",
  631. "Mískito (Nicaragua)",
  632. "Macedonian",
  633. "Macedonian (Macedonia)",
  634. "Malayalam",
  635. "Malayalam (India)",
  636. "Manipuri (India)",
  637. "Mongolian (Mongolia)",
  638. "Marathi (India)",
  639. "Malay",
  640. "Malay (Malaysia)",
  641. "Maltese",
  642. "Maltese (Malta)",
  643. "Burmese (Myanmar)",
  644. "Erzya (Russia)",
  645. "Nahuatl languages (Mexico)",
  646. "Min Nan Chinese (Taiwan)",
  647. "Norwegian Bokmål",
  648. "Norwegian Bokmål (Norway)",
  649. "Low German (Germany)",
  650. "Low German (Netherlands)",
  651. "Nepali (Nepal)",
  652. "Central Nahuatl (Mexico)",
  653. "Niuean (Niue)",
  654. "Niuean (New Zealand)",
  655. "Dutch",
  656. "Dutch (Aruba)",
  657. "Dutch (Belgium)",
  658. "Dutch (Netherlands)",
  659. "Norwegian Nynorsk",
  660. "Norwegian Nynorsk (Norway)",
  661. "South Ndebele (South Africa)",
  662. "Pedi (South Africa)",
  663. "Occitan (France)",
  664. "Oromo",
  665. "Oromo (Ethiopia)",
  666. "Oromo (Kenya)",
  667. "Oriya (India)",
  668. "Ossetian (Russia)",
  669. "Panjabi (India)",
  670. "Papiamento",
  671. "Papiamento (Netherlands Antilles)",
  672. "Papiamento (Aruba)",
  673. "Papiamento (Curaçao)",
  674. "Panjabi (Pakistan)",
  675. "Polish",
  676. "Polish (Poland)",
  677. "Pirate",
  678. "Pushto (Afghanistan)",
  679. "Portuguese",
  680. "Portuguese (Brazil)",
  681. "Portuguese (Portugal)",
  682. "Ayacucho Quechua (Peru)",
  683. "Cusco Quechua (Peru)",
  684. "Rajasthani (India)",
  685. "Romanian",
  686. "Romanian (Romania)",
  687. "Russian",
  688. "Russian (Russia)",
  689. "Russian (Ukraine)",
  690. "Kinyarwanda (Rwanda)",
  691. "Sanskrit (India)",
  692. "Santali (India)",
  693. "Sardinian (Italy)",
  694. "Scots (Scotland)",
  695. "Sindhi (India)",
  696. "Northern Sami (Norway)",
  697. "Samogitian (Lithuania)",
  698. "Shuswap (Canada)",
  699. "Sidamo (Ethiopia)",
  700. "Sinhala",
  701. "Sinhala (Sri Lanka)",
  702. "Slovak",
  703. "Slovak (Slovakia)",
  704. "Slovenian",
  705. "Slovenian (Slovenia)",
  706. "Somali",
  707. "Somali (Djibouti)",
  708. "Somali (Ethiopia)",
  709. "Somali (Kenya)",
  710. "Somali (Somalia)",
  711. "Songhai languages (Mali)",
  712. "Albanian",
  713. "Albanian (Albania)",
  714. "Albanian (Kosovo)",
  715. "Albanian (Macedonia)",
  716. "Serbian",
  717. "Serbian (Cyrillic)",
  718. "Serbian (Latin)",
  719. "Serbian (Montenegro)",
  720. "Serbian (Serbia)",
  721. "Swati (South Africa)",
  722. "Southern Sotho (South Africa)",
  723. "Swedish",
  724. "Swedish (Finland)",
  725. "Swedish (Sweden)",
  726. "Swahili (Kenya)",
  727. "Swahili (Tanzania)",
  728. "Silesian (Poland)",
  729. "Tamil",
  730. "Tamil (India)",
  731. "Tamil (Sri Lanka)",
  732. "Tulu (India)",
  733. "Telugu",
  734. "Telugu (India)",
  735. "Tajik (Tajikistan)",
  736. "Chitwania Tharu (Nepal)",
  737. "Thai",
  738. "Thai (Thailand)",
  739. "Tigrinya",
  740. "Tigrinya (Eritrea)",
  741. "Tigrinya (Ethiopia)",
  742. "Tigre (Eritrea)",
  743. "Turkmen (Turkmenistan)",
  744. "Tagalog (Philippines)",
  745. "Tswana (South Africa)",
  746. "Turkish",
  747. "Turkish (Cyprus)",
  748. "Turkish (Turkey)",
  749. "Tsonga (South Africa)",
  750. "Tatar (Russia)",
  751. "Uighur (China)",
  752. "Ukrainian",
  753. "Ukrainian (Ukraine)",
  754. "Unami (United States)",
  755. "Urdu",
  756. "Urdu (India)",
  757. "Urdu (Pakistan)",
  758. "Uzbek",
  759. "Uzbek (Uzbekistan)",
  760. "Venda (South Africa)",
  761. "Vietnamese",
  762. "Vietnamese (Vietnam)",
  763. "Walloon (Belgium)",
  764. "Walser (Switzerland)",
  765. "Wolaytta (Ethiopia)",
  766. "Wolof (Senegal)",
  767. "Xhosa (South Africa)",
  768. "Yiddish (United States)",
  769. "Yoruba (Nigeria)",
  770. "Yue Chinese (Hong Kong)",
  771. "Chinese",
  772. "Chinese (China)",
  773. "Chinese (Hong Kong)",
  774. "Chinese (Singapore)",
  775. "Chinese (Taiwan)",
  776. "Zulu (South Africa)",
  777. nullptr
  778. };
  779. // Windows has some weird locale identifiers which do not honor the ISO 639-1
  780. // standardized nomenclature. Whenever those don't conflict with existing ISO
  781. // identifiers, we override them.
  782. //
  783. // Reference:
  784. // - https://msdn.microsoft.com/en-us/library/windows/desktop/ms693062(v=vs.85).aspx
  785. static const char *locale_renames[][2] = {
  786. { "in", "id" }, // Indonesian
  787. { "iw", "he" }, // Hebrew
  788. { "no", "nb" }, // Norwegian Bokmål
  789. { nullptr, nullptr }
  790. };
  791. ///////////////////////////////////////////////
  792. Dictionary Translation::_get_messages() const {
  793. Dictionary d;
  794. for (const Map<StringName, StringName>::Element *E = translation_map.front(); E; E = E->next()) {
  795. d[E->key()] = E->value();
  796. }
  797. return d;
  798. }
  799. Vector<String> Translation::_get_message_list() const {
  800. Vector<String> msgs;
  801. msgs.resize(translation_map.size());
  802. int idx = 0;
  803. for (const Map<StringName, StringName>::Element *E = translation_map.front(); E; E = E->next()) {
  804. msgs.set(idx, E->key());
  805. idx += 1;
  806. }
  807. return msgs;
  808. }
  809. void Translation::_set_messages(const Dictionary &p_messages) {
  810. List<Variant> keys;
  811. p_messages.get_key_list(&keys);
  812. for (auto E = keys.front(); E; E = E->next()) {
  813. translation_map[E->get()] = p_messages[E->get()];
  814. }
  815. }
  816. void Translation::set_locale(const String &p_locale) {
  817. String univ_locale = TranslationServer::standardize_locale(p_locale);
  818. if (!TranslationServer::is_locale_valid(univ_locale)) {
  819. String trimmed_locale = TranslationServer::get_language_code(univ_locale);
  820. ERR_FAIL_COND_MSG(!TranslationServer::is_locale_valid(trimmed_locale), "Invalid locale: " + trimmed_locale + ".");
  821. locale = trimmed_locale;
  822. } else {
  823. locale = univ_locale;
  824. }
  825. if (OS::get_singleton()->get_main_loop()) {
  826. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  827. }
  828. }
  829. void Translation::add_message(const StringName &p_src_text, const StringName &p_xlated_text, const StringName &p_context) {
  830. translation_map[p_src_text] = p_xlated_text;
  831. }
  832. void Translation::add_plural_message(const StringName &p_src_text, const Vector<String> &p_plural_xlated_texts, const StringName &p_context) {
  833. 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");
  834. ERR_FAIL_COND_MSG(p_plural_xlated_texts.is_empty(), "Parameter vector p_plural_xlated_texts passed in is empty.");
  835. translation_map[p_src_text] = p_plural_xlated_texts[0];
  836. }
  837. StringName Translation::get_message(const StringName &p_src_text, const StringName &p_context) const {
  838. if (p_context != StringName()) {
  839. 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");
  840. }
  841. const Map<StringName, StringName>::Element *E = translation_map.find(p_src_text);
  842. if (!E) {
  843. return StringName();
  844. }
  845. return E->get();
  846. }
  847. StringName Translation::get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context) const {
  848. 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");
  849. return get_message(p_src_text);
  850. }
  851. void Translation::erase_message(const StringName &p_src_text, const StringName &p_context) {
  852. if (p_context != StringName()) {
  853. 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");
  854. }
  855. translation_map.erase(p_src_text);
  856. }
  857. void Translation::get_message_list(List<StringName> *r_messages) const {
  858. for (const Map<StringName, StringName>::Element *E = translation_map.front(); E; E = E->next()) {
  859. r_messages->push_back(E->key());
  860. }
  861. }
  862. int Translation::get_message_count() const {
  863. return translation_map.size();
  864. }
  865. void Translation::_bind_methods() {
  866. ClassDB::bind_method(D_METHOD("set_locale", "locale"), &Translation::set_locale);
  867. ClassDB::bind_method(D_METHOD("get_locale"), &Translation::get_locale);
  868. ClassDB::bind_method(D_METHOD("add_message", "src_message", "xlated_message", "context"), &Translation::add_message, DEFVAL(""));
  869. ClassDB::bind_method(D_METHOD("add_plural_message", "src_message", "xlated_messages", "context"), &Translation::add_plural_message, DEFVAL(""));
  870. ClassDB::bind_method(D_METHOD("get_message", "src_message", "context"), &Translation::get_message, DEFVAL(""));
  871. ClassDB::bind_method(D_METHOD("get_plural_message", "src_message", "src_plural_message", "n", "context"), &Translation::get_plural_message, DEFVAL(""));
  872. ClassDB::bind_method(D_METHOD("erase_message", "src_message", "context"), &Translation::erase_message, DEFVAL(""));
  873. ClassDB::bind_method(D_METHOD("get_message_list"), &Translation::_get_message_list);
  874. ClassDB::bind_method(D_METHOD("get_message_count"), &Translation::get_message_count);
  875. ClassDB::bind_method(D_METHOD("_set_messages"), &Translation::_set_messages);
  876. ClassDB::bind_method(D_METHOD("_get_messages"), &Translation::_get_messages);
  877. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "messages", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_messages", "_get_messages");
  878. ADD_PROPERTY(PropertyInfo(Variant::STRING, "locale"), "set_locale", "get_locale");
  879. }
  880. ///////////////////////////////////////////////
  881. bool TranslationServer::is_locale_valid(const String &p_locale) {
  882. const char **ptr = locale_list;
  883. while (*ptr) {
  884. if (*ptr == p_locale) {
  885. return true;
  886. }
  887. ptr++;
  888. }
  889. return false;
  890. }
  891. String TranslationServer::standardize_locale(const String &p_locale) {
  892. // Replaces '-' with '_' for macOS Sierra-style locales
  893. String univ_locale = p_locale.replace("-", "_");
  894. // Handles known non-ISO locale names used e.g. on Windows
  895. int idx = 0;
  896. while (locale_renames[idx][0] != nullptr) {
  897. if (locale_renames[idx][0] == univ_locale) {
  898. univ_locale = locale_renames[idx][1];
  899. break;
  900. }
  901. idx++;
  902. }
  903. return univ_locale;
  904. }
  905. String TranslationServer::get_language_code(const String &p_locale) {
  906. ERR_FAIL_COND_V_MSG(p_locale.length() < 2, p_locale, "Invalid locale '" + p_locale + "'.");
  907. // Most language codes are two letters, but some are three,
  908. // so we have to look for a regional code separator ('_' or '-')
  909. // to extract the left part.
  910. // For example we get 'nah_MX' as input and should return 'nah'.
  911. int split = p_locale.find("_");
  912. if (split == -1) {
  913. split = p_locale.find("-");
  914. }
  915. if (split == -1) { // No separator, so the locale is already only a language code.
  916. return p_locale;
  917. }
  918. return p_locale.left(split);
  919. }
  920. void TranslationServer::set_locale(const String &p_locale) {
  921. String univ_locale = standardize_locale(p_locale);
  922. if (!is_locale_valid(univ_locale)) {
  923. String trimmed_locale = get_language_code(univ_locale);
  924. print_verbose(vformat("Unsupported locale '%s', falling back to '%s'.", p_locale, trimmed_locale));
  925. if (!is_locale_valid(trimmed_locale)) {
  926. ERR_PRINT(vformat("Unsupported locale '%s', falling back to 'en'.", trimmed_locale));
  927. locale = "en";
  928. } else {
  929. locale = trimmed_locale;
  930. }
  931. } else {
  932. locale = univ_locale;
  933. }
  934. if (OS::get_singleton()->get_main_loop()) {
  935. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  936. }
  937. ResourceLoader::reload_translation_remaps();
  938. }
  939. String TranslationServer::get_locale() const {
  940. return locale;
  941. }
  942. String TranslationServer::get_locale_name(const String &p_locale) const {
  943. if (!locale_name_map.has(p_locale)) {
  944. return String();
  945. }
  946. return locale_name_map[p_locale];
  947. }
  948. Array TranslationServer::get_loaded_locales() const {
  949. Array locales;
  950. for (const Set<Ref<Translation>>::Element *E = translations.front(); E; E = E->next()) {
  951. const Ref<Translation> &t = E->get();
  952. ERR_FAIL_COND_V(t.is_null(), Array());
  953. String l = t->get_locale();
  954. locales.push_back(l);
  955. }
  956. return locales;
  957. }
  958. Vector<String> TranslationServer::get_all_locales() {
  959. Vector<String> locales;
  960. const char **ptr = locale_list;
  961. while (*ptr) {
  962. locales.push_back(*ptr);
  963. ptr++;
  964. }
  965. return locales;
  966. }
  967. Vector<String> TranslationServer::get_all_locale_names() {
  968. Vector<String> locales;
  969. const char **ptr = locale_names;
  970. while (*ptr) {
  971. locales.push_back(String::utf8(*ptr));
  972. ptr++;
  973. }
  974. return locales;
  975. }
  976. void TranslationServer::add_translation(const Ref<Translation> &p_translation) {
  977. translations.insert(p_translation);
  978. }
  979. void TranslationServer::remove_translation(const Ref<Translation> &p_translation) {
  980. translations.erase(p_translation);
  981. }
  982. Ref<Translation> TranslationServer::get_translation_object(const String &p_locale) {
  983. Ref<Translation> res;
  984. String lang = get_language_code(p_locale);
  985. bool near_match_found = false;
  986. for (const Set<Ref<Translation>>::Element *E = translations.front(); E; E = E->next()) {
  987. const Ref<Translation> &t = E->get();
  988. ERR_FAIL_COND_V(t.is_null(), nullptr);
  989. String l = t->get_locale();
  990. // Exact match.
  991. if (l == p_locale) {
  992. return t;
  993. }
  994. // If near match found, keep that match, but keep looking to try to look for perfect match.
  995. if (get_language_code(l) == lang && !near_match_found) {
  996. res = t;
  997. near_match_found = true;
  998. }
  999. }
  1000. return res;
  1001. }
  1002. void TranslationServer::clear() {
  1003. translations.clear();
  1004. }
  1005. StringName TranslationServer::translate(const StringName &p_message, const StringName &p_context) const {
  1006. // Match given message against the translation catalog for the project locale.
  1007. if (!enabled) {
  1008. return p_message;
  1009. }
  1010. ERR_FAIL_COND_V_MSG(locale.length() < 2, p_message, "Could not translate message as configured locale '" + locale + "' is invalid.");
  1011. StringName res = _get_message_from_translations(p_message, p_context, locale, false);
  1012. if (!res && fallback.length() >= 2) {
  1013. res = _get_message_from_translations(p_message, p_context, fallback, false);
  1014. }
  1015. if (!res) {
  1016. return p_message;
  1017. }
  1018. return res;
  1019. }
  1020. StringName TranslationServer::translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  1021. if (!enabled) {
  1022. if (p_n == 1) {
  1023. return p_message;
  1024. }
  1025. return p_message_plural;
  1026. }
  1027. ERR_FAIL_COND_V_MSG(locale.length() < 2, p_message, "Could not translate message as configured locale '" + locale + "' is invalid.");
  1028. StringName res = _get_message_from_translations(p_message, p_context, locale, true, p_message_plural, p_n);
  1029. if (!res && fallback.length() >= 2) {
  1030. res = _get_message_from_translations(p_message, p_context, fallback, true, p_message_plural, p_n);
  1031. }
  1032. if (!res) {
  1033. if (p_n == 1) {
  1034. return p_message;
  1035. }
  1036. return p_message_plural;
  1037. }
  1038. return res;
  1039. }
  1040. 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 {
  1041. // Locale can be of the form 'll_CC', i.e. language code and regional code,
  1042. // e.g. 'en_US', 'en_GB', etc. It might also be simply 'll', e.g. 'en'.
  1043. // To find the relevant translation, we look for those with locale starting
  1044. // with the language code, and then if any is an exact match for the long
  1045. // form. If not found, we fall back to a near match (another locale with
  1046. // same language code).
  1047. // Note: ResourceLoader::_path_remap reproduces this locale near matching
  1048. // logic, so be sure to propagate changes there when changing things here.
  1049. StringName res;
  1050. String lang = get_language_code(p_locale);
  1051. bool near_match = false;
  1052. for (const Set<Ref<Translation>>::Element *E = translations.front(); E; E = E->next()) {
  1053. const Ref<Translation> &t = E->get();
  1054. ERR_FAIL_COND_V(t.is_null(), p_message);
  1055. String l = t->get_locale();
  1056. bool exact_match = (l == p_locale);
  1057. if (!exact_match) {
  1058. if (near_match) {
  1059. continue; // Only near-match once, but keep looking for exact matches.
  1060. }
  1061. if (get_language_code(l) != lang) {
  1062. continue; // Language code does not match.
  1063. }
  1064. }
  1065. StringName r;
  1066. if (!plural) {
  1067. r = t->get_message(p_message, p_context);
  1068. } else {
  1069. r = t->get_plural_message(p_message, p_message_plural, p_n, p_context);
  1070. }
  1071. if (!r) {
  1072. continue;
  1073. }
  1074. res = r;
  1075. if (exact_match) {
  1076. break;
  1077. } else {
  1078. near_match = true;
  1079. }
  1080. }
  1081. return res;
  1082. }
  1083. TranslationServer *TranslationServer::singleton = nullptr;
  1084. bool TranslationServer::_load_translations(const String &p_from) {
  1085. if (ProjectSettings::get_singleton()->has_setting(p_from)) {
  1086. Vector<String> translations = ProjectSettings::get_singleton()->get(p_from);
  1087. int tcount = translations.size();
  1088. if (tcount) {
  1089. const String *r = translations.ptr();
  1090. for (int i = 0; i < tcount; i++) {
  1091. Ref<Translation> tr = ResourceLoader::load(r[i]);
  1092. if (tr.is_valid()) {
  1093. add_translation(tr);
  1094. }
  1095. }
  1096. }
  1097. return true;
  1098. }
  1099. return false;
  1100. }
  1101. void TranslationServer::setup() {
  1102. String test = GLOBAL_DEF("internationalization/locale/test", "");
  1103. test = test.strip_edges();
  1104. if (test != "") {
  1105. set_locale(test);
  1106. } else {
  1107. set_locale(OS::get_singleton()->get_locale());
  1108. }
  1109. fallback = GLOBAL_DEF("internationalization/locale/fallback", "en");
  1110. #ifdef TOOLS_ENABLED
  1111. {
  1112. String options = "";
  1113. int idx = 0;
  1114. while (locale_list[idx]) {
  1115. if (idx > 0) {
  1116. options += ",";
  1117. }
  1118. options += locale_list[idx];
  1119. idx++;
  1120. }
  1121. ProjectSettings::get_singleton()->set_custom_property_info("internationalization/locale/fallback", PropertyInfo(Variant::STRING, "internationalization/locale/fallback", PROPERTY_HINT_ENUM, options));
  1122. }
  1123. #endif
  1124. }
  1125. void TranslationServer::set_tool_translation(const Ref<Translation> &p_translation) {
  1126. tool_translation = p_translation;
  1127. }
  1128. Ref<Translation> TranslationServer::get_tool_translation() const {
  1129. return tool_translation;
  1130. }
  1131. String TranslationServer::get_tool_locale() {
  1132. #ifdef TOOLS_ENABLED
  1133. if (TranslationServer::get_singleton()->get_tool_translation().is_valid() && (Engine::get_singleton()->is_editor_hint() || Main::is_project_manager())) {
  1134. return tool_translation->get_locale();
  1135. } else {
  1136. #else
  1137. {
  1138. #endif
  1139. return get_locale();
  1140. }
  1141. }
  1142. StringName TranslationServer::tool_translate(const StringName &p_message, const StringName &p_context) const {
  1143. if (tool_translation.is_valid()) {
  1144. StringName r = tool_translation->get_message(p_message, p_context);
  1145. if (r) {
  1146. return r;
  1147. }
  1148. }
  1149. return p_message;
  1150. }
  1151. StringName TranslationServer::tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  1152. if (tool_translation.is_valid()) {
  1153. StringName r = tool_translation->get_plural_message(p_message, p_message_plural, p_n, p_context);
  1154. if (r) {
  1155. return r;
  1156. }
  1157. }
  1158. if (p_n == 1) {
  1159. return p_message;
  1160. }
  1161. return p_message_plural;
  1162. }
  1163. void TranslationServer::set_doc_translation(const Ref<Translation> &p_translation) {
  1164. doc_translation = p_translation;
  1165. }
  1166. StringName TranslationServer::doc_translate(const StringName &p_message, const StringName &p_context) const {
  1167. if (doc_translation.is_valid()) {
  1168. StringName r = doc_translation->get_message(p_message, p_context);
  1169. if (r) {
  1170. return r;
  1171. }
  1172. }
  1173. return p_message;
  1174. }
  1175. StringName TranslationServer::doc_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  1176. if (doc_translation.is_valid()) {
  1177. StringName r = doc_translation->get_plural_message(p_message, p_message_plural, p_n, p_context);
  1178. if (r) {
  1179. return r;
  1180. }
  1181. }
  1182. if (p_n == 1) {
  1183. return p_message;
  1184. }
  1185. return p_message_plural;
  1186. }
  1187. void TranslationServer::_bind_methods() {
  1188. ClassDB::bind_method(D_METHOD("set_locale", "locale"), &TranslationServer::set_locale);
  1189. ClassDB::bind_method(D_METHOD("get_locale"), &TranslationServer::get_locale);
  1190. ClassDB::bind_method(D_METHOD("get_locale_name", "locale"), &TranslationServer::get_locale_name);
  1191. ClassDB::bind_method(D_METHOD("translate", "message", "context"), &TranslationServer::translate, DEFVAL(""));
  1192. ClassDB::bind_method(D_METHOD("translate_plural", "message", "plural_message", "n", "context"), &TranslationServer::translate_plural, DEFVAL(""));
  1193. ClassDB::bind_method(D_METHOD("add_translation", "translation"), &TranslationServer::add_translation);
  1194. ClassDB::bind_method(D_METHOD("remove_translation", "translation"), &TranslationServer::remove_translation);
  1195. ClassDB::bind_method(D_METHOD("get_translation_object", "locale"), &TranslationServer::get_translation_object);
  1196. ClassDB::bind_method(D_METHOD("clear"), &TranslationServer::clear);
  1197. ClassDB::bind_method(D_METHOD("get_loaded_locales"), &TranslationServer::get_loaded_locales);
  1198. }
  1199. void TranslationServer::load_translations() {
  1200. String locale = get_locale();
  1201. _load_translations("internationalization/locale/translations"); //all
  1202. _load_translations("internationalization/locale/translations_" + locale.substr(0, 2));
  1203. if (locale.substr(0, 2) != locale) {
  1204. _load_translations("internationalization/locale/translations_" + locale);
  1205. }
  1206. }
  1207. TranslationServer::TranslationServer() {
  1208. singleton = this;
  1209. for (int i = 0; locale_list[i]; ++i) {
  1210. locale_name_map.insert(locale_list[i], String::utf8(locale_names[i]));
  1211. }
  1212. }