about summary refs log tree commit diff
path: root/src/locale/i18n.web.ts
blob: 4e8b8d95d46c1cafb6014691657757d775ecbb14 (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
import {useEffect} from 'react'
import {i18n} from '@lingui/core'

import {useLanguagePrefs} from '#/state/preferences'

export const locales = {
  en: 'English',
  hi: 'हिंदी',
}
export const defaultLocale = 'en'

/**
 * We do a dynamic import of just the catalog that we need
 * @param locale any locale string
 */
export async function dynamicActivate(locale: string) {
  let mod: any

  if (locale === 'hi') {
    mod = await import(`./locales/hi/messages`)
  } else {
    mod = await import(`./locales/en/messages`)
  }

  i18n.load(locale, mod.messages)
  i18n.activate(locale)
}

export async function useLocaleLanguage() {
  const {appLanguage} = useLanguagePrefs()
  useEffect(() => {
    dynamicActivate(appLanguage)
  }, [appLanguage])
}