diff options
Diffstat (limited to 'client/src/app/helpers')
-rw-r--r-- | client/src/app/helpers/i18n-utils.ts | 69 |
1 files changed, 54 insertions, 15 deletions
diff --git a/client/src/app/helpers/i18n-utils.ts b/client/src/app/helpers/i18n-utils.ts index b7d73d16b..9e22bb4c1 100644 --- a/client/src/app/helpers/i18n-utils.ts +++ b/client/src/app/helpers/i18n-utils.ts | |||
@@ -1,4 +1,6 @@ | |||
1 | import IntlMessageFormat from 'intl-messageformat' | 1 | import IntlMessageFormat from 'intl-messageformat' |
2 | import { shouldPolyfill as shouldPolyfillLocale } from '@formatjs/intl-locale/should-polyfill' | ||
3 | import { shouldPolyfill as shouldPolyfillPlural } from '@formatjs/intl-pluralrules/should-polyfill' | ||
2 | import { logger } from '@root-helpers/logger' | 4 | import { logger } from '@root-helpers/logger' |
3 | import { environment } from '../../environments/environment' | 5 | import { environment } from '../../environments/environment' |
4 | 6 | ||
@@ -10,31 +12,68 @@ function getDevLocale () { | |||
10 | return 'fr-FR' | 12 | return 'fr-FR' |
11 | } | 13 | } |
12 | 14 | ||
13 | function prepareIcu (icu: string) { | 15 | async function polyfillICU () { |
14 | let alreadyWarned = false | 16 | // Important to be in this order, Plural needs Locale (https://formatjs.io/docs/polyfills/intl-pluralrules) |
17 | await polyfillICULocale() | ||
18 | await polyfillICUPlural() | ||
19 | } | ||
15 | 20 | ||
16 | try { | 21 | async function polyfillICULocale () { |
17 | const msg = new IntlMessageFormat(icu, $localize.locale) | 22 | // This locale is supported |
23 | if (shouldPolyfillLocale()) { | ||
24 | // TODO: remove, it's only needed to support Plural polyfill and so iOS 12 | ||
25 | console.log('Loading Intl Locale polyfill for ' + $localize.locale) | ||
26 | |||
27 | await import('@formatjs/intl-locale/polyfill') | ||
28 | } | ||
29 | } | ||
30 | |||
31 | async function polyfillICUPlural () { | ||
32 | const unsupportedLocale = shouldPolyfillPlural($localize.locale) | ||
33 | |||
34 | // This locale is supported | ||
35 | if (!unsupportedLocale) { | ||
36 | return | ||
37 | } | ||
18 | 38 | ||
19 | return (context: { [id: string]: number | string }, fallback: string) => { | 39 | // TODO: remove, it's only needed to support iOS 12 |
20 | try { | 40 | console.log('Loading Intl Plural rules polyfill for ' + $localize.locale) |
21 | return msg.format(context) as string | ||
22 | } catch (err) { | ||
23 | if (!alreadyWarned) logger.warn(`Cannot format ICU ${icu}.`, err) | ||
24 | 41 | ||
25 | alreadyWarned = true | 42 | // Load the polyfill 1st BEFORE loading data |
26 | return fallback | 43 | await import('@formatjs/intl-pluralrules/polyfill-force') |
27 | } | 44 | // Degraded mode, so only load the en local data |
45 | await import(`@formatjs/intl-pluralrules/locale-data/en.js`) | ||
46 | } | ||
47 | |||
48 | // --------------------------------------------------------------------------- | ||
49 | |||
50 | const icuCache = new Map<string, IntlMessageFormat>() | ||
51 | const icuWarnings = new Set<string>() | ||
52 | const fallback = 'String translation error' | ||
53 | |||
54 | function formatICU (icu: string, context: { [id: string]: number | string }) { | ||
55 | try { | ||
56 | let msg = icuCache.get(icu) | ||
57 | |||
58 | if (!msg) { | ||
59 | msg = new IntlMessageFormat(icu, $localize.locale) | ||
60 | icuCache.set(icu, msg) | ||
28 | } | 61 | } |
62 | |||
63 | return msg.format(context) as string | ||
29 | } catch (err) { | 64 | } catch (err) { |
30 | logger.warn(`Cannot build intl message ${icu}.`, err) | 65 | if (!icuWarnings.has(icu)) { |
66 | logger.warn(`Cannot format ICU ${icu}.`, err) | ||
67 | } | ||
31 | 68 | ||
32 | return (_context: unknown, fallback: string) => fallback | 69 | icuWarnings.add(icu) |
70 | return fallback | ||
33 | } | 71 | } |
34 | } | 72 | } |
35 | 73 | ||
36 | export { | 74 | export { |
37 | getDevLocale, | 75 | getDevLocale, |
38 | prepareIcu, | 76 | polyfillICU, |
77 | formatICU, | ||
39 | isOnDevLocale | 78 | isOnDevLocale |
40 | } | 79 | } |