Files
RuneStone/scripts/i18nAudit.mjs
T
Knotty Wyvern e9c59f28de Send the remaining interface text through the catalog and audit it
Phase 4f.

- scripts/i18nAudit.mjs runs in pnpm check. It fails for a key that the
  catalog does not have, a catalog key that the code does not use, and
  a row without all four languages.
- message() marks a key that a variable carries to the translation call
  later: table formats, formula errors, and browser status messages.
- 53 new rows with French, Japanese, and Simplified Chinese text for the
  status and error messages of the workbench, the tree, Settings, theme
  import, images, the built-in browser, and code blocks. The backlinks
  toggle now translates too.
- 13 unused rows are gone.
- Errors of the plugin API stay in English. docs/development.md has the
  rule in "Interface text".

pnpm check, pnpm test (350 of 350), and the full smoke suite pass,
except two tests that failed one time under load and passed on retry
and 3 of 3 times alone.
2026-10-28 12:00:24 -07:00

77 lines
3.5 KiB
JavaScript

// Check the translation catalog (src/lib/i18n/messages.ts) against the code:
// - unknown: the code translates a key that the catalog does not have,
// - unused: the catalog has a key that the code never translates,
// - untranslated: a catalog row has an empty or missing translation.
// The code translates a key with `$t("…")` in Svelte, `tr("…")` in TypeScript, or marks it with `message("…")`
// when a variable carries it to the translation call later. Every string literal in the first argument counts,
// so `tr(open ? "Close" : "Open")` uses both keys.
import { readdir, readFile } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
const root = fileURLToPath(new URL("../", import.meta.url));
const catalogFile = path.join(root, "src/lib/i18n/messages.ts");
const calls = /(?:\$t|\btr|\bmessage)\(/g;
async function sources(dir) {
const entries = await readdir(dir, { withFileTypes: true });
const nested = await Promise.all(entries.map(entry => entry.isDirectory()
? sources(path.join(dir, entry.name))
: /\.(svelte|ts)$/.test(entry.name) ? [path.join(dir, entry.name)] : []));
return nested.flat();
}
/** The string literals of the first argument that starts at `start`. A literal in a comparison is not a key. */
function argumentKeys(source, start) {
const keys = [];
let depth = 0;
for (let index = start; index < source.length; index++) {
const char = source[index];
if (char === "\"" || char === "'" || char === "`") {
let value = "", end = index + 1, dynamic = false;
for (; end < source.length && source[end] !== char; end++) {
if (source[end] === "\\") { value += source[++end]; continue; }
if (char === "`" && source[end] === "$" && source[end + 1] === "{") dynamic = true;
value += source[end];
}
const compared = /[=!]==?\s*$/.test(source.slice(start, index)) || /^\s*[=!]==?/.test(source.slice(end + 1));
if (!dynamic && !compared) keys.push(value);
index = end;
} else if ("([{".includes(char)) depth++;
else if (")]}".includes(char)) { if (depth === 0) break; depth--; }
else if (char === "," && depth === 0) break;
}
return keys;
}
export async function auditCatalog() {
const { messageRows } = await import(pathToFileURL(catalogFile).href);
const catalog = new Map(messageRows.map(row => [row[0], row]));
const used = new Set();
const failures = [];
for (const file of await sources(path.join(root, "src"))) {
if (file === catalogFile) continue;
const source = await readFile(file, "utf8");
for (const match of source.matchAll(calls)) {
for (const key of argumentKeys(source, match.index + match[0].length)) {
used.add(key);
if (!catalog.has(key)) {
const line = source.slice(0, match.index).split("\n").length;
failures.push(`Unknown key: ${path.relative(root, file).replaceAll("\\", "/")}:${line}: ${key}`);
}
}
}
}
for (const [key, row] of catalog) {
if (!used.has(key)) failures.push(`Unused key: ${key}`);
if (row.length < 4 || row.slice(1).some(value => !value?.trim())) failures.push(`Untranslated key: ${key}`);
}
return { failures, keys: catalog.size, used: used.size };
}
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
const { failures, keys } = await auditCatalog();
if (failures.length) { console.error(failures.join("\n")); process.exitCode = 1; }
else console.log(`PASS: the code uses all ${keys} catalog keys, and each key has every translation.`);
}