]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/helpers/i18n-utils.ts
b7d73d16bda40e3aa9b8d28d7bfd52fdc05ca6cd
[github/Chocobozzz/PeerTube.git] / client / src / app / helpers / i18n-utils.ts
1 import IntlMessageFormat from 'intl-messageformat'
2 import { logger } from '@root-helpers/logger'
3 import { environment } from '../../environments/environment'
4
5 function isOnDevLocale () {
6 return environment.production === false && window.location.search === '?lang=fr'
7 }
8
9 function getDevLocale () {
10 return 'fr-FR'
11 }
12
13 function prepareIcu (icu: string) {
14 let alreadyWarned = false
15
16 try {
17 const msg = new IntlMessageFormat(icu, $localize.locale)
18
19 return (context: { [id: string]: number | string }, fallback: string) => {
20 try {
21 return msg.format(context) as string
22 } catch (err) {
23 if (!alreadyWarned) logger.warn(`Cannot format ICU ${icu}.`, err)
24
25 alreadyWarned = true
26 return fallback
27 }
28 }
29 } catch (err) {
30 logger.warn(`Cannot build intl message ${icu}.`, err)
31
32 return (_context: unknown, fallback: string) => fallback
33 }
34 }
35
36 export {
37 getDevLocale,
38 prepareIcu,
39 isOnDevLocale
40 }