]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/core-utils/i18n/i18n.ts
Add is locale support
[github/Chocobozzz/PeerTube.git] / shared / core-utils / i18n / i18n.ts
1 export const LOCALE_FILES = [ 'player', 'server' ]
2
3 export const I18N_LOCALES = {
4 // Always first to avoid issues when using express acceptLanguages function when no accept language header is set
5 'en-US': 'English',
6
7 'ar': 'العربية',
8 'ca-ES': 'Català',
9 'cs-CZ': 'Čeština',
10 'de-DE': 'Deutsch',
11 'el-GR': 'ελληνικά',
12 'eo': 'Esperanto',
13 'es-ES': 'Español',
14 'eu-ES': 'Euskara',
15 'fi-FI': 'suomi',
16 'fr-FR': 'Français',
17 'gd': 'Gàidhlig',
18 'gl-ES': 'galego',
19 'hr': 'hrvatski',
20 'hu-HU': 'magyar',
21 'fa-IR': 'فارسی',
22 'it-IT': 'Italiano',
23 'ja-JP': '日本語',
24 'kab': 'Taqbaylit',
25 'is': 'Íslenska',
26 'nl-NL': 'Nederlands',
27 'oc': 'Occitan',
28 'pl-PL': 'Polski',
29 'pt-BR': 'Português (Brasil)',
30 'pt-PT': 'Português (Portugal)',
31 'ru-RU': 'русский',
32 'sq': 'Shqip',
33 'sv-SE': 'Svenska',
34 'nn': 'norsk nynorsk',
35 'nb-NO': 'norsk bokmål',
36 'th-TH': 'ไทย',
37 'vi-VN': 'Tiếng Việt',
38 'tok': 'Toki Pona',
39 'zh-Hans-CN': '简体中文(中国)',
40 'zh-Hant-TW': '繁體中文(台灣)'
41 }
42
43 const I18N_LOCALE_ALIAS = {
44 'ar-001': 'ar',
45 'ca': 'ca-ES',
46 'cs': 'cs-CZ',
47 'de': 'de-DE',
48 'el': 'el-GR',
49 'en': 'en-US',
50 'es': 'es-ES',
51 'eu': 'eu-ES',
52 'fi': 'fi-FI',
53 'gl': 'gl-ES',
54 'fa': 'fa-IR',
55 'fr': 'fr-FR',
56 'hu': 'hu-HU',
57 'it': 'it-IT',
58 'ja': 'ja-JP',
59 'nl': 'nl-NL',
60 'pl': 'pl-PL',
61 'pt': 'pt-BR',
62 'nb': 'nb-NO',
63 'ru': 'ru-RU',
64 'sv': 'sv-SE',
65 'th': 'th-TH',
66 'vi': 'vi-VN',
67 'zh-CN': 'zh-Hans-CN',
68 'zh-Hans': 'zh-Hans-CN',
69 'zh-Hant': 'zh-Hant-TW',
70 'zh-TW': 'zh-Hant-TW',
71 'zh': 'zh-Hans-CN'
72 }
73
74 export const POSSIBLE_LOCALES = Object.keys(I18N_LOCALES)
75 .concat(Object.keys(I18N_LOCALE_ALIAS))
76
77 export function getDefaultLocale () {
78 return 'en-US'
79 }
80
81 export function isDefaultLocale (locale: string) {
82 return getCompleteLocale(locale) === getCompleteLocale(getDefaultLocale())
83 }
84
85 export function peertubeTranslate (str: string, translations?: { [ id: string ]: string }) {
86 if (!translations?.[str]) return str
87
88 return translations[str]
89 }
90
91 const possiblePaths = POSSIBLE_LOCALES.map(l => '/' + l)
92 export function is18nPath (path: string) {
93 return possiblePaths.includes(path)
94 }
95
96 export function is18nLocale (locale: string) {
97 return POSSIBLE_LOCALES.includes(locale)
98 }
99
100 export function getCompleteLocale (locale: string) {
101 if (!locale) return locale
102
103 if (I18N_LOCALE_ALIAS[locale]) return I18N_LOCALE_ALIAS[locale]
104
105 return locale
106 }
107
108 export function getShortLocale (locale: string) {
109 if (locale.includes('-') === false) return locale
110
111 return locale.split('-')[0]
112 }
113
114 export function buildFileLocale (locale: string) {
115 return getCompleteLocale(locale)
116 }