/** * The glossary of coined terms as files Weblate imports (issue #112). The source is the tables in * §6 of docs/guides/translations-weblate.md; this writes * * docs/i18n-review/glossary.csv one row per term, one column per language * docs/i18n-review/glossary/.csv Weblate's CSV with a header (source, target, * developer_comments), one file per language, for the * project glossary's Upload * * node scripts/i18n-glossary.ts writes them * node scripts/i18n-glossary.ts --check fails when they differ from the guide * * A term changes in the guide, in the same PR as the keys that use it; then run this. */ import { mkdirSync, readFileSync, writeFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); const GUIDE = path.join(ROOT, "docs/guides/translations-weblate.md"); const OUT = path.join(ROOT, "docs/i18n-review"); export interface GlossaryRow { section: string; /** The English term (Weblate's source). */ term: string; /** locale → the rendering the catalogs use. */ values: Record; } /** Product and technical names every language keeps as they are (§6, general rules). */ export const KEEP = [ "Cloudhal", "Studio", "MCP", "OAuth", "URL", "JSON", "Markdown", "Snapshot", "Release", "SEO", "sitemap.xml", ]; /** Columns that hold several terms, ` / `-separated, in this order. */ const SPLIT: Record = { dates: ["is before", "is on or before", "is after", "is on or after"], numbers: ["is less than", "is at most", "is greater than", "is at least"], }; const cells = (line: string) => line .trim() .slice(1, -1) .split("|") .map((c) => c.trim()); /** `Leave lists (unlist)` → `Leave lists`; a parenthesis of several words stays part of the term. */ function termOf(header: string): string { const open = header.lastIndexOf(" ("); if (open < 0 || !header.endsWith(")")) return header; const inner = header.slice(open + 2, -1); return inner.includes(" ") ? header : header.slice(0, open); } /** One language's row of a §6 table, added to `rows` term by term. */ function addLanguage( rows: GlossaryRow[], { section, header, row }: { section: string; header: string[]; row: string[] }, ) { const locale = row[0]?.replaceAll("`", "") ?? ""; for (let i = 1; i < header.length; i++) { const name = header[i] ?? ""; const terms = SPLIT[name] ?? [termOf(name)]; const values = terms.length > 1 ? (row[i] ?? "").split(" / ") : [row[i] ?? ""]; if (values.length !== terms.length) throw new Error(`${section}: ${locale} has ${values.length} forms for ${name}`); terms.forEach((term, j) => { let entry = rows.find((r) => r.section === section && r.term === term); if (!entry) { entry = { section, term, values: {} }; rows.push(entry); } entry.values[locale] = values[j] as string; }); } } /** The §6 tables of the guide as rows; throws when a table does not have the expected shape. */ export function glossaryRows(markdown: string): GlossaryRow[] { const start = markdown.indexOf("## 6. Glossary"); const end = markdown.indexOf("### Open term questions", start); if (start < 0 || end < 0) throw new Error("no §6 glossary in the guide"); const rows: GlossaryRow[] = []; let section = ""; let header: string[] | undefined; for (const line of markdown.slice(start, end).split("\n")) { if (line.startsWith("### ")) { section = line.slice(4).trim(); header = undefined; continue; } if (!line.startsWith("|")) { header = undefined; continue; } const row = cells(line); if (!header) { header = row; if (row[0] !== "Language") throw new Error(`${section}: first column is not Language`); continue; } if (!row[0]?.startsWith("---")) addLanguage(rows, { section, header, row }); } const locales = Object.keys(rows[0]?.values ?? {}); for (const term of KEEP) rows.push({ section: "Keep as is", term, values: Object.fromEntries(locales.map((l) => [l, term])), }); return rows; } const field = (value: string) => /[",\n]/.test(value) ? `"${value.replaceAll('"', '""')}"` : value; const csv = (lines: string[][]) => `${lines.map((l) => l.map(field).join(",")).join("\n")}\n`; /** The one-file view: section, English term, one column per language. */ export function wideCsv(rows: GlossaryRow[]): string { const locales = Object.keys(rows[0]?.values ?? {}); return csv([ ["section", "en", ...locales], ...rows.map((r) => [r.section, r.term, ...locales.map((l) => r.values[l] ?? "")]), ]); } /** Weblate's CSV for one language (formats/csv: header row, source and target). */ export function weblateCsv(rows: GlossaryRow[], locale: string): string { return csv([ ["source", "target", "developer_comments"], ...rows.map((r) => [ r.term, r.values[locale] ?? "", r.section === "Keep as is" ? "Keep as is: a product or technical name (translations-weblate.md §6)" : `Coined term, ${r.section} (translations-weblate.md §6)`, ]), ]); } /** Every file this writes, relative to docs/i18n-review/, with its content. */ export function glossaryFiles(markdown: string): Map { const rows = glossaryRows(markdown); const files = new Map([["glossary.csv", wideCsv(rows)]]); for (const locale of Object.keys(rows[0]?.values ?? {})) files.set(`glossary/${locale}.csv`, weblateCsv(rows, locale)); return files; } if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { const files = glossaryFiles(readFileSync(GUIDE, "utf8")); if (process.argv.includes("--check")) { const stale = [...files].filter(([rel, text]) => { try { return readFileSync(path.join(OUT, rel), "utf8") !== text; } catch { return true; } }); if (stale.length) { console.error( `Stale glossary files: ${stale.map(([r]) => r).join(", ")}\nRun node scripts/i18n-glossary.ts`, ); process.exit(1); } } else { mkdirSync(path.join(OUT, "glossary"), { recursive: true }); for (const [rel, text] of files) writeFileSync(path.join(OUT, rel), text); console.log(`wrote ${files.size} glossary files to ${path.relative(ROOT, OUT)}`); } }