Finish extraction: 100%, and a coverage number worth believing

The 143 the codemod refused turned out to be two different things, and only
one of them needed a person.

A third were phrases sitting next to an icon -- `<Plus /> New rule`. The
refusal rule was "has siblings", which is broader than the danger: what breaks
a translation is a sibling that renders *text*, splitting a sentence into
fragments no one can reorder. An element beside a phrase does not. Narrowing
the rule to text-producing siblings let the codemod take 73 more.

The rest were real sentences with values in the middle, rebuilt by hand as
named placeholders -- "Your active script “{name}” was written by hand",
"Waiting on the server — goes out {when}." Named rather than positional
because a translator moves the parts around; counted things go through
plural() so Russian and Ukrainian get their three forms rather than English's
two.

Sentences with an element inside them needed something new. `Open <code>mailto:
</code> links in ihasmail` has two obvious treatments and both are wrong:
splitting it into two t() calls hands over fragments that cannot be reordered,
and dropping the <code> keeps the sentence whole but loses the monospace that
said "this is a literal". tNode() keeps the sentence whole and makes the
element a named hole in it, so a translator sees one sentence and can put the
hole where their language wants it. The German test asserts exactly that: the
same call renders the code first when the catalogue says so.

The coverage number was also lying, and it is worth saying how. It counted
text inside <code> and inside translate="no" as untranslated work, and
placeholders like "123456" and "+1 555 0100" -- a one-time code and a phone
format. None of those will ever be translated, so the report sat at 21 with 6
real items left. A number with an unreachable floor is something to argue with
rather than act on, so the tool now applies the same rules the codemod does.

596 wrapped, nothing remaining. Verified in the browser across 15 views, which
is where the last bulk pass hid a bug the tests could not see: no entities, no
unfilled placeholders, no raw t( in rendered text, and the toggle switches that
looked like emptied labels are text-free by design.
This commit is contained in:
2026-08-31 10:41:24 -07:00
parent 46c1dc28e3
commit 3f4b33cb51
40 changed files with 228 additions and 123 deletions
+111
View File
@@ -0,0 +1,111 @@
import { afterEach, describe, expect, it } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
import { currentLanguage, interpolate, plural, setCatalog, t, tNode, type Catalog } from "@/lib/i18n";
const de: Catalog = {
strings: {
"Archive": "Archivieren",
"Move {n} to {folder}": "{n} nach {folder} verschieben",
// German puts the parts in a different order, which is the whole reason
// the element is a named hole rather than a split sentence.
"Open {scheme} links here": "{scheme}-Links hier öffnen",
},
plurals: { "{n} messages": { one: "{n} Nachricht", other: "{n} Nachrichten" } },
};
/* Russian is the reason plural() does not take (one, other): it needs three
forms, and which one applies is not a question about the number 1. */
const ru: Catalog = {
strings: {},
plurals: { "{n} messages": { one: "{n} сообщение", few: "{n} сообщения", many: "{n} сообщений", other: "{n} сообщения" } },
};
afterEach(() => setCatalog("en", { strings: {}, plurals: {} }));
describe("t", () => {
it("returns the English it was given when nothing is loaded", () => {
// The whole point of English-as-key: a missing translation degrades to
// readable English rather than to a symbolic name leaking into the UI.
expect(t("Archive")).toBe("Archive");
expect(currentLanguage()).toBe("en");
});
it("translates once a catalogue is in force", () => {
setCatalog("de", de);
expect(t("Archive")).toBe("Archivieren");
});
it("falls back per string, not per catalogue", () => {
setCatalog("de", de);
expect(t("Report spam")).toBe("Report spam");
});
});
describe("interpolation", () => {
it("fills named placeholders", () => {
expect(interpolate("Move {n} to {folder}", { n: 3, folder: "Archive" })).toBe("Move 3 to Archive");
});
it("survives a translator reordering the sentence", () => {
// Positional arguments would not: German moves the parts around and means
// the same thing.
setCatalog("de", de);
expect(t("Move {n} to {folder}", { n: 3, folder: "Archiv" })).toBe("3 nach Archiv verschieben");
});
it("leaves an unknown placeholder alone rather than printing undefined", () => {
expect(interpolate("Hello {who}", {})).toBe("Hello {who}");
});
});
describe("plural", () => {
const FORMS = { one: "{n} message", other: "{n} messages" };
it("picks the English form without a catalogue", () => {
expect(plural(1, FORMS)).toBe("1 message");
expect(plural(0, FORMS)).toBe("0 messages");
expect(plural(5, FORMS)).toBe("5 messages");
});
it("uses the target language's own rule, not English's", () => {
setCatalog("ru", ru);
expect(plural(1, FORMS)).toBe("1 сообщение"); // one
expect(plural(3, FORMS)).toBe("3 сообщения"); // few
expect(plural(7, FORMS)).toBe("7 сообщений"); // many
});
it("falls back to `other` when the catalogue lacks the category", () => {
setCatalog("de", de);
// German has no "few"; asking for 3 must not render undefined.
expect(plural(3, FORMS)).toBe("3 Nachrichten");
});
it("takes extra variables alongside the count", () => {
expect(plural(2, { one: "{n} message in {folder}", other: "{n} messages in {folder}" }, { folder: "Inbox" }))
.toBe("2 messages in Inbox");
});
});
describe("tNode", () => {
const render = (node: React.ReactNode) => renderToStaticMarkup(<>{node}</>);
it("keeps an element inside the sentence", () => {
expect(render(tNode("Open {scheme} links here", { scheme: <code>mailto:</code> })))
.toBe("Open <code>mailto:</code> links here");
});
it("lets a translator move the element", () => {
// Splitting the sentence into two t() calls could not do this: the
// fragments would render in the English order whatever the catalogue said.
setCatalog("de", de);
expect(render(tNode("Open {scheme} links here", { scheme: <code>mailto:</code> })))
.toBe("<code>mailto:</code>-Links hier öffnen");
});
it("leaves a placeholder alone when nothing is supplied for it", () => {
expect(render(tNode("Open {scheme} links here", {}))).toBe("Open {scheme} links here");
});
it("takes plain variables alongside elements", () => {
expect(render(tNode("{count} of {scheme}", { scheme: <b>x</b> }, { count: 3 }))).toBe("3 of <b>x</b>");
});
});