languages_helper.rb 11 KB

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