i18n.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. export const LOCALE_FILES = [ 'player', 'server' ]
  2. export const I18N_LOCALES = {
  3. // Always first to avoid issues when using express acceptLanguages function when no accept language header is set
  4. 'en-US': 'English',
  5. 'ca-ES': 'Català',
  6. 'cs-CZ': 'Čeština',
  7. 'de-DE': 'Deutsch',
  8. 'el-GR': 'ελληνικά',
  9. 'eo': 'Esperanto',
  10. 'es-ES': 'Español',
  11. 'eu-ES': 'Euskara',
  12. 'fi-FI': 'suomi',
  13. 'fr-FR': 'Français',
  14. 'gd': 'Gàidhlig',
  15. 'hu-HU': 'magyar',
  16. 'it-IT': 'Italiano',
  17. 'ja-JP': '日本語',
  18. 'nl-NL': 'Nederlands',
  19. 'pl-PL': 'Polski',
  20. 'pt-BR': 'Português (Brasil)',
  21. 'pt-PT': 'Português (Portugal)',
  22. 'ru-RU': 'русский',
  23. 'sv-SE': 'svenska',
  24. 'th-TH': 'ไทย',
  25. 'zh-Hans-CN': '简体中文(中国)',
  26. 'zh-Hant-TW': '繁體中文(台灣)'
  27. }
  28. const I18N_LOCALE_ALIAS = {
  29. 'ca': 'ca-ES',
  30. 'cs': 'cs-CZ',
  31. 'de': 'de-DE',
  32. 'el': 'el-GR',
  33. 'en': 'en-US',
  34. 'es': 'es-ES',
  35. 'eu': 'eu-ES',
  36. 'fi': 'fi-FI',
  37. 'fr': 'fr-FR',
  38. 'ja': 'ja-JP',
  39. 'it': 'it-IT',
  40. 'hu': 'hu-HU',
  41. 'nl': 'nl-NL',
  42. 'pl': 'pl-PL',
  43. 'pt': 'pt-BR',
  44. 'ru': 'ru-RU',
  45. 'sv': 'sv-SE',
  46. 'th': 'th-TH',
  47. 'zh': 'zh-Hans-CN',
  48. 'zh-Hans': 'zh-Hans-CN',
  49. 'zh-CN': 'zh-Hans-CN',
  50. 'zh-Hant': 'zh-Hant-TW',
  51. 'zh-TW': 'zh-Hant-TW'
  52. }
  53. export const POSSIBLE_LOCALES = Object.keys(I18N_LOCALES)
  54. .concat(Object.keys(I18N_LOCALE_ALIAS))
  55. export function getDefaultLocale () {
  56. return 'en-US'
  57. }
  58. export function isDefaultLocale (locale: string) {
  59. return getCompleteLocale(locale) === getCompleteLocale(getDefaultLocale())
  60. }
  61. export function peertubeTranslate (str: string, translations?: { [ id: string ]: string }) {
  62. // FIXME: remove disable rule when the client is upgraded to typescript 3.7
  63. // eslint-disable-next-line
  64. return translations && translations[str] ? translations[str] : str
  65. }
  66. const possiblePaths = POSSIBLE_LOCALES.map(l => '/' + l)
  67. export function is18nPath (path: string) {
  68. return possiblePaths.includes(path)
  69. }
  70. export function is18nLocale (locale: string) {
  71. return POSSIBLE_LOCALES.includes(locale)
  72. }
  73. export function getCompleteLocale (locale: string) {
  74. if (!locale) return locale
  75. if (I18N_LOCALE_ALIAS[locale]) return I18N_LOCALE_ALIAS[locale]
  76. return locale
  77. }
  78. export function getShortLocale (locale: string) {
  79. if (locale.includes('-') === false) return locale
  80. return locale.split('-')[0]
  81. }
  82. export function buildFileLocale (locale: string) {
  83. return getCompleteLocale(locale)
  84. }