Files
descrybe/apps/web/scripts/build-phrase-extra.mjs
T

63 lines
2.1 KiB
JavaScript
Raw Normal View History

/**
* Build locale-extra-rest.mjs from English→locale phrase map for keys still English in non-es packs.
* Also covers new settings/dashboard strings for all locales including es.
*/
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { EXTRA as EXTRA_ES } from "./locale-extra-es.mjs";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
function parseMessageDict(source) {
const dict = {};
const re = /"([^"\\]+)"\s*:\s*((?:"(?:\\.|[^"\\])*")|(?:`(?:\\.|[^`\\])*`))/gs;
let m;
while ((m = re.exec(source))) {
const key = m[1];
const raw = m[2];
dict[key] = raw.startsWith("`")
? raw.slice(1, -1).replace(/\\`/g, "`").replace(/\\\$/g, "$").replace(/\\n/g, "\n")
: JSON.parse(raw);
}
return dict;
}
const en = parseMessageDict(
fs.readFileSync(path.resolve(__dirname, "../src/lib/i18n/messages/en.ts"), "utf8")
);
/** English source string → { fr, de, it, pt, nl, pl, ja, es? } */
const BY_EN = JSON.parse(fs.readFileSync(path.join(__dirname, "phrase-map.json"), "utf8"));
const codes = ["es", "fr", "de", "it", "pt", "nl", "pl", "ja"];
const EXTRA = Object.fromEntries(codes.map((c) => [c, {}]));
// 1) Seed non-es from Spanish extras via English lookup of the same key
for (const [key, esText] of Object.entries(EXTRA_ES.es ?? {})) {
const enText = en[key];
if (!enText) continue;
EXTRA.es[key] = esText;
const mapped = BY_EN[enText];
if (!mapped) continue;
for (const code of ["fr", "de", "it", "pt", "nl", "pl", "ja"]) {
if (mapped[code]) EXTRA[code][key] = mapped[code];
}
}
// 2) Apply phrase map to every en key (fills new settings/dashboard too)
for (const [key, enText] of Object.entries(en)) {
const mapped = BY_EN[enText];
if (!mapped) continue;
for (const code of codes) {
if (mapped[code]) EXTRA[code][key] = mapped[code];
}
}
const out = `/** Auto-built by build-phrase-extra.mjs — do not hand-edit; update phrase-map.json. */\nexport const EXTRA = ${JSON.stringify(EXTRA, null, 2)};\n`;
fs.writeFileSync(path.join(__dirname, "locale-extra-rest.mjs"), out, "utf8");
for (const code of codes) {
console.log(code, Object.keys(EXTRA[code]).length);
}