PeerTube/client/src/assets/player/translations-manager.ts

53 lines
1.8 KiB
TypeScript
Raw Normal View History

2020-06-26 01:37:26 -05:00
import { getCompleteLocale, getShortLocale, is18nLocale, isDefaultLocale } from '@shared/models'
2019-12-17 04:20:24 -06:00
export class TranslationsManager {
private static videojsLocaleCache: { [ path: string ]: any } = {}
static getServerTranslations (serverUrl: string, locale: string) {
const path = TranslationsManager.getLocalePath(serverUrl, locale)
// It is the default locale, nothing to translate
if (!path) return Promise.resolve(undefined)
return fetch(path + '/server.json')
.then(res => res.json())
.catch(err => {
console.error('Cannot get server translations', err)
return undefined
})
}
static loadLocaleInVideoJS (serverUrl: string, locale: string, videojs: any) {
const path = TranslationsManager.getLocalePath(serverUrl, locale)
// It is the default locale, nothing to translate
if (!path) return Promise.resolve(undefined)
let p: Promise<any>
if (TranslationsManager.videojsLocaleCache[ path ]) {
p = Promise.resolve(TranslationsManager.videojsLocaleCache[ path ])
} else {
p = fetch(path + '/player.json')
.then(res => res.json())
.then(json => {
TranslationsManager.videojsLocaleCache[ path ] = json
return json
})
.catch(err => {
console.error('Cannot get player translations', err)
return undefined
})
}
const completeLocale = getCompleteLocale(locale)
return p.then(json => videojs.addLanguage(getShortLocale(completeLocale), json))
}
private static getLocalePath (serverUrl: string, locale: string) {
const completeLocale = getCompleteLocale(locale)
if (!is18nLocale(completeLocale) || isDefaultLocale(completeLocale)) return undefined
return serverUrl + '/client/locales/' + completeLocale
}
}