diff options
author | Chocobozzz <me@florianbigard.com> | 2018-06-06 17:37:13 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-06-06 17:37:13 +0200 |
commit | 74b7c6d48e9ca377fe938c8134ed74b612e62ba0 (patch) | |
tree | cb848dec054a71669fef5ea3986bdd0d2c354248 /shared/models | |
parent | 7ce44a74a3b052190cfacd4bd5ee6b92cfc620ac (diff) | |
download | PeerTube-74b7c6d48e9ca377fe938c8134ed74b612e62ba0.tar.gz PeerTube-74b7c6d48e9ca377fe938c8134ed74b612e62ba0.tar.zst PeerTube-74b7c6d48e9ca377fe938c8134ed74b612e62ba0.zip |
Little i18n refractoring
Diffstat (limited to 'shared/models')
-rw-r--r-- | shared/models/i18n/i18n.ts | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/shared/models/i18n/i18n.ts b/shared/models/i18n/i18n.ts index 4d50bc36e..be1420150 100644 --- a/shared/models/i18n/i18n.ts +++ b/shared/models/i18n/i18n.ts | |||
@@ -1,34 +1,45 @@ | |||
1 | export const LOCALE_FILES = [ 'player', 'server' ] | ||
2 | |||
1 | export const I18N_LOCALES = { | 3 | export const I18N_LOCALES = { |
2 | 'en-US': 'English (US)', | 4 | 'en-US': 'English (US)', |
3 | fr: 'French' | 5 | fr: 'French' |
4 | } | 6 | } |
5 | 7 | ||
8 | const I18N_LOCALE_ALIAS = { | ||
9 | 'en': 'en-US' | ||
10 | } | ||
11 | |||
12 | export const POSSIBLE_LOCALES = Object.keys(I18N_LOCALES) | ||
13 | .concat(Object.keys(I18N_LOCALE_ALIAS)) | ||
14 | |||
15 | const possiblePaths = POSSIBLE_LOCALES.map(l => '/' + l) | ||
16 | |||
6 | export function getDefaultLocale () { | 17 | export function getDefaultLocale () { |
7 | return 'en-US' | 18 | return 'en-US' |
8 | } | 19 | } |
9 | 20 | ||
10 | export function isDefaultLocale (locale: string) { | 21 | export function isDefaultLocale (locale: string) { |
11 | return locale === getDefaultLocale() | 22 | return getCompleteLocale(locale) === getCompleteLocale(getDefaultLocale()) |
12 | } | 23 | } |
13 | 24 | ||
14 | const possiblePaths = Object.keys(I18N_LOCALES).map(l => '/' + l) | ||
15 | export function is18nPath (path: string) { | 25 | export function is18nPath (path: string) { |
16 | return possiblePaths.indexOf(path) !== -1 | 26 | return possiblePaths.indexOf(path) !== -1 |
17 | } | 27 | } |
18 | 28 | ||
19 | const possibleLanguages = Object.keys(I18N_LOCALES) | ||
20 | export function is18nLocale (locale: string) { | 29 | export function is18nLocale (locale: string) { |
21 | return possibleLanguages.indexOf(locale) !== -1 | 30 | return POSSIBLE_LOCALES.indexOf(locale) !== -1 |
31 | } | ||
32 | |||
33 | export function getCompleteLocale (locale: string) { | ||
34 | if (!locale) return locale | ||
35 | |||
36 | if (I18N_LOCALE_ALIAS[locale]) return I18N_LOCALE_ALIAS[locale] | ||
37 | |||
38 | return locale | ||
22 | } | 39 | } |
23 | 40 | ||
24 | // Only use in dev mode, so relax | ||
25 | // In production, the locale always match with a I18N_LANGUAGES key | ||
26 | export function buildFileLocale (locale: string) { | 41 | export function buildFileLocale (locale: string) { |
27 | if (!is18nLocale(locale)) { | 42 | const completeLocale = getCompleteLocale(locale) |
28 | // Some working examples for development purpose | ||
29 | if (locale.split('-')[ 0 ] === 'en') return 'en_US' | ||
30 | else if (locale === 'fr') return 'fr' | ||
31 | } | ||
32 | 43 | ||
33 | return locale.replace('-', '_') | 44 | return completeLocale.replace('-', '_') |
34 | } | 45 | } |