translation.cpp 38 KB

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