Commit 65e58a08 authored by Zach Pomerantz's avatar Zach Pomerantz Committed by GitHub

fix: show i18n keys while messages load (#3683)

* fix: show i18n keys while messages load

* fix: i18n initialization check
parent 71b20b43
import { i18n } from '@lingui/core' import { i18n } from '@lingui/core'
import { I18nProvider } from '@lingui/react' import { I18nProvider } from '@lingui/react'
import { SupportedLocale } from 'constants/locales' import { DEFAULT_LOCALE, SupportedLocale } from 'constants/locales'
import { import {
af, af,
ar, ar,
...@@ -79,8 +79,6 @@ const plurals: LocalePlural = { ...@@ -79,8 +79,6 @@ const plurals: LocalePlural = {
export async function dynamicActivate(locale: SupportedLocale) { export async function dynamicActivate(locale: SupportedLocale) {
i18n.loadLocaleData(locale, { plurals: () => plurals[locale] }) i18n.loadLocaleData(locale, { plurals: () => plurals[locale] })
try { try {
// There are no default messages in production,
// see https://github.com/lingui/js-lingui/issues/388#issuecomment-497779030
const catalog = await import(`${process.env.REACT_APP_LOCALES}/${locale}.js`) const catalog = await import(`${process.env.REACT_APP_LOCALES}/${locale}.js`)
// Bundlers will either export it as default or as a named export named default. // Bundlers will either export it as default or as a named export named default.
i18n.load(locale, catalog.messages || catalog.default.messages) i18n.load(locale, catalog.messages || catalog.default.messages)
...@@ -104,6 +102,16 @@ export function Provider({ locale, forceRenderAfterLocaleChange = true, onActiva ...@@ -104,6 +102,16 @@ export function Provider({ locale, forceRenderAfterLocaleChange = true, onActiva
}) })
}, [locale, onActivate]) }, [locale, onActivate])
// Initialize the locale immediately if it is DEFAULT_LOCALE, so that keys are shown while the translation messages load.
// This renders the translation _keys_, not the translation _messages_, which is only acceptable while loading the DEFAULT_LOCALE,
// as [there are no "default" messages](https://github.com/lingui/js-lingui/issues/388#issuecomment-497779030).
// See https://github.com/lingui/js-lingui/issues/1194#issuecomment-1068488619.
if (i18n.locale === undefined && locale === DEFAULT_LOCALE) {
i18n.loadLocaleData(DEFAULT_LOCALE, { plurals: () => plurals[DEFAULT_LOCALE] })
i18n.load(DEFAULT_LOCALE, {})
i18n.activate(DEFAULT_LOCALE)
}
return ( return (
<I18nProvider forceRenderOnLocaleChange={forceRenderAfterLocaleChange} i18n={i18n}> <I18nProvider forceRenderOnLocaleChange={forceRenderAfterLocaleChange} i18n={i18n}>
{children} {children}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment