test(i18n): compare the {{…}} variables of each key between fr.json and en.json (#480)
The guard of #453 held both locale files to one set of keys but never
read the values: a key writing {{count}} in fr.json and {{n}} in en.json
passed it and showed a raw {{n}} in one language.
For every leaf both files hold, variablesOf reads the {{…}} tokens as
i18next does (format options included, without the whitespace i18next
ignores) and variableGaps lists every key whose token sets differ, with
the tokens of each side. A canary reads import.rootFiles.message_one
(count, folder, names) and the formatted token of
onboarding.card.transactions.uncategorized (filter, lowercase) in both
files. Measured on main ff8738b: 1743 common leaves, 117 with variables,
zero gap, so no exception list.
The leaf walk is shared with nodesOf, so a path compared for its
variables is a path compared for its key.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
ff8738b498
commit
16060f94d5
1 changed files with 158 additions and 7 deletions
|
|
@ -1,4 +1,5 @@
|
|||
// Guard: fr.json and en.json declare the same keys (#453).
|
||||
// Guard: fr.json and en.json declare the same keys (#453), and each key the
|
||||
// same interpolation variables (#480).
|
||||
//
|
||||
// i18next serves fr.json, the default language, and en.json, the fallback
|
||||
// (`src/i18n/config.ts`). A key written in fr.json only shows raw to an
|
||||
|
|
@ -8,11 +9,17 @@
|
|||
//
|
||||
// A key is the dotted path of a leaf, array indices included
|
||||
// (`docs.dashboard.features.6`) and plural suffixes too
|
||||
// (`import.sources.fileCount_one`). Values are never compared: a text left
|
||||
// untranslated in the other file passes. A path that is an array in one file
|
||||
// (`import.sources.fileCount_one`). A path that is an array in one file
|
||||
// and an object in the other, or a leaf in one and an object or an array in
|
||||
// the other, is a gap as well: `t(key, { returnObjects: true })` would hand
|
||||
// the component another shape.
|
||||
//
|
||||
// Of the values, only the `{{…}}` tokens are compared (#480): a text left
|
||||
// untranslated in the other file passes. A key that writes `{{count}}` in
|
||||
// fr.json and `{{n}}` in en.json shows a raw `{{…}}` in one of the two
|
||||
// languages, and a format option left out on one side (`{{filter}}` against
|
||||
// `{{filter, lowercase}}`) keeps the capital the sentence meant to drop: each
|
||||
// leaf both files hold carries one set of tokens, format options included.
|
||||
|
||||
import { describe, it, expect } from "vitest";
|
||||
import fr from "./locales/fr.json";
|
||||
|
|
@ -20,24 +27,41 @@ import en from "./locales/en.json";
|
|||
|
||||
type NodeKind = "object" | "array" | "leaf";
|
||||
|
||||
/** Every node under the root of a locale tree, by dotted path (array indices included), with its kind. */
|
||||
function nodesOf(tree: unknown): Map<string, NodeKind> {
|
||||
const nodes = new Map<string, NodeKind>();
|
||||
/**
|
||||
* Calls `onNode` for every node under the root of a locale tree, with its
|
||||
* dotted path (array indices included), its kind and its value.
|
||||
*/
|
||||
function walk(tree: unknown, onNode: (path: string, kind: NodeKind, value: unknown) => void): void {
|
||||
const visit = (value: unknown, path: string[]): void => {
|
||||
const kind: NodeKind = Array.isArray(value)
|
||||
? "array"
|
||||
: typeof value === "object" && value !== null
|
||||
? "object"
|
||||
: "leaf";
|
||||
if (path.length > 0) nodes.set(path.join("."), kind);
|
||||
if (path.length > 0) onNode(path.join("."), kind, value);
|
||||
if (kind === "leaf") return;
|
||||
// An array's entries are its indices, as strings: `features.6`.
|
||||
for (const [key, item] of Object.entries(value as object)) visit(item, [...path, key]);
|
||||
};
|
||||
visit(tree, []);
|
||||
}
|
||||
|
||||
/** Every node under the root of a locale tree, by dotted path (array indices included), with its kind. */
|
||||
function nodesOf(tree: unknown): Map<string, NodeKind> {
|
||||
const nodes = new Map<string, NodeKind>();
|
||||
walk(tree, (path, kind) => nodes.set(path, kind));
|
||||
return nodes;
|
||||
}
|
||||
|
||||
/** Every leaf of a locale tree, by the dotted path `nodesOf` gives it, with its text. */
|
||||
function leavesOf(tree: unknown): Map<string, string> {
|
||||
const leaves = new Map<string, string>();
|
||||
walk(tree, (path, kind, value) => {
|
||||
if (kind === "leaf") leaves.set(path, String(value));
|
||||
});
|
||||
return leaves;
|
||||
}
|
||||
|
||||
/**
|
||||
* The keys of `from` that `into` lacks, sorted: the leaves of `from` with
|
||||
* nothing at the same path in `into`. A path `into` holds with another kind is
|
||||
|
|
@ -63,6 +87,48 @@ function kindGaps(
|
|||
.sort();
|
||||
}
|
||||
|
||||
/**
|
||||
* The `{{…}}` tokens of a text, sorted, each once. A token is what i18next
|
||||
* reads between `{{` and the next `}}`, format options included
|
||||
* (`filter, lowercase`), without the whitespace i18next ignores: around the
|
||||
* name and around each format option.
|
||||
*/
|
||||
function variablesOf(text: string): string[] {
|
||||
const tokens = [...text.matchAll(/\{\{(.+?)\}\}/g)].map(([, inner]) =>
|
||||
inner
|
||||
.split(",")
|
||||
.map((part) => part.trim())
|
||||
.join(", "),
|
||||
);
|
||||
return [...new Set(tokens)].sort();
|
||||
}
|
||||
|
||||
/**
|
||||
* The leaves both maps hold with other `{{…}}` tokens, sorted, as
|
||||
* `path: {{a}}, {{b}} in <a>; {{a}}, {{c}} in <b>`, every token of each side
|
||||
* listed. A path only one side holds, or holds with another kind, is left to
|
||||
* `missingKeys` and `kindGaps`.
|
||||
*/
|
||||
function variableGaps(
|
||||
a: Map<string, string>,
|
||||
b: Map<string, string>,
|
||||
nameA: string,
|
||||
nameB: string,
|
||||
): string[] {
|
||||
const listed = (tokens: string[]): string =>
|
||||
tokens.length > 0 ? tokens.map((token) => `{{${token}}}`).join(", ") : "no variable";
|
||||
const gaps: string[] = [];
|
||||
for (const [path, text] of a) {
|
||||
const other = b.get(path);
|
||||
if (other === undefined) continue;
|
||||
const inA = variablesOf(text);
|
||||
const inB = variablesOf(other);
|
||||
const same = inA.length === inB.length && inA.every((token, i) => token === inB[i]);
|
||||
if (!same) gaps.push(`${path}: ${listed(inA)} in ${nameA}; ${listed(inB)} in ${nameB}`);
|
||||
}
|
||||
return gaps.sort();
|
||||
}
|
||||
|
||||
const FR = nodesOf(fr);
|
||||
const EN = nodesOf(en);
|
||||
|
||||
|
|
@ -127,3 +193,88 @@ describe("locale keys: the comparison (#453)", () => {
|
|||
expect(kindGaps(a, b, "a", "b")).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
const FR_LEAVES = leavesOf(fr);
|
||||
const EN_LEAVES = leavesOf(en);
|
||||
|
||||
describe("locale values: interpolation variables (#480)", () => {
|
||||
it("reads the tokens of each file, format options included", () => {
|
||||
// The canary: texts read as empty, or tokens read as nothing, would make
|
||||
// the comparison pass on nothing, and so would one file read twice.
|
||||
for (const [name, leaves] of [["fr.json", FR_LEAVES], ["en.json", EN_LEAVES]] as const) {
|
||||
expect(variablesOf(leaves.get("import.rootFiles.message_one") ?? ""), name).toEqual([
|
||||
"count",
|
||||
"folder",
|
||||
"names",
|
||||
]);
|
||||
expect(variablesOf(leaves.get("onboarding.card.transactions.uncategorized") ?? ""), name).toContain(
|
||||
"filter, lowercase",
|
||||
);
|
||||
}
|
||||
expect(EN_LEAVES.get("import.rootFiles.message_one"), "en.json and fr.json are read apart").not.toBe(
|
||||
FR_LEAVES.get("import.rootFiles.message_one"),
|
||||
);
|
||||
});
|
||||
|
||||
it("gives every leaf both files hold the same {{…}} tokens", () => {
|
||||
expect(variableGaps(FR_LEAVES, EN_LEAVES, "fr.json", "en.json"), "leaves with other variables").toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("locale values: the variable comparison (#480)", () => {
|
||||
const gaps = (a: object, b: object): string[] => variableGaps(leavesOf(a), leavesOf(b), "a", "b");
|
||||
|
||||
it("reads each token whole, format options included, sorted and each once", () => {
|
||||
expect(variablesOf("{{names}}: {{count}} files in {{folder}}")).toEqual(["count", "folder", "names"]);
|
||||
expect(variablesOf("Only {{filter, lowercase}} ones")).toEqual(["filter, lowercase"]);
|
||||
expect(variablesOf("{{n}} of {{n}}")).toEqual(["n"]);
|
||||
expect(variablesOf("No variable")).toEqual([]);
|
||||
});
|
||||
|
||||
it("drops only the whitespace i18next ignores, around the name and each format option", () => {
|
||||
expect(variablesOf("{{ count }}, {{filter,lowercase}}, {{ filter , lowercase }}")).toEqual([
|
||||
"count",
|
||||
"filter, lowercase",
|
||||
]);
|
||||
});
|
||||
|
||||
it("names a variable renamed on one side, with every token of each side", () => {
|
||||
expect(gaps({ x: "{{count}} in {{folder}}" }, { x: "{{count}} in {{dir}}" })).toEqual([
|
||||
"x: {{count}}, {{folder}} in a; {{count}}, {{dir}} in b",
|
||||
]);
|
||||
});
|
||||
|
||||
it("names a variable missing on either side", () => {
|
||||
expect(gaps({ x: "{{count}} files" }, { x: "Files" })).toEqual(["x: {{count}} in a; no variable in b"]);
|
||||
expect(gaps({ x: "Files" }, { x: "{{count}} files" })).toEqual(["x: no variable in a; {{count}} in b"]);
|
||||
expect(gaps({ x: "{{a}} and {{b}}" }, { x: "{{b}}" })).toEqual(["x: {{a}}, {{b}} in a; {{b}} in b"]);
|
||||
});
|
||||
|
||||
it("holds another format option, or none, on one side as a gap", () => {
|
||||
expect(gaps({ x: "Only {{filter, lowercase}}" }, { x: "Only {{filter}}" })).toEqual([
|
||||
"x: {{filter, lowercase}} in a; {{filter}} in b",
|
||||
]);
|
||||
expect(gaps({ x: "{{filter, lowercase}}" }, { x: "{{filter, uppercase}}" })).toEqual([
|
||||
"x: {{filter, lowercase}} in a; {{filter, uppercase}} in b",
|
||||
]);
|
||||
});
|
||||
|
||||
it("lists every leaf in gap, array items included, not only the first", () => {
|
||||
expect(
|
||||
gaps(
|
||||
{ z: "{{a}}", list: ["{{n}}", "{{m}}"], same: "{{s}}" },
|
||||
{ z: "{{b}}", list: ["{{n}}", "{{k}}"], same: "{{s}}" },
|
||||
),
|
||||
).toEqual(["list.1: {{m}} in a; {{k}} in b", "z: {{a}} in a; {{b}} in b"]);
|
||||
});
|
||||
|
||||
it("passes the same variables in another order, a repeated variable and a key without variable", () => {
|
||||
expect(gaps({ x: "{{a}}, then {{b}}" }, { x: "{{b}}, then {{a}}" })).toEqual([]);
|
||||
expect(gaps({ x: "{{count}} of {{count}}" }, { x: "{{count}}" })).toEqual([]);
|
||||
expect(gaps({ x: "Annuler" }, { x: "Cancel" })).toEqual([]);
|
||||
});
|
||||
|
||||
it("leaves a path one side lacks, or holds with another kind, to the key comparison", () => {
|
||||
expect(gaps({ only: "{{a}}", x: { y: "{{a}}" } }, { x: "{{b}}" })).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue