translation.cpp 33 KB

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