1 import { logger } from '@root-helpers/logger'
2 import { getCompleteLocale, getShortLocale, is18nLocale, isDefaultLocale } from '@shared/core-utils/i18n'
4 export class TranslationsManager {
5 private static videojsLocaleCache: { [ path: string ]: any } = {}
7 static getServerTranslations (serverUrl: string, locale: string): Promise<{ [id: string]: string }> {
8 const path = TranslationsManager.getLocalePath(serverUrl, locale)
9 // It is the default locale, nothing to translate
10 if (!path) return Promise.resolve(undefined)
12 return fetch(path + '/server.json')
13 .then(res => res.json())
15 logger.error('Cannot get server translations', err)
20 static loadLocaleInVideoJS (serverUrl: string, locale: string, videojs: any) {
21 const path = TranslationsManager.getLocalePath(serverUrl, locale)
22 // It is the default locale, nothing to translate
23 if (!path) return Promise.resolve(undefined)
27 if (TranslationsManager.videojsLocaleCache[path]) {
28 p = Promise.resolve(TranslationsManager.videojsLocaleCache[path])
30 p = fetch(path + '/player.json')
31 .then(res => res.json())
33 TranslationsManager.videojsLocaleCache[path] = json
37 logger.error('Cannot get player translations', err)
42 const completeLocale = getCompleteLocale(locale)
43 return p.then(json => videojs.addLanguage(getShortLocale(completeLocale), json))
46 private static getLocalePath (serverUrl: string, locale: string) {
47 const completeLocale = getCompleteLocale(locale)
49 if (!is18nLocale(completeLocale) || isDefaultLocale(completeLocale)) return undefined
51 return serverUrl + '/client/locales/' + completeLocale