]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/translations-manager.ts
Reduce auto play error log
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / translations-manager.ts
CommitLineData
42b40636 1import { logger } from '@root-helpers/logger'
bd45d503 2import { getCompleteLocale, getShortLocale, is18nLocale, isDefaultLocale } from '@shared/core-utils/i18n'
3f9c4955
C
3
4export class TranslationsManager {
5 private static videojsLocaleCache: { [ path: string ]: any } = {}
6
5abc96fc 7 static getServerTranslations (serverUrl: string, locale: string): Promise<{ [id: string]: string }> {
3f9c4955
C
8 const path = TranslationsManager.getLocalePath(serverUrl, locale)
9 // It is the default locale, nothing to translate
10 if (!path) return Promise.resolve(undefined)
11
12 return fetch(path + '/server.json')
13 .then(res => res.json())
14 .catch(err => {
42b40636 15 logger.error('Cannot get server translations', err)
3f9c4955
C
16 return undefined
17 })
18 }
19
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)
24
25 let p: Promise<any>
26
9df52d66
C
27 if (TranslationsManager.videojsLocaleCache[path]) {
28 p = Promise.resolve(TranslationsManager.videojsLocaleCache[path])
3f9c4955
C
29 } else {
30 p = fetch(path + '/player.json')
31 .then(res => res.json())
32 .then(json => {
9df52d66 33 TranslationsManager.videojsLocaleCache[path] = json
3f9c4955
C
34 return json
35 })
36 .catch(err => {
42b40636 37 logger.error('Cannot get player translations', err)
3f9c4955
C
38 return undefined
39 })
40 }
41
42 const completeLocale = getCompleteLocale(locale)
43 return p.then(json => videojs.addLanguage(getShortLocale(completeLocale), json))
44 }
45
46 private static getLocalePath (serverUrl: string, locale: string) {
47 const completeLocale = getCompleteLocale(locale)
48
49 if (!is18nLocale(completeLocale) || isDefaultLocale(completeLocale)) return undefined
50
51 return serverUrl + '/client/locales/' + completeLocale
52 }
53}