133 lines
5.7 KiB
TypeScript
133 lines
5.7 KiB
TypeScript
import { readFileSync } from "node:fs";
|
|
import { fileURLToPath } from "node:url";
|
|
import path from "node:path";
|
|
import { describe, expect, test } from "vitest";
|
|
import { checkI18nParity } from "../../../scripts/i18n-check.ts";
|
|
import { defaultLocale, getDirection, i18nConfig, i18nNamespaces, isRTL, isSupportedLocale, supportedLocales } from "./index.js";
|
|
|
|
type JsonValue = boolean | null | number | string | JsonValue[] | { [key: string]: JsonValue };
|
|
type JsonObject = { [key: string]: JsonValue };
|
|
|
|
function readLocaleNamespace(locale: string, namespace: string): JsonObject {
|
|
return JSON.parse(
|
|
readFileSync(path.join(path.dirname(fileURLToPath(import.meta.url)), `../../../apps/web/public/locales/${locale}/${namespace}.json`), "utf8"),
|
|
) as JsonObject;
|
|
}
|
|
|
|
function collectLeafKeys(value: JsonValue, prefix = ""): string[] {
|
|
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
return prefix ? [prefix] : [];
|
|
}
|
|
|
|
return Object.entries(value)
|
|
.flatMap(([key, nestedValue]) => collectLeafKeys(nestedValue, prefix ? `${prefix}.${key}` : key))
|
|
.sort();
|
|
}
|
|
|
|
describe("@toolctrl/i18n", () => {
|
|
test("keeps German as the default and fallback locale", () => {
|
|
expect(defaultLocale).toBe("de");
|
|
expect(i18nConfig.fallbackLng).toBe("de");
|
|
});
|
|
|
|
test("keeps German and English as maintained locale set", () => {
|
|
expect(supportedLocales).toEqual(["de", "en"]);
|
|
expect(i18nConfig.supportedLngs).toEqual(["de", "en"]);
|
|
});
|
|
|
|
test("checks supported locales", () => {
|
|
expect(isSupportedLocale("de")).toBe(true);
|
|
expect(isSupportedLocale("en")).toBe(true);
|
|
expect(isSupportedLocale("fr")).toBe(false);
|
|
});
|
|
|
|
test("keeps German and English translation keys in parity", () => {
|
|
for (const namespace of i18nNamespaces) {
|
|
const germanKeys = collectLeafKeys(readLocaleNamespace("de", namespace));
|
|
const englishKeys = collectLeafKeys(readLocaleNamespace("en", namespace));
|
|
|
|
expect(englishKeys).toEqual(germanKeys);
|
|
}
|
|
});
|
|
|
|
test("keeps static UI translation calls backed by locale entries", () => {
|
|
const result = checkI18nParity({ cwd: path.join(path.dirname(fileURLToPath(import.meta.url)), "../../../") });
|
|
const missingKeysWithoutFallback = result.issues.filter((issue) => issue.type === "missing-source-key" && issue.fallback === "no-fallback");
|
|
|
|
expect(result.ok).toBe(true);
|
|
expect(missingKeysWithoutFallback).toEqual([]);
|
|
});
|
|
|
|
test("registers the legacy + per-area namespaces as loadable", () => {
|
|
// `translations` stays default for backwards-compat; `common`, `settings`,
|
|
// `billing`, `crm`, `auth` and `emailMarketing` are per-area namespaces
|
|
// split out of the legacy file. See #72.
|
|
const expected = ["translations", "errors", "mail", "common", "settings", "billing", "crm", "auth", "emailMarketing"];
|
|
expect(i18nNamespaces).toEqual(expected);
|
|
expect(i18nConfig.ns).toEqual(expected);
|
|
expect(i18nConfig.defaultNS).toBe("translations");
|
|
});
|
|
|
|
test("ships parity between de and en common namespace files", () => {
|
|
const germanKeys = collectLeafKeys(readLocaleNamespace("de", "common"));
|
|
const englishKeys = collectLeafKeys(readLocaleNamespace("en", "common"));
|
|
expect(englishKeys).toEqual(germanKeys);
|
|
expect(germanKeys.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
test("ships parity between de and en settings namespace files", () => {
|
|
const germanKeys = collectLeafKeys(readLocaleNamespace("de", "settings"));
|
|
const englishKeys = collectLeafKeys(readLocaleNamespace("en", "settings"));
|
|
expect(englishKeys).toEqual(germanKeys);
|
|
expect(germanKeys.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
test("ships parity between de and en for the per-area namespaces split from the legacy file", () => {
|
|
// #72 i18n namespace split — billing/crm/auth/emailMarketing moved out of
|
|
// translations.json into dedicated namespace files.
|
|
for (const namespace of ["billing", "crm", "auth", "emailMarketing"]) {
|
|
const germanKeys = collectLeafKeys(readLocaleNamespace("de", namespace));
|
|
const englishKeys = collectLeafKeys(readLocaleNamespace("en", namespace));
|
|
expect(englishKeys, namespace).toEqual(germanKeys);
|
|
expect(germanKeys.length, namespace).toBeGreaterThan(0);
|
|
}
|
|
});
|
|
|
|
test("keeps the errors namespace in parity between German and English", () => {
|
|
const germanKeys = collectLeafKeys(readLocaleNamespace("de", "errors"));
|
|
const englishKeys = collectLeafKeys(readLocaleNamespace("en", "errors"));
|
|
|
|
expect(englishKeys).toEqual(germanKeys);
|
|
for (const status of ["401", "403", "404", "500", "503"]) {
|
|
expect(germanKeys).toEqual(expect.arrayContaining([`${status}.eyebrow`, `${status}.title`, `${status}.description`]));
|
|
}
|
|
});
|
|
|
|
test("keeps the mail namespace in parity between German and English", () => {
|
|
const germanKeys = collectLeafKeys(readLocaleNamespace("de", "mail"));
|
|
const englishKeys = collectLeafKeys(readLocaleNamespace("en", "mail"));
|
|
|
|
expect(englishKeys).toEqual(germanKeys);
|
|
for (const template of ["welcome", "passwordReset", "userInvitation", "emailVerification", "accountSetup"]) {
|
|
expect(germanKeys).toEqual(expect.arrayContaining([`${template}.subject`]));
|
|
}
|
|
});
|
|
|
|
test("returns ltr for supported locales and rtl for known RTL languages", () => {
|
|
expect(getDirection("de")).toBe("ltr");
|
|
expect(getDirection("en")).toBe("ltr");
|
|
expect(getDirection("ar")).toBe("rtl");
|
|
expect(getDirection("he")).toBe("rtl");
|
|
expect(getDirection("fa")).toBe("rtl");
|
|
expect(getDirection("ur")).toBe("rtl");
|
|
expect(getDirection(undefined)).toBe("ltr");
|
|
expect(getDirection("xx-unknown")).toBe("ltr");
|
|
});
|
|
|
|
test("isRTL matches getDirection", () => {
|
|
expect(isRTL("de")).toBe(false);
|
|
expect(isRTL("ar")).toBe(true);
|
|
expect(isRTL(undefined)).toBe(false);
|
|
});
|
|
});
|