languages_helper.rb 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. ckb: ['Sorani (Kurdish)', 'سۆرانی'].freeze,
  190. kab: ['Kabyle', 'Taqbaylit'].freeze,
  191. kmr: ['Kurmanji (Kurdish)', 'Kurmancî'].freeze,
  192. zgh: ['Standard Moroccan Tamazight', 'ⵜⴰⵎⴰⵣⵉⵖⵜ'].freeze,
  193. }.freeze
  194. SUPPORTED_LOCALES = {}.merge(ISO_639_1).merge(ISO_639_3).freeze
  195. # For ISO-639-1 and ISO-639-3 language codes, we have their official
  196. # names, but for some translations, we need the names of the
  197. # regional variants specifically
  198. REGIONAL_LOCALE_NAMES = {
  199. 'es-AR': 'Español (Argentina)',
  200. 'es-MX': 'Español (México)',
  201. 'pt-BR': 'Português (Brasil)',
  202. 'pt-PT': 'Português (Portugal)',
  203. 'sr-Latn': 'Srpski (latinica)',
  204. 'zh-CN': '简体中文',
  205. 'zh-HK': '繁體中文(香港)',
  206. 'zh-TW': '繁體中文(臺灣)',
  207. }.freeze
  208. def native_locale_name(locale)
  209. if locale.blank? || locale == 'und'
  210. I18n.t('generic.none')
  211. elsif (supported_locale = SUPPORTED_LOCALES[locale.to_sym])
  212. supported_locale[1]
  213. elsif (regional_locale = REGIONAL_LOCALE_NAMES[locale.to_sym])
  214. regional_locale
  215. else
  216. locale
  217. end
  218. end
  219. def standard_locale_name(locale)
  220. if locale.blank?
  221. I18n.t('generic.none')
  222. elsif (supported_locale = SUPPORTED_LOCALES[locale.to_sym])
  223. supported_locale[0]
  224. else
  225. locale
  226. end
  227. end
  228. def valid_locale_or_nil(str)
  229. return if str.blank?
  230. code, = str.to_s.split(/[_-]/) # Strip out the region from e.g. en_US or ja-JP
  231. return unless valid_locale?(code)
  232. code
  233. end
  234. def valid_locale_cascade(*arr)
  235. arr.each do |str|
  236. locale = valid_locale_or_nil(str)
  237. return locale if locale.present?
  238. end
  239. nil
  240. end
  241. def valid_locale?(locale)
  242. locale.present? && SUPPORTED_LOCALES.key?(locale.to_sym)
  243. end
  244. def available_locale_or_nil(locale_name)
  245. locale_name.to_sym if locale_name.present? && I18n.available_locales.include?(locale_name.to_sym)
  246. end
  247. end