plan/react-rewrite #1
@@ -0,0 +1,46 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { createI18nInstance } from "./config.js";
|
||||
|
||||
describe("createI18nInstance", () => {
|
||||
it("creates an initialized i18next instance for the given locale", async () => {
|
||||
const i18n = await createI18nInstance({ locale: "ru" });
|
||||
expect(i18n.language).toBe("ru");
|
||||
expect(i18n.isInitialized).toBe(true);
|
||||
});
|
||||
|
||||
it("can translate a known key from the Russian locale", async () => {
|
||||
const i18n = await createI18nInstance({ locale: "ru" });
|
||||
const value = i18n.t("BOARD.DEPARTURE");
|
||||
// The Russian file has "BOARD.DEPARTURE" = "Вылет"
|
||||
expect(value).toBe("Вылет");
|
||||
});
|
||||
|
||||
it("can translate a known key from the English locale", async () => {
|
||||
const i18n = await createI18nInstance({ locale: "en" });
|
||||
const value = i18n.t("BOARD.DEPARTURE");
|
||||
expect(value).toBe("Departure");
|
||||
});
|
||||
|
||||
it("uses initialResources instead of loading from filesystem when provided", async () => {
|
||||
const i18n = await createI18nInstance({
|
||||
locale: "ru",
|
||||
initialResources: {
|
||||
common: { TEST_KEY: "Тестовое значение" },
|
||||
},
|
||||
});
|
||||
expect(i18n.t("TEST_KEY")).toBe("Тестовое значение");
|
||||
});
|
||||
|
||||
it("returns the key path if a key is missing (no fallback to other locale)", async () => {
|
||||
const i18n = await createI18nInstance({ locale: "ru" });
|
||||
expect(i18n.t("NONEXISTENT.KEY")).toBe("NONEXISTENT.KEY");
|
||||
});
|
||||
|
||||
it("each call returns a fresh instance (request-scoped)", async () => {
|
||||
const a = await createI18nInstance({ locale: "ru" });
|
||||
const b = await createI18nInstance({ locale: "en" });
|
||||
expect(a).not.toBe(b);
|
||||
expect(a.language).toBe("ru");
|
||||
expect(b.language).toBe("en");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
import i18next from "i18next";
|
||||
import ICU from "i18next-icu";
|
||||
import resourcesToBackend from "i18next-resources-to-backend";
|
||||
import type { Language } from "./resolver.js";
|
||||
|
||||
export async function createI18nInstance(options: {
|
||||
locale: Language;
|
||||
initialResources?: Record<string, Record<string, unknown>>;
|
||||
}): Promise<typeof i18next> {
|
||||
const instance = i18next.createInstance();
|
||||
|
||||
instance.use(ICU);
|
||||
|
||||
// If no pre-loaded resources, load from the locale JSON files on the filesystem.
|
||||
if (!options.initialResources) {
|
||||
instance.use(
|
||||
resourcesToBackend(
|
||||
(language: string, namespace: string) =>
|
||||
import(`./locales/${language}/${namespace}.json`),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
await instance.init({
|
||||
lng: options.locale,
|
||||
ns: ["common"],
|
||||
defaultNS: "common",
|
||||
fallbackLng: false,
|
||||
interpolation: {
|
||||
escapeValue: false,
|
||||
},
|
||||
keySeparator: ".",
|
||||
...(options.initialResources
|
||||
? {
|
||||
resources: {
|
||||
[options.locale]: options.initialResources,
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
});
|
||||
|
||||
return instance;
|
||||
}
|
||||
Reference in New Issue
Block a user