2021-05-26 23:34:52 +03:00
|
|
|
import React from 'react'
|
|
|
|
import { i18n } from '@lingui/core'
|
|
|
|
import { I18nProvider } from '@lingui/react'
|
|
|
|
import { detect, fromUrl } from '@lingui/detect-locale'
|
|
|
|
import { ReactNode, useEffect } from 'react'
|
|
|
|
|
2021-05-27 18:52:17 +03:00
|
|
|
export const locales = ['en', 'pseudo-en', 'de', 'es-AR', 'es-US', 'it-IT', 'iw', 'ro', 'ru', 'vi', 'zh-CN', 'zh-TW']
|
2021-05-26 23:34:52 +03:00
|
|
|
export const defaultLocale = 'en'
|
|
|
|
|
|
|
|
const getDetectedLocale = () => {
|
|
|
|
const detected =
|
|
|
|
detect(
|
|
|
|
fromUrl('lang'), // helps local development
|
|
|
|
defaultLocale
|
|
|
|
) ?? defaultLocale
|
|
|
|
return locales.includes(detected) ? detected : defaultLocale
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function dynamicActivate(locale: string) {
|
2021-05-27 18:52:17 +03:00
|
|
|
const { messages } = await import(`@lingui/loader!./locales/${locale}.po`)
|
2021-05-27 19:38:37 +03:00
|
|
|
i18n.loadLocaleData(locale, { plurals: () => null })
|
2021-05-26 23:34:52 +03:00
|
|
|
i18n.load(locale, messages)
|
|
|
|
i18n.activate(locale)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function LanguageProvider({ children }: { children: ReactNode }) {
|
|
|
|
useEffect(() => {
|
2021-05-27 18:52:17 +03:00
|
|
|
dynamicActivate(getDetectedLocale()).catch((error) => {
|
|
|
|
console.error('Failed to load locale data', error)
|
|
|
|
})
|
2021-05-26 23:34:52 +03:00
|
|
|
}, [])
|
|
|
|
|
|
|
|
return <I18nProvider i18n={i18n}>{children}</I18nProvider>
|
|
|
|
}
|