diff options
author | Chocobozzz <me@florianbigard.com> | 2022-05-24 16:29:01 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-06-08 13:40:40 +0200 |
commit | eaa529528cafcfb291009f9f99d296c81e792899 (patch) | |
tree | c8e3562f73312fb331a363e1daeaeb4949cc8448 /client/src/app/helpers/i18n-utils.ts | |
parent | e435cf44c00aba359bf0f265d06bff4841b3f7fe (diff) | |
download | PeerTube-eaa529528cafcfb291009f9f99d296c81e792899.tar.gz PeerTube-eaa529528cafcfb291009f9f99d296c81e792899.tar.zst PeerTube-eaa529528cafcfb291009f9f99d296c81e792899.zip |
Support ICU in TS components
Diffstat (limited to 'client/src/app/helpers/i18n-utils.ts')
-rw-r--r-- | client/src/app/helpers/i18n-utils.ts | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/client/src/app/helpers/i18n-utils.ts b/client/src/app/helpers/i18n-utils.ts index bbfb12959..2017a31ea 100644 --- a/client/src/app/helpers/i18n-utils.ts +++ b/client/src/app/helpers/i18n-utils.ts | |||
@@ -1,4 +1,5 @@ | |||
1 | import { environment } from '../../environments/environment' | 1 | import { environment } from '../../environments/environment' |
2 | import IntlMessageFormat from 'intl-messageformat' | ||
2 | 3 | ||
3 | function isOnDevLocale () { | 4 | function isOnDevLocale () { |
4 | return environment.production === false && window.location.search === '?lang=fr' | 5 | return environment.production === false && window.location.search === '?lang=fr' |
@@ -8,7 +9,31 @@ function getDevLocale () { | |||
8 | return 'fr-FR' | 9 | return 'fr-FR' |
9 | } | 10 | } |
10 | 11 | ||
12 | function prepareIcu (icu: string) { | ||
13 | let alreadyWarned = false | ||
14 | |||
15 | try { | ||
16 | const msg = new IntlMessageFormat(icu, $localize.locale) | ||
17 | |||
18 | return (context: { [id: string]: number | string }, fallback: string) => { | ||
19 | try { | ||
20 | return msg.format(context) as string | ||
21 | } catch (err) { | ||
22 | if (!alreadyWarned) console.warn('Cannot format ICU %s.', icu, err) | ||
23 | |||
24 | alreadyWarned = true | ||
25 | return fallback | ||
26 | } | ||
27 | } | ||
28 | } catch (err) { | ||
29 | console.warn('Cannot build intl message %s.', icu, err) | ||
30 | |||
31 | return (_context: unknown, fallback: string) => fallback | ||
32 | } | ||
33 | } | ||
34 | |||
11 | export { | 35 | export { |
12 | getDevLocale, | 36 | getDevLocale, |
37 | prepareIcu, | ||
13 | isOnDevLocale | 38 | isOnDevLocale |
14 | } | 39 | } |