1 import { getCompleteLocale, getShortLocale, is18nLocale, isDefaultLocale } from '@shared/core-utils/i18n'
3 export class TranslationsManager {
4 private static videojsLocaleCache: { [ path: string ]: any } = {}
6 static getServerTranslations (serverUrl: string, locale: string): Promise<{ [id: string]: string }> {
7 const path = TranslationsManager.getLocalePath(serverUrl, locale)
8 // It is the default locale, nothing to translate
9 if (!path) return Promise.resolve(undefined)
11 return fetch(path + '/server.json')
12 .then(res => res.json())
14 console.error('Cannot get server translations', err)
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)
26 if (TranslationsManager.videojsLocaleCache[ path ]) {
27 p = Promise.resolve(TranslationsManager.videojsLocaleCache[ path ])
29 p = fetch(path + '/player.json')
30 .then(res => res.json())
32 TranslationsManager.videojsLocaleCache[ path ] = json
36 console.error('Cannot get player translations', err)
41 const completeLocale = getCompleteLocale(locale)
42 return p.then(json => videojs.addLanguage(getShortLocale(completeLocale), json))
45 private static getLocalePath (serverUrl: string, locale: string) {
46 const completeLocale = getCompleteLocale(locale)
48 if (!is18nLocale(completeLocale) || isDefaultLocale(completeLocale)) return undefined
50 return serverUrl + '/client/locales/' + completeLocale