48 lines
2.3 KiB
JavaScript
48 lines
2.3 KiB
JavaScript
|
|
// One-off: merge workflow localization output into messages/{fa,en}.json under "auto",
|
||
|
|
// then report any auto.* namespaces referenced in src/ but missing from messages (orphans
|
||
|
|
// from failed batches that edited files without returning keys).
|
||
|
|
const fs = require("fs");
|
||
|
|
const path = require("path");
|
||
|
|
const cp = require("child_process");
|
||
|
|
|
||
|
|
const ROOT = path.resolve(__dirname, "..");
|
||
|
|
const outFile = process.argv[2];
|
||
|
|
if (!outFile) { console.error("usage: node merge-i18n.js <workflow-output-file>"); process.exit(1); }
|
||
|
|
|
||
|
|
// 1. Extract the result JSON from the workflow output file (whole file is valid JSON).
|
||
|
|
const raw = fs.readFileSync(outFile, "utf8");
|
||
|
|
const parsed = JSON.parse(raw);
|
||
|
|
const result = parsed.result || parsed;
|
||
|
|
const localized = result.localized || [];
|
||
|
|
console.log(`workflow result: localized=${localized.length} skipped=${(result.skipped||[]).length}`);
|
||
|
|
|
||
|
|
// 2. Merge into messages, preserving existing keys; create "auto" namespace.
|
||
|
|
for (const locale of ["fa", "en"]) {
|
||
|
|
const file = path.join(ROOT, "messages", `${locale}.json`);
|
||
|
|
const msg = JSON.parse(fs.readFileSync(file, "utf8"));
|
||
|
|
msg.auto = msg.auto || {};
|
||
|
|
let added = 0;
|
||
|
|
for (const item of localized) {
|
||
|
|
if (!item.pathKey) continue;
|
||
|
|
const payload = locale === "fa" ? item.fa : item.en;
|
||
|
|
if (payload && typeof payload === "object") { msg.auto[item.pathKey] = payload; added++; }
|
||
|
|
}
|
||
|
|
fs.writeFileSync(file, JSON.stringify(msg, null, 2) + "\n");
|
||
|
|
console.log(`${locale}.json: merged ${added} namespaces (auto.* total=${Object.keys(msg.auto).length})`);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 3. Find auto.* namespaces referenced in src but missing from merged en.json → orphans.
|
||
|
|
const en = JSON.parse(fs.readFileSync(path.join(ROOT, "messages", "en.json"), "utf8"));
|
||
|
|
const present = new Set(Object.keys(en.auto || {}));
|
||
|
|
const grep = cp.spawnSync(
|
||
|
|
"grep",
|
||
|
|
["-rhoE", "(useTranslations|getTranslations)\\(\"auto\\.[a-zA-Z0-9]+\"", path.join(ROOT, "src")],
|
||
|
|
{ encoding: "utf8" }
|
||
|
|
);
|
||
|
|
const referenced = new Set();
|
||
|
|
for (const m of (grep.stdout || "").matchAll(/auto\.([a-zA-Z0-9]+)/g)) referenced.add(m[1]);
|
||
|
|
const orphans = [...referenced].filter((ns) => !present.has(ns));
|
||
|
|
console.log(`\nreferenced auto.* namespaces: ${referenced.size}`);
|
||
|
|
console.log(`ORPHANS (referenced but missing keys): ${orphans.length}`);
|
||
|
|
orphans.forEach((o) => console.log(" - auto." + o));
|