plan/react-rewrite #1

Merged
gnezim merged 138 commits from plan/react-rewrite into main 2026-04-15 12:21:16 +03:00
2 changed files with 89 additions and 0 deletions
Showing only changes of commit 1fd2788c35 - Show all commits
+46
View File
@@ -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");
});
});
+43
View File
@@ -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;
}