Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

✨ feat: preload bundled i18n resources and lazy-load target language (#12929)

Innei8b29bb7fc968
diff --git a/src/locales/create.ts b/src/locales/create.ts
index 7a743a0f169..fef5d63070e 100644
--- a/src/locales/create.ts
+++ b/src/locales/create.ts
@@ -17,7 +17,14 @@ import { unwrapESMModule } from '@/utils/esm/unwrapESMModule';
import { loadI18nNamespaceModule } from '../utils/i18n/loadI18nNamespaceModule';
-const defaultResources = { chat, common, error };
+const createBundledResources = () => ({
+ chat: { ...chat },
+ common: { ...common },
+ error: { ...error },
+});
+
+const defaultResources = createBundledResources();
+const bundledNamespaces = Object.keys(defaultResources);
const { I18N_DEBUG, I18N_DEBUG_BROWSER, I18N_DEBUG_SERVER } = getDebugConfig();
const debugMode = (I18N_DEBUG ?? isOnServerSide) ? I18N_DEBUG_SERVER : I18N_DEBUG_BROWSER;
@@ -48,17 +55,29 @@ export const createI18nNext = (lang?: string) => {
return {
init: (params: { initAsync?: boolean } = {}) => {
const { initAsync = true } = params;
+ const initialLang = normalizeLocale(lang);
+ const bundledLanguageResources =
+ initialLang === DEFAULT_LANG
+ ? {
+ [DEFAULT_LANG]: defaultResources,
+ }
+ : {
+ [DEFAULT_LANG]: defaultResources,
+ [initialLang]: createBundledResources(),
+ };
- return instance.init({
+ const initPromise = instance.init({
debug: debugMode,
defaultNS: ['error', 'common', 'chat'],
fallbackLng: DEFAULT_LANG,
-
initAsync,
+ // Keep init synchronous so components can render with bundled en-US resources
+ // before the user's actual language finishes loading in the background.
+ ns: [],
// Preload default language (en-US) synchronously to avoid Suspense on first render
resources: {
- [DEFAULT_LANG]: defaultResources,
+ ...bundledLanguageResources,
},
// Keep backend loading enabled for namespaces that are not preloaded above.
partialBundledLanguages: true,
@@ -66,10 +85,24 @@ export const createI18nNext = (lang?: string) => {
interpolation: {
escapeValue: false,
},
+ // Re-render components when new language resources are loaded from backend,
+ // so preloaded en-US fallback gets replaced by the user's actual language.
+ react: {
+ bindI18nStore: 'added',
+ useSuspense: false,
+ },
keySeparator: false,
- lng: lang,
+ lng: initialLang,
});
+
+ if (initialLang !== DEFAULT_LANG) {
+ initPromise.then(() => {
+ void instance.reloadResources([initialLang], bundledNamespaces);
+ });
+ }
+
+ return initPromise;
},
instance,
};
diff --git a/src/utils/i18n/createI18n.test.ts b/src/utils/i18n/createI18n.test.ts
new file mode 100644
index 00000000000..58ef92bdc7b
--- /dev/null
+++ b/src/utils/i18n/createI18n.test.ts
@@ -0,0 +1,44 @@
+// @vitest-environment node
+import { describe, expect, it, vi } from 'vitest';
+
+const loadI18nNamespaceModule = vi.fn(async ({ lng, ns }: { lng: string; ns: string }) => ({
+ default: {
+ key: `${lng}:${ns}`,
+ },
+}));
+
+vi.mock('./loadI18nNamespaceModule', () => ({
+ loadI18nNamespaceModule,
+}));
+
+describe('createI18nNext', () => {
+ it('initializes synchronously with bundled fallback resources and reloads the actual language in background', async () => {
+ const { createI18nNext } = await import('@/locales/create');
+
+ const i18n = createI18nNext('zh-CN');
+ const reloadSpy = vi.spyOn(i18n.instance, 'reloadResources');
+ const initPromise = i18n.init({ initAsync: false });
+
+ expect(i18n.instance.isInitialized).toBe(true);
+ expect(i18n.instance.getResource('zh-CN', 'common', 'copy')).toBeDefined();
+
+ await initPromise;
+ await Promise.resolve();
+ await Promise.resolve();
+
+ expect(i18n.instance.hasResourceBundle('zh-CN', 'common')).toBe(true);
+ expect(i18n.instance.hasResourceBundle('zh-CN', 'chat')).toBe(true);
+ expect(i18n.instance.hasResourceBundle('zh-CN', 'error')).toBe(true);
+
+ expect(reloadSpy).toHaveBeenCalledWith(['zh-CN'], ['chat', 'common', 'error']);
+ expect(loadI18nNamespaceModule).toHaveBeenCalledWith(
+ expect.objectContaining({ lng: 'zh-CN', ns: 'common' }),
+ );
+ expect(loadI18nNamespaceModule).toHaveBeenCalledWith(
+ expect.objectContaining({ lng: 'zh-CN', ns: 'chat' }),
+ );
+ expect(loadI18nNamespaceModule).toHaveBeenCalledWith(
+ expect.objectContaining({ lng: 'zh-CN', ns: 'error' }),
+ );
+ });
+});