From a8f09ad43fbd6de52234fa8378ba7348024c72f8 Mon Sep 17 00:00:00 2001 From: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Date: Thu, 24 Jul 2025 14:40:37 +0800 Subject: [PATCH] =?UTF-8?q?refactor(i18next):=20streamline=20fallback=20tr?= =?UTF-8?q?anslation=20handling=20and=20initi=E2=80=A6=20(#22894)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/i18n/i18next-config.ts | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/web/i18n/i18next-config.ts b/web/i18n/i18next-config.ts index 8c5bd375a7..7af727af7e 100644 --- a/web/i18n/i18next-config.ts +++ b/web/i18n/i18next-config.ts @@ -50,24 +50,35 @@ export const loadLangResources = async (lang: string) => { acc[camelCase(NAMESPACES[index])] = mod return acc }, {} as Record) + return resources +} + +const getFallbackTranslation = () => { + const resources = NAMESPACES.reduce((acc, ns, index) => { + acc[camelCase(NAMESPACES[index])] = require(`./en-US/${ns}`).default + return acc + }, {} as Record) return { translation: resources, } } -i18n.use(initReactI18next) - .init({ - lng: undefined, - fallbackLng: 'en-US', - }) +if (!i18n.isInitialized) { + i18n.use(initReactI18next) + .init({ + lng: undefined, + fallbackLng: 'en-US', + resources: { + 'en-US': getFallbackTranslation(), + }, + }) +} export const changeLanguage = async (lng?: string) => { const resolvedLng = lng ?? 'en-US' - const resources = { - [resolvedLng]: await loadLangResources(resolvedLng), - } + const resource = await loadLangResources(resolvedLng) if (!i18n.hasResourceBundle(resolvedLng, 'translation')) - i18n.addResourceBundle(resolvedLng, 'translation', resources[resolvedLng].translation, true, true) + i18n.addResourceBundle(resolvedLng, 'translation', resource, true, true) await i18n.changeLanguage(resolvedLng) }