2021-05-28 19:03:53 +03:00
|
|
|
import React, { useEffect } from 'react'
|
2021-05-26 23:34:52 +03:00
|
|
|
import { i18n } from '@lingui/core'
|
|
|
|
import { I18nProvider } from '@lingui/react'
|
2021-05-28 19:03:53 +03:00
|
|
|
import { ReactNode } from 'react'
|
2021-05-29 15:14:01 +03:00
|
|
|
import { useActiveLocale, useSetLocaleFromUrl } from 'hooks/useActiveLocale'
|
2021-05-29 03:36:37 +03:00
|
|
|
import { SupportedLocale } from 'constants/locales'
|
2021-05-26 23:34:52 +03:00
|
|
|
|
2021-05-28 19:03:53 +03:00
|
|
|
export async function dynamicActivate(locale: SupportedLocale) {
|
2021-05-29 07:04:37 +03:00
|
|
|
const { messages } = await import(`@lingui/loader!./locales/${locale}.po`)
|
|
|
|
i18n.loadLocaleData(locale, { plurals: () => null })
|
|
|
|
i18n.load(locale, messages)
|
|
|
|
i18n.activate(locale)
|
2021-05-26 23:34:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function LanguageProvider({ children }: { children: ReactNode }) {
|
2021-05-29 15:14:01 +03:00
|
|
|
useSetLocaleFromUrl()
|
2021-05-29 03:36:37 +03:00
|
|
|
const locale = useActiveLocale()
|
2021-05-28 19:03:53 +03:00
|
|
|
|
2021-05-26 23:34:52 +03:00
|
|
|
useEffect(() => {
|
2021-05-29 07:04:37 +03:00
|
|
|
dynamicActivate(locale).catch((error) => {
|
|
|
|
console.error('Failed to activate locale', locale, error)
|
|
|
|
})
|
2021-05-29 03:36:37 +03:00
|
|
|
}, [locale])
|
2021-05-26 23:34:52 +03:00
|
|
|
|
2021-05-29 08:07:42 +03:00
|
|
|
return (
|
|
|
|
<I18nProvider forceRenderOnLocaleChange={false} i18n={i18n}>
|
|
|
|
{children}
|
|
|
|
</I18nProvider>
|
|
|
|
)
|
2021-05-26 23:34:52 +03:00
|
|
|
}
|