aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/helpers/i18n-utils.ts
blob: 2017a31ea3dc253feeddf9823dab95dc4fa1f74d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { environment } from '../../environments/environment'
import IntlMessageFormat from 'intl-messageformat'

function isOnDevLocale () {
  return environment.production === false && window.location.search === '?lang=fr'
}

function getDevLocale () {
  return 'fr-FR'
}

function prepareIcu (icu: string) {
  let alreadyWarned = false

  try {
    const msg = new IntlMessageFormat(icu, $localize.locale)

    return (context: { [id: string]: number | string }, fallback: string) => {
      try {
        return msg.format(context) as string
      } catch (err) {
        if (!alreadyWarned) console.warn('Cannot format ICU %s.', icu, err)

        alreadyWarned = true
        return fallback
      }
    }
  } catch (err) {
    console.warn('Cannot build intl message %s.', icu, err)

    return (_context: unknown, fallback: string) => fallback
  }
}

export {
  getDevLocale,
  prepareIcu,
  isOnDevLocale
}