123 lines
5.9 KiB
JavaScript
123 lines
5.9 KiB
JavaScript
#!/usr/bin/env node
|
||
// PEP-98: dictionary coverage. Every key marked in code — `t("…")`, `m("…")`, `t.n(n, "…", "…", "…")`,
|
||
// `tn(locale, n, …)` — must have an English translation; in the strict areas (web, bot) every
|
||
// Cyrillic string literal and JSX text must be such a key. Exemptions: `// i18n-ignore` on the
|
||
// line or the line above, `// i18n-ignore-file` anywhere in the file.
|
||
// node scripts/i18n-check.ts — check, exit 1 on problems
|
||
// node scripts/i18n-check.ts --summary — counts per file only
|
||
// node scripts/i18n-check.ts --file <path> — one file, strict
|
||
import { readdirSync, readFileSync, statSync } from "node:fs";
|
||
import { join, relative } from "node:path";
|
||
import ts from "typescript";
|
||
import { dictionary } from "../packages/shared/src/i18n/index.ts";
|
||
|
||
const root = new URL("..", import.meta.url).pathname;
|
||
const STRICT = ["apps/web/app", "apps/web/components", "apps/web/lib", "apps/bot/src"];
|
||
const KEYS_ONLY = ["packages/core/src", "packages/runner/src", "packages/mcp/src", "packages/shared/src"];
|
||
const CYR = /[А-Яа-яЁё]/;
|
||
|
||
function files(dir: string): string[] {
|
||
const out: string[] = [];
|
||
const abs = join(root, dir);
|
||
for (const name of readdirSync(abs)) {
|
||
if (name === "node_modules" || name.startsWith(".")) continue;
|
||
const p = join(abs, name);
|
||
if (statSync(p).isDirectory()) out.push(...files(relative(root, p)));
|
||
else if (/\.(ts|tsx)$/.test(name) && !name.endsWith(".d.ts") && !p.includes("/i18n/")) out.push(p);
|
||
}
|
||
return out;
|
||
}
|
||
|
||
const en = dictionary("en");
|
||
const missing = new Map<string, string>(); // key → first place
|
||
const loose: { where: string; text: string }[] = [];
|
||
|
||
function calleeName(e: ts.Expression): string {
|
||
if (ts.isIdentifier(e)) return e.text;
|
||
if (ts.isPropertyAccessExpression(e)) return `${calleeName(e.expression)}.${e.name.text}`;
|
||
return "";
|
||
}
|
||
|
||
const KEY_CALLS = /^(t|m|tr)$|\.(t|m)$/;
|
||
const PLURAL_CALLS = /^(tn)$|\.n$|^t\.n$/;
|
||
|
||
function check(file: string, strict: boolean) {
|
||
const src = readFileSync(file, "utf8");
|
||
if (!CYR.test(src) || src.includes("i18n-ignore-file")) return;
|
||
const lines = src.split("\n");
|
||
const sf = ts.createSourceFile(file, src, ts.ScriptTarget.Latest, true, file.endsWith("x") ? ts.ScriptKind.TSX : ts.ScriptKind.TS);
|
||
const rel = relative(root, file);
|
||
const ignored = (node: ts.Node) => {
|
||
const line = sf.getLineAndCharacterOfPosition(node.getStart()).line;
|
||
return (lines[line] ?? "").includes("i18n-ignore") || (lines[line - 1] ?? "").includes("i18n-ignore");
|
||
};
|
||
const keyArgs = new Set<ts.Node>();
|
||
const visitCalls = (node: ts.Node) => {
|
||
if (ts.isCallExpression(node)) {
|
||
const name = calleeName(node.expression);
|
||
const lit = node.arguments.filter((a) => ts.isStringLiteral(a) || ts.isNoSubstitutionTemplateLiteral(a));
|
||
if (KEY_CALLS.test(name) && lit[0]) keyArgs.add(lit[0]);
|
||
else if (PLURAL_CALLS.test(name)) lit.forEach((a) => keyArgs.add(a));
|
||
}
|
||
ts.forEachChild(node, visitCalls);
|
||
};
|
||
visitCalls(sf);
|
||
for (const k of keyArgs) {
|
||
const text = (k as ts.StringLiteral).text;
|
||
if (CYR.test(text) && en[text] === undefined && !missing.has(text)) missing.set(text, `${rel}:${sf.getLineAndCharacterOfPosition(k.getStart()).line + 1}`);
|
||
}
|
||
if (!strict) return;
|
||
const visit = (node: ts.Node) => {
|
||
let text: string | null = null;
|
||
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) text = node.text;
|
||
else if (ts.isTemplateExpression(node)) text = node.getText();
|
||
else if (ts.isJsxText(node)) text = node.text.trim();
|
||
if (text && CYR.test(text) && !keyArgs.has(node) && !ignored(node)) {
|
||
// Import paths and type-only positions never are user text.
|
||
if (!ts.isImportDeclaration(node.parent) && !ts.isLiteralTypeNode(node.parent)) {
|
||
loose.push({ where: `${rel}:${sf.getLineAndCharacterOfPosition(node.getStart()).line + 1}`, text: text.slice(0, 80) });
|
||
}
|
||
if (ts.isTemplateExpression(node)) return;
|
||
}
|
||
ts.forEachChild(node, visit);
|
||
};
|
||
visit(sf);
|
||
}
|
||
|
||
const only = process.argv.indexOf("--file");
|
||
if (only > 0) check(process.argv[only + 1]!, true);
|
||
else {
|
||
for (const d of STRICT) for (const f of files(d)) check(f, true);
|
||
for (const d of KEYS_ONLY) for (const f of files(d)) check(f, false);
|
||
}
|
||
|
||
if (process.argv.includes("--summary")) {
|
||
const per = new Map<string, number>();
|
||
for (const l of loose) {
|
||
const f = l.where.replace(/:\d+$/, "");
|
||
per.set(f, (per.get(f) ?? 0) + 1);
|
||
}
|
||
for (const [f, n] of [...per].sort((a, b) => b[1] - a[1])) console.log(`${String(n).padStart(4)} ${f}`);
|
||
console.log(`untranslated literals: ${loose.length}; keys without English: ${missing.size}`);
|
||
process.exit(0);
|
||
}
|
||
|
||
// PM decision: the product name is «Peppa FACTORY» in every language — never translated or transliterated.
|
||
const badName: string[] = [];
|
||
for (const locale of ["en", "kk"] as const) {
|
||
for (const [k, v] of Object.entries(dictionary(locale))) {
|
||
// PEP-105: the Russian key is the Russian UI text — it must not say «Пеппа» either.
|
||
if (locale === "en" && /Пепп(?:ой|а|ы|е|у)(?![а-яё])/.test(k)) badName.push(`ru: «${k.slice(0, 80)}»`);
|
||
if (/Пеппа|Пеппы|Пеппе|Пеппу|Пеппой/.test(v) || /\bPeppa\b(?! FACTORY)(?![-./])/.test(v.replace(/Peppa\.(?=["\s]|$)/g, "Peppa FACTORY."))) badName.push(`${locale}: «${k.slice(0, 60)}» → «${v.slice(0, 80)}»`);
|
||
}
|
||
}
|
||
for (const b of badName) console.log(`название не «Peppa FACTORY»: ${b}`);
|
||
if (badName.length && !process.argv.includes("--summary")) process.exitCode = 1;
|
||
for (const [k, where] of missing) console.log(`нет перевода: ${where} «${k}»`);
|
||
for (const l of loose) console.log(`не через словарь: ${l.where} «${l.text}»`);
|
||
if (missing.size || loose.length || badName.length) {
|
||
console.log(`\ni18n-check: ${missing.size} ключей без перевода, ${loose.length} строк не через словарь`);
|
||
process.exit(1);
|
||
}
|
||
console.log("i18n-check: ok");
|