Files
outline/app/utils/i18n.test.ts
T
Tom Moor 4a96de653b perf: Smaller locale files and fewer locale fetches (#14592)
* perf: Smaller locale files, start i18n with stored user language, preload locale

* fix: Set spellchecker when locale is unchanged, match lng from allowlist

* refactor: Type-safe locale cache keyed by supported language

* Remove locale preload

* fix: Ignore unsupported languages in changeLanguage
2026-11-14 19:57:53 -05:00

115 lines
3.2 KiB
TypeScript

import i18n from "i18next";
import de_DE from "../../shared/i18n/locales/de_DE/translation.json";
import en_GB from "../../shared/i18n/locales/en_GB/translation.json";
import en_US from "../../shared/i18n/locales/en_US/translation.json";
import pt_PT from "../../shared/i18n/locales/pt_PT/translation.json";
// i18n is already initialized globally via app/test/setup.ts — only add
// test resources here, without re-initializing the singleton.
beforeAll(() => {
i18n
.addResources("en-US", "translation", en_US)
.addResources("de-DE", "translation", de_DE)
.addResources("pt-PT", "translation", pt_PT);
});
describe("i18n env is unset", () => {
beforeEach(async () => {
await i18n.changeLanguage("en-US");
});
it("translation of key should match", () =>
expect(i18n.t("Saving")).toBe("Saving"));
it("translation if changed to de-DE", async () => {
await i18n.changeLanguage("de-DE");
expect(i18n.t("Saving")).toBe("Speichert");
});
it("translation if changed to pt-PT", async () => {
await i18n.changeLanguage("pt-PT");
expect(i18n.t("Saving")).toBe("A guardar");
});
});
describe("i18n env is en-US", () => {
beforeEach(async () => {
await i18n.changeLanguage("en-US");
});
it("translation of key should match", () =>
expect(i18n.t("Saving")).toBe("Saving"));
it("translation if changed to de-DE", async () => {
await i18n.changeLanguage("de-DE");
expect(i18n.t("Saving")).toBe("Speichert");
});
it("translation if changed to pt-PT", async () => {
await i18n.changeLanguage("pt-PT");
expect(i18n.t("Saving")).toBe("A guardar");
});
});
describe("i18n env is de-DE", () => {
beforeEach(async () => {
await i18n.changeLanguage("de-DE");
});
it("translation of key should match", () =>
expect(i18n.t("Saving")).toBe("Speichert"));
it("translation if changed to en-US", async () => {
await i18n.changeLanguage("en-US");
expect(i18n.t("Saving")).toBe("Saving");
});
it("translation if changed to pt-PT", async () => {
await i18n.changeLanguage("pt-PT");
expect(i18n.t("Saving")).toBe("A guardar");
});
});
describe("i18n env is pt-PT", () => {
beforeEach(async () => {
await i18n.changeLanguage("pt-PT");
});
it("translation of key should match", () =>
expect(i18n.t("Saving")).toBe("A guardar"));
it("translation if changed to en-US", async () => {
await i18n.changeLanguage("en-US");
expect(i18n.t("Saving")).toBe("Saving");
});
it("translation if changed to de-DE", async () => {
await i18n.changeLanguage("de-DE");
expect(i18n.t("Saving")).toBe("Speichert");
});
});
describe("translations without entries that match their key", () => {
beforeAll(async () => {
i18n.addResources(
"en-GB",
"translation",
Object.fromEntries(
Object.entries(en_GB).filter(([key, value]) => key !== value)
)
);
await i18n.changeLanguage("en-GB");
});
it("should show the key", () => expect(i18n.t("Saving")).toBe("Saving"));
it("should keep plurals", () => {
expect(i18n.t("{{ count }} document starred", { count: 1 })).toBe(
"1 document marked with a star"
);
expect(i18n.t("{{ count }} document starred", { count: 2 })).toBe(
"2 documents marked with a star"
);
});
});