aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/translations-manager.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/assets/player/translations-manager.ts')
-rw-r--r--client/src/assets/player/translations-manager.ts52
1 files changed, 52 insertions, 0 deletions
diff --git a/client/src/assets/player/translations-manager.ts b/client/src/assets/player/translations-manager.ts
new file mode 100644
index 000000000..e9f300ce7
--- /dev/null
+++ b/client/src/assets/player/translations-manager.ts
@@ -0,0 +1,52 @@
1import { getCompleteLocale, getShortLocale, is18nLocale, isDefaultLocale } from '../../../../shared/models'
2
3export class TranslationsManager {
4 private static videojsLocaleCache: { [ path: string ]: any } = {}
5
6 static getServerTranslations (serverUrl: string, locale: string) {
7 const path = TranslationsManager.getLocalePath(serverUrl, locale)
8 // It is the default locale, nothing to translate
9 if (!path) return Promise.resolve(undefined)
10
11 return fetch(path + '/server.json')
12 .then(res => res.json())
13 .catch(err => {
14 console.error('Cannot get server translations', err)
15 return undefined
16 })
17 }
18
19 static loadLocaleInVideoJS (serverUrl: string, locale: string, videojs: any) {
20 const path = TranslationsManager.getLocalePath(serverUrl, locale)
21 // It is the default locale, nothing to translate
22 if (!path) return Promise.resolve(undefined)
23
24 let p: Promise<any>
25
26 if (TranslationsManager.videojsLocaleCache[ path ]) {
27 p = Promise.resolve(TranslationsManager.videojsLocaleCache[ path ])
28 } else {
29 p = fetch(path + '/player.json')
30 .then(res => res.json())
31 .then(json => {
32 TranslationsManager.videojsLocaleCache[ path ] = json
33 return json
34 })
35 .catch(err => {
36 console.error('Cannot get player translations', err)
37 return undefined
38 })
39 }
40
41 const completeLocale = getCompleteLocale(locale)
42 return p.then(json => videojs.addLanguage(getShortLocale(completeLocale), json))
43 }
44
45 private static getLocalePath (serverUrl: string, locale: string) {
46 const completeLocale = getCompleteLocale(locale)
47
48 if (!is18nLocale(completeLocale) || isDefaultLocale(completeLocale)) return undefined
49
50 return serverUrl + '/client/locales/' + completeLocale
51 }
52}