Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(web,i18n): preload all namespaces after init and locale change

justin4f51e5c1b69a
diff --git a/apps/web/src/lib/i18n/index.test.ts b/apps/web/src/lib/i18n/index.test.ts
new file mode 100644
index 000000000..8154747b9
--- /dev/null
+++ b/apps/web/src/lib/i18n/index.test.ts
@@ -0,0 +1,36 @@
+import { describe, expect, it, vi } from "vitest";
+
+vi.mock("@i18n/resources", async () => {
+ const actual =
+ await vi.importActual<typeof import("@i18n/resources")>("@i18n/resources");
+ return {
+ ...actual,
+ loadLocale: vi.fn(async () => ({
+ common: { testCommon: "common-value" },
+ auth: { testAuth: "auth-value" },
+ })),
+ };
+});
+
+const { i18n, preloadNamespaces } = await import("./index");
+const resources = await import("@i18n/resources");
+
+describe("preloadNamespaces", () => {
+ it("loads every namespace so non-default keys resolve after async init", async () => {
+ await preloadNamespaces("en-US");
+
+ expect(i18n.t("auth:testAuth")).toBe("auth-value");
+ expect(i18n.t("common:testCommon")).toBe("common-value");
+ });
+
+ it("reuses the cached locale JSON across calls", async () => {
+ const callsBefore = (resources.loadLocale as ReturnType<typeof vi.fn>).mock
+ .calls.length;
+
+ await preloadNamespaces("en-US");
+
+ expect(
+ (resources.loadLocale as ReturnType<typeof vi.fn>).mock.calls.length,
+ ).toBe(callsBefore);
+ });
+});
diff --git a/apps/web/src/lib/i18n/index.ts b/apps/web/src/lib/i18n/index.ts
index 71e25c79d..7ebf29541 100644
--- a/apps/web/src/lib/i18n/index.ts
+++ b/apps/web/src/lib/i18n/index.ts
@@ -42,23 +42,50 @@ export function getBrowserLocale(): string | null {
return navigator.language || navigator.languages?.[0] || null;
}
+// Components subscribe to the default namespace only, so any other namespace
+// they reference through `t("ns:key")` must be preloaded after init and on
+// every locale change. The lazy backend already returns the whole locale JSON,
+// so cache it once per locale to avoid a fresh dynamic import per namespace.
+const localeResources = new Map<AppLocale, Promise<Record<string, unknown>>>();
+
+function loadLocaleResources(
+ locale: AppLocale,
+): Promise<Record<string, unknown>> {
+ const cached = localeResources.get(locale);
+ if (cached) return cached;
+ const pending = loadLocale(locale).then(
+ (resources) => resources as Record<string, unknown>,
+ );
+ localeResources.set(locale, pending);
+ return pending;
+}
+
+export function preloadNamespaces(locale: AppLocale): Promise<void> {
+ return loadLocaleResources(locale).then((resources) =>
+ i18n.loadNamespaces(Object.keys(resources)),
+ );
+}
+
+const initialLocale = resolveLocale(null, getBrowserLocale());
+
void i18n
.use(
resourcesToBackend((language: string, namespace: string) => {
const locale = isSupportedLocale(language) ? language : defaultLocale;
- return loadLocale(locale).then(
- (resources) => (resources as Record<string, unknown>)[namespace],
+ return loadLocaleResources(locale).then(
+ (resources) => resources[namespace],
);
}),
)
.use(initReactI18next)
.init({
- lng: resolveLocale(null, getBrowserLocale()),
+ lng: initialLocale,
fallbackLng: defaultLocale,
defaultNS: "common",
interpolation: {
escapeValue: false,
},
- });
+ })
+ .then(() => preloadNamespaces(initialLocale));
export { i18n };
diff --git a/apps/web/src/lib/i18n/provider.tsx b/apps/web/src/lib/i18n/provider.tsx
index ca54ed636..6c84963ce 100644
--- a/apps/web/src/lib/i18n/provider.tsx
+++ b/apps/web/src/lib/i18n/provider.tsx
@@ -1,7 +1,12 @@
import { type PropsWithChildren, useEffect, useMemo } from "react";
import { I18nextProvider } from "react-i18next";
import useAuth from "@/components/providers/auth-provider/hooks/use-auth";
-import { getBrowserLocale, i18n, resolveLocale } from "./index";
+import {
+ getBrowserLocale,
+ i18n,
+ preloadNamespaces,
+ resolveLocale,
+} from "./index";
export function AppI18nProvider({ children }: PropsWithChildren) {
const { user } = useAuth();
@@ -12,7 +17,9 @@ export function AppI18nProvider({ children }: PropsWithChildren) {
);
useEffect(() => {
- void i18n.changeLanguage(resolvedLocale);
+ void i18n
+ .changeLanguage(resolvedLocale)
+ .then(() => preloadNamespaces(resolvedLocale));
document.documentElement.lang = resolvedLocale;
}, [resolvedLocale]);