1
0

languages_helper.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. # frozen_string_literal: true
  2. # rubocop:disable Metrics/ModuleLength, Style/WordArray
  3. module LanguagesHelper
  4. ISO_639_1 = {
  5. aa: ['Afar', 'Afaraf'].freeze,
  6. ab: ['Abkhaz', 'аҧсуа бызшәа'].freeze,
  7. ae: ['Avestan', 'avesta'].freeze,
  8. af: ['Afrikaans', 'Afrikaans'].freeze,
  9. ak: ['Akan', 'Akan'].freeze,
  10. am: ['Amharic', 'አማርኛ'].freeze,
  11. an: ['Aragonese', 'aragonés'].freeze,
  12. ar: ['Arabic', 'اللغة العربية'].freeze,
  13. as: ['Assamese', 'অসমীয়া'].freeze,
  14. av: ['Avaric', 'авар мацӀ'].freeze,
  15. ay: ['Aymara', 'aymar aru'].freeze,
  16. az: ['Azerbaijani', 'azərbaycan dili'].freeze,
  17. ba: ['Bashkir', 'башҡорт теле'].freeze,
  18. be: ['Belarusian', 'беларуская мова'].freeze,
  19. bg: ['Bulgarian', 'български език'].freeze,
  20. bh: ['Bihari', 'भोजपुरी'].freeze,
  21. bi: ['Bislama', 'Bislama'].freeze,
  22. bm: ['Bambara', 'bamanankan'].freeze,
  23. bn: ['Bengali', 'বাংলা'].freeze,
  24. bo: ['Tibetan', 'བོད་ཡིག'].freeze,
  25. br: ['Breton', 'brezhoneg'].freeze,
  26. bs: ['Bosnian', 'bosanski jezik'].freeze,
  27. ca: ['Catalan', 'Català'].freeze,
  28. ce: ['Chechen', 'нохчийн мотт'].freeze,
  29. ch: ['Chamorro', 'Chamoru'].freeze,
  30. co: ['Corsican', 'corsu'].freeze,
  31. cr: ['Cree', 'ᓀᐦᐃᔭᐍᐏᐣ'].freeze,
  32. cs: ['Czech', 'čeština'].freeze,
  33. cu: ['Old Church Slavonic', 'ѩзыкъ словѣньскъ'].freeze,
  34. cv: ['Chuvash', 'чӑваш чӗлхи'].freeze,
  35. cy: ['Welsh', 'Cymraeg'].freeze,
  36. da: ['Danish', 'dansk'].freeze,
  37. de: ['German', 'Deutsch'].freeze,
  38. dv: ['Divehi', 'Dhivehi'].freeze,
  39. dz: ['Dzongkha', 'རྫོང་ཁ'].freeze,
  40. ee: ['Ewe', 'Eʋegbe'].freeze,
  41. el: ['Greek', 'Ελληνικά'].freeze,
  42. en: ['English', 'English'].freeze,
  43. eo: ['Esperanto', 'Esperanto'].freeze,
  44. es: ['Spanish', 'Español'].freeze,
  45. et: ['Estonian', 'eesti'].freeze,
  46. eu: ['Basque', 'euskara'].freeze,
  47. fa: ['Persian', 'فارسی'].freeze,
  48. ff: ['Fula', 'Fulfulde'].freeze,
  49. fi: ['Finnish', 'suomi'].freeze,
  50. fj: ['Fijian', 'Vakaviti'].freeze,
  51. fo: ['Faroese', 'føroyskt'].freeze,
  52. fr: ['French', 'Français'].freeze,
  53. fy: ['Western Frisian', 'Frysk'].freeze,
  54. ga: ['Irish', 'Gaeilge'].freeze,
  55. gd: ['Scottish Gaelic', 'Gàidhlig'].freeze,
  56. gl: ['Galician', 'galego'].freeze,
  57. gu: ['Gujarati', 'ગુજરાતી'].freeze,
  58. gv: ['Manx', 'Gaelg'].freeze,
  59. ha: ['Hausa', 'هَوُسَ'].freeze,
  60. he: ['Hebrew', 'עברית'].freeze,
  61. hi: ['Hindi', 'हिन्दी'].freeze,
  62. ho: ['Hiri Motu', 'Hiri Motu'].freeze,
  63. hr: ['Croatian', 'Hrvatski'].freeze,
  64. ht: ['Haitian', 'Kreyòl ayisyen'].freeze,
  65. hu: ['Hungarian', 'magyar'].freeze,
  66. hy: ['Armenian', 'Հայերեն'].freeze,
  67. hz: ['Herero', 'Otjiherero'].freeze,
  68. ia: ['Interlingua', 'Interlingua'].freeze,
  69. id: ['Indonesian', 'Bahasa Indonesia'].freeze,
  70. ie: ['Interlingue', 'Interlingue'].freeze,
  71. ig: ['Igbo', 'Asụsụ Igbo'].freeze,
  72. ii: ['Nuosu', 'ꆈꌠ꒿ Nuosuhxop'].freeze,
  73. ik: ['Inupiaq', 'Iñupiaq'].freeze,
  74. io: ['Ido', 'Ido'].freeze,
  75. is: ['Icelandic', 'Íslenska'].freeze,
  76. it: ['Italian', 'Italiano'].freeze,
  77. iu: ['Inuktitut', 'ᐃᓄᒃᑎᑐᑦ'].freeze,
  78. ja: ['Japanese', '日本語'].freeze,
  79. jv: ['Javanese', 'basa Jawa'].freeze,
  80. ka: ['Georgian', 'ქართული'].freeze,
  81. kg: ['Kongo', 'Kikongo'].freeze,
  82. ki: ['Kikuyu', 'Gĩkũyũ'].freeze,
  83. kj: ['Kwanyama', 'Kuanyama'].freeze,
  84. kk: ['Kazakh', 'қазақ тілі'].freeze,
  85. kl: ['Kalaallisut', 'kalaallisut'].freeze,
  86. km: ['Khmer', 'ខេមរភាសា'].freeze,
  87. kn: ['Kannada', 'ಕನ್ನಡ'].freeze,
  88. ko: ['Korean', '한국어'].freeze,
  89. kr: ['Kanuri', 'Kanuri'].freeze,
  90. ks: ['Kashmiri', 'कश्मीरी'].freeze,
  91. ku: ['Kurmanji (Kurdish)', 'Kurmancî'].freeze,
  92. kv: ['Komi', 'коми кыв'].freeze,
  93. kw: ['Cornish', 'Kernewek'].freeze,
  94. ky: ['Kyrgyz', 'Кыргызча'].freeze,
  95. la: ['Latin', 'latine'].freeze,
  96. lb: ['Luxembourgish', 'Lëtzebuergesch'].freeze,
  97. lg: ['Ganda', 'Luganda'].freeze,
  98. li: ['Limburgish', 'Limburgs'].freeze,
  99. ln: ['Lingala', 'Lingála'].freeze,
  100. lo: ['Lao', 'ລາວ'].freeze,
  101. lt: ['Lithuanian', 'lietuvių kalba'].freeze,
  102. lu: ['Luba-Katanga', 'Tshiluba'].freeze,
  103. lv: ['Latvian', 'latviešu valoda'].freeze,
  104. mg: ['Malagasy', 'fiteny malagasy'].freeze,
  105. mh: ['Marshallese', 'Kajin M̧ajeļ'].freeze,
  106. mi: ['Māori', 'te reo Māori'].freeze,
  107. mk: ['Macedonian', 'македонски јазик'].freeze,
  108. ml: ['Malayalam', 'മലയാളം'].freeze,
  109. mn: ['Mongolian', 'Монгол хэл'].freeze,
  110. mr: ['Marathi', 'मराठी'].freeze,
  111. ms: ['Malay', 'Bahasa Melayu'].freeze,
  112. mt: ['Maltese', 'Malti'].freeze,
  113. my: ['Burmese', 'ဗမာစာ'].freeze,
  114. na: ['Nauru', 'Ekakairũ Naoero'].freeze,
  115. nb: ['Norwegian Bokmål', 'Norsk bokmål'].freeze,
  116. nd: ['Northern Ndebele', 'isiNdebele'].freeze,
  117. ne: ['Nepali', 'नेपाली'].freeze,
  118. ng: ['Ndonga', 'Owambo'].freeze,
  119. nl: ['Dutch', 'Nederlands'].freeze,
  120. nn: ['Norwegian Nynorsk', 'Norsk Nynorsk'].freeze,
  121. no: ['Norwegian', 'Norsk'].freeze,
  122. nr: ['Southern Ndebele', 'isiNdebele'].freeze,
  123. nv: ['Navajo', 'Diné bizaad'].freeze,
  124. ny: ['Chichewa', 'chiCheŵa'].freeze,
  125. oc: ['Occitan', 'occitan'].freeze,
  126. oj: ['Ojibwe', 'ᐊᓂᔑᓈᐯᒧᐎᓐ'].freeze,
  127. om: ['Oromo', 'Afaan Oromoo'].freeze,
  128. or: ['Oriya', 'ଓଡ଼ିଆ'].freeze,
  129. os: ['Ossetian', 'ирон æвзаг'].freeze,
  130. pa: ['Panjabi', 'ਪੰਜਾਬੀ'].freeze,
  131. pi: ['Pāli', 'पाऴि'].freeze,
  132. pl: ['Polish', 'Polski'].freeze,
  133. ps: ['Pashto', 'پښتو'].freeze,
  134. pt: ['Portuguese', 'Português'].freeze,
  135. qu: ['Quechua', 'Runa Simi'].freeze,
  136. rm: ['Romansh', 'rumantsch grischun'].freeze,
  137. rn: ['Kirundi', 'Ikirundi'].freeze,
  138. ro: ['Romanian', 'Română'].freeze,
  139. ru: ['Russian', 'Русский'].freeze,
  140. rw: ['Kinyarwanda', 'Ikinyarwanda'].freeze,
  141. sa: ['Sanskrit', 'संस्कृतम्'].freeze,
  142. sc: ['Sardinian', 'sardu'].freeze,
  143. sd: ['Sindhi', 'सिन्धी'].freeze,
  144. se: ['Northern Sami', 'Davvisámegiella'].freeze,
  145. sg: ['Sango', 'yângâ tî sängö'].freeze,
  146. si: ['Sinhala', 'සිංහල'].freeze,
  147. sk: ['Slovak', 'slovenčina'].freeze,
  148. sl: ['Slovenian', 'slovenščina'].freeze,
  149. sn: ['Shona', 'chiShona'].freeze,
  150. so: ['Somali', 'Soomaaliga'].freeze,
  151. sq: ['Albanian', 'Shqip'].freeze,
  152. sr: ['Serbian', 'српски језик'].freeze,
  153. ss: ['Swati', 'SiSwati'].freeze,
  154. st: ['Southern Sotho', 'Sesotho'].freeze,
  155. su: ['Sundanese', 'Basa Sunda'].freeze,
  156. sv: ['Swedish', 'Svenska'].freeze,
  157. sw: ['Swahili', 'Kiswahili'].freeze,
  158. ta: ['Tamil', 'தமிழ்'].freeze,
  159. te: ['Telugu', 'తెలుగు'].freeze,
  160. tg: ['Tajik', 'тоҷикӣ'].freeze,
  161. th: ['Thai', 'ไทย'].freeze,
  162. ti: ['Tigrinya', 'ትግርኛ'].freeze,
  163. tk: ['Turkmen', 'Türkmen'].freeze,
  164. tl: ['Tagalog', 'Wikang Tagalog'].freeze,
  165. tn: ['Tswana', 'Setswana'].freeze,
  166. to: ['Tonga', 'faka Tonga'].freeze,
  167. tr: ['Turkish', 'Türkçe'].freeze,
  168. ts: ['Tsonga', 'Xitsonga'].freeze,
  169. tt: ['Tatar', 'татар теле'].freeze,
  170. tw: ['Twi', 'Twi'].freeze,
  171. ty: ['Tahitian', 'Reo Tahiti'].freeze,
  172. ug: ['Uyghur', 'ئۇيغۇرچە‎'].freeze,
  173. uk: ['Ukrainian', 'Українська'].freeze,
  174. ur: ['Urdu', 'اردو'].freeze,
  175. uz: ['Uzbek', 'Ўзбек'].freeze,
  176. ve: ['Venda', 'Tshivenḓa'].freeze,
  177. vi: ['Vietnamese', 'Tiếng Việt'].freeze,
  178. vo: ['Volapük', 'Volapük'].freeze,
  179. wa: ['Walloon', 'walon'].freeze,
  180. wo: ['Wolof', 'Wollof'].freeze,
  181. xh: ['Xhosa', 'isiXhosa'].freeze,
  182. yi: ['Yiddish', 'ייִדיש'].freeze,
  183. yo: ['Yoruba', 'Yorùbá'].freeze,
  184. za: ['Zhuang', 'Saɯ cueŋƅ'].freeze,
  185. zh: ['Chinese', '中文'].freeze,
  186. zu: ['Zulu', 'isiZulu'].freeze,
  187. }.freeze
  188. ISO_639_3 = {
  189. ast: ['Asturian', 'Asturianu'].freeze,
  190. ckb: ['Sorani (Kurdish)', 'سۆرانی'].freeze,
  191. cnr: ['Montenegrin', 'crnogorski'].freeze,
  192. jbo: ['Lojban', 'la .lojban.'].freeze,
  193. kab: ['Kabyle', 'Taqbaylit'].freeze,
  194. kmr: ['Kurmanji (Kurdish)', 'Kurmancî'].freeze,
  195. ldn: ['Láadan', 'Láadan'].freeze,
  196. lfn: ['Lingua Franca Nova', 'lingua franca nova'].freeze,
  197. sco: ['Scots', 'Scots'].freeze,
  198. sma: ['Southern Sami', 'Åarjelsaemien Gïele'].freeze,
  199. smj: ['Lule Sami', 'Julevsámegiella'].freeze,
  200. tok: ['Toki Pona', 'toki pona'].freeze,
  201. zba: ['Balaibalan', 'باليبلن'].freeze,
  202. zgh: ['Standard Moroccan Tamazight', 'ⵜⴰⵎⴰⵣⵉⵖⵜ'].freeze,
  203. }.freeze
  204. SUPPORTED_LOCALES = {}.merge(ISO_639_1).merge(ISO_639_3).freeze
  205. # For ISO-639-1 and ISO-639-3 language codes, we have their official
  206. # names, but for some translations, we need the names of the
  207. # regional variants specifically
  208. REGIONAL_LOCALE_NAMES = {
  209. 'es-AR': 'Español (Argentina)',
  210. 'es-MX': 'Español (México)',
  211. 'pt-BR': 'Português (Brasil)',
  212. 'pt-PT': 'Português (Portugal)',
  213. 'sr-Latn': 'Srpski (latinica)',
  214. 'zh-CN': '简体中文',
  215. 'zh-HK': '繁體中文(香港)',
  216. 'zh-TW': '繁體中文(臺灣)',
  217. }.freeze
  218. def native_locale_name(locale)
  219. if locale.blank? || locale == 'und'
  220. I18n.t('generic.none')
  221. elsif (supported_locale = SUPPORTED_LOCALES[locale.to_sym])
  222. supported_locale[1]
  223. elsif (regional_locale = REGIONAL_LOCALE_NAMES[locale.to_sym])
  224. regional_locale
  225. else
  226. locale
  227. end
  228. end
  229. def standard_locale_name(locale)
  230. if locale.blank?
  231. I18n.t('generic.none')
  232. elsif (supported_locale = SUPPORTED_LOCALES[locale.to_sym])
  233. supported_locale[0]
  234. else
  235. locale
  236. end
  237. end
  238. def valid_locale_or_nil(str)
  239. return if str.blank?
  240. code, = str.to_s.split(/[_-]/) # Strip out the region from e.g. en_US or ja-JP
  241. return unless valid_locale?(code)
  242. code
  243. end
  244. def valid_locale_cascade(*arr)
  245. arr.each do |str|
  246. locale = valid_locale_or_nil(str)
  247. return locale if locale.present?
  248. end
  249. nil
  250. end
  251. def valid_locale?(locale)
  252. locale.present? && SUPPORTED_LOCALES.key?(locale.to_sym)
  253. end
  254. def available_locale_or_nil(locale_name)
  255. locale_name.to_sym if locale_name.present? && I18n.available_locales.include?(locale_name.to_sym)
  256. end
  257. end
  258. # rubocop:enable Metrics/ModuleLength, Style/WordArray