/** * The `i18n` branch Hosted Weblate reads and writes (issue #112, ADR-0034 amendment of * 2026-11-29, docs/guides/translations-weblate.md §3). The branch is an orphan that holds only * `pnpm i18n:export` output under `i18n/` (`i18n//.json`, `i18n/descriptions.json`) * and a README; `main` stays the source of truth and only ever changes through a pull request. * * node scripts/i18n-branch.ts sync --dir [--ref origin/i18n] * main → branch: writes main's export into /i18n/, keeping every reviewer edit the * branch has that main has not imported yet. The caller commits with an * `I18n-Source:
` trailer, which marks the merge base for the next run. * node scripts/i18n-branch.ts stage [--ref origin/i18n] [--out .i18n] [--summary ] * branch → main: writes main's export to with the branch's edits merged in, for * `pnpm i18n:import`, which checks them and rewrites the catalogs. * node scripts/i18n-branch.ts check-pr --head [--ref origin/i18n] * fails unless the Weblate pull request at changes only existing translation files * (`i18n-branch/pr-check.ts`). It reads git objects only and runs none of the PR's code. * * Both use the per-key three-way merge in `i18n-branch/merge.ts`; the base is the branch as the * last sync commit left it. `.github/workflows/i18n-branch.yml` runs both. */ import { execFileSync } from "node:child_process"; import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync, } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { LOCALE_LIST } from "../apps/studio/src/i18n/locales.ts"; import { type MergeResult, mergeCatalog, type Tree } from "./i18n-branch/merge.ts"; import { changeProblems, jsonProblem, parseRawDiff } from "./i18n-branch/pr-check.ts"; const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); /** The commit trailer a sync writes; the newest commit carrying it is the next merge base. */ export const SOURCE_TRAILER = "I18n-Source:"; const git = (...args: string[]) => execFileSync("git", args, { cwd: ROOT, encoding: "utf8", maxBuffer: 256 * 1024 * 1024 }); /** `i18n//.json` at `ref`, keyed `/.json`; undefined without the ref. */ export function readRef(ref: string | undefined): Map | undefined { if (!ref) return undefined; try { git("rev-parse", "--verify", "--quiet", `${ref}^{commit}`); } catch { return undefined; } const files = new Map(); for (const name of git("ls-tree", "-r", "--name-only", ref, "--", "i18n/").split("\n")) { const rel = name.slice("i18n/".length); if (!name.endsWith(".json") || rel.split("/").length !== 2) continue; files.set(rel, JSON.parse(git("show", `${ref}:${name}`)) as Tree); } return files; } /** The newest sync commit on `ref`, or undefined (a branch no sync has written yet). */ export function lastSync(ref: string): string | undefined { try { return git("log", "-1", "--format=%H", `--grep=^${SOURCE_TRAILER}`, ref).trim() || undefined; } catch { return undefined; } } /** main's catalogs as `pnpm i18n:export` writes them. */ function exportMain(out: string) { execFileSync( process.execPath, [path.join(ROOT, "scripts/i18n-exchange.ts"), "export", "--out", out], { cwd: ROOT, stdio: "ignore", }, ); } const json = (tree: unknown) => `${JSON.stringify(tree, null, 2)}\n`; const readTree = (file: string) => JSON.parse(readFileSync(file, "utf8")) as Tree; interface Merged { /** `/.json` → merged messages, every language but English. */ files: Map; fromBranch: string[]; conflicts: string[]; } /** main's export in `exported`, merged with the branch at `ref`. */ export function mergeExport(exported: string, ref: string): Merged { const theirs = readRef(ref); const sync = theirs ? lastSync(ref) : undefined; const base = readRef(sync); const merged: Merged = { files: new Map(), fromBranch: [], conflicts: [] }; const english = new Map( readdirSync(path.join(exported, "en")).map((f) => [f, readTree(path.join(exported, "en", f))]), ); for (const locale of readdirSync(exported)) { if (locale === "en" || !existsSync(path.join(exported, locale, "core.json"))) continue; for (const [file, en] of english) { const rel = `${locale}/${file}`; const own = path.join(exported, rel); if (!existsSync(own)) continue; const result: MergeResult = mergeCatalog({ english: en, ours: readTree(own), theirs: theirs?.get(rel), base: base?.get(rel), }); merged.files.set(rel, result.tree); const label = `${locale}:${file.slice(0, -".json".length)}:`; merged.fromBranch.push(...result.fromBranch.map((k) => label + k)); merged.conflicts.push(...result.conflicts.map((k) => label + k)); } } return merged; } const BRANCH_README = `# Cloudhal Studio translations (the \`i18n\` branch) Hosted Weblate reads and writes this branch (issue #112, ADR-0034). It holds only \`pnpm i18n:export\` output: \`i18n/en/.json\` is the English source, \`i18n//.json\` one language, \`i18n/descriptions.json\` each key's English text and translator note. Do not edit it by hand or open pull requests against it: translate on Weblate, or follow docs/guides/translations-weblate.md on \`main\`. Weblate has no write access; its pull request from the \`weblate\` user's fork is the only one merged here, by a workflow on \`main\` (.github/workflows/i18n-branch.yml), after a check that it changes only translation files. The same workflow writes main's catalogs here and brings Weblate's work back to \`main\` through a pull request that runs \`pnpm i18n:import\` and its checks. `; /** main → branch: writes the merged export into `/i18n/`. */ function sync(dir: string, ref: string) { const tmp = mkdtempSync(path.join(tmpdir(), "cloudhal-i18n-")); try { exportMain(tmp); const merged = mergeExport(tmp, ref); const target = path.join(dir, "i18n"); for (const locale of readdirSync(tmp)) { const from = path.join(tmp, locale); if (!existsSync(path.join(from, "core.json"))) continue; mkdirSync(path.join(target, locale), { recursive: true }); const known = new Set(readdirSync(from)); // a catalog file main no longer has goes; a language main does not know is left alone for (const f of readdirSync(path.join(target, locale))) if (f.endsWith(".json") && !known.has(f)) rmSync(path.join(target, locale, f)); for (const f of known) { const tree = merged.files.get(`${locale}/${f}`) ?? readTree(path.join(from, f)); writeFileSync(path.join(target, locale, f), json(tree)); } } writeFileSync( path.join(target, "descriptions.json"), readFileSync(path.join(tmp, "descriptions.json")), ); writeFileSync(path.join(dir, "README.md"), BRANCH_README); report(merged, "kept from the branch (not yet imported into main)"); } finally { rmSync(tmp, { recursive: true, force: true }); } } /** branch → main: writes the merged export to `out` for `pnpm i18n:import`. */ function stage(out: string, ref: string, summary: string | undefined) { if (!readRef(ref)) throw new Error(`no ${ref}: fetch the i18n branch first (git fetch origin i18n)`); rmSync(out, { recursive: true, force: true }); exportMain(out); const merged = mergeExport(out, ref); for (const [rel, tree] of merged.files) writeFileSync(path.join(out, rel), json(tree)); report(merged, "taken from the branch"); if (summary) writeFileSync(summary, summaryOf(merged, ref)); } function report(merged: Merged, what: string) { console.log(`${merged.fromBranch.length} keys ${what}`); if (merged.conflicts.length) console.log( `${merged.conflicts.length} keys changed on both sides since the last sync; the branch (Weblate) value was kept:\n ${merged.conflicts.join("\n ")}`, ); } /** The import pull request's body: which keys come from Weblate, per language. */ export function summaryOf(merged: Pick, ref: string): string { const byLocale = new Map(); for (const k of merged.fromBranch) { const locale = k.slice(0, k.indexOf(":")); byLocale.set(locale, (byLocale.get(locale) ?? 0) + 1); } const rows = [...byLocale].map(([l, n]) => `| \`${l}\` | ${n} |`); const lines = [ `Translations from Weblate (\`${ref}\`), merged into main's catalogs by \`node scripts/i18n-branch.ts stage\` and imported with \`pnpm i18n:import\` (ADR-0034, #112).`, "", "Weblate's approvals stay on Weblate: this PR changes no language's `reviewed` flag.", "", "| Language | Keys from Weblate |", "|---|---|", ...rows, ]; if (merged.conflicts.length) lines.push( "", "Changed on main and on Weblate since the last sync; the Weblate value was kept, check it:", "", ...merged.conflicts.map((k) => `- \`${k}\``), ); return `${lines.join("\n")}\n`; } /** Why the pull request at `head` may not be merged into `base` (empty: it may). */ export function prProblems(base: string, head: string): string[] { // three dots: what the PR changes since it left `base`, not what `base` gained meanwhile const changes = parseRawDiff(git("diff", "--raw", "-z", "--no-renames", `${base}...${head}`)); const problems = changeProblems(changes, LOCALE_LIST); if (problems.length) return problems; for (const c of changes) { const problem = jsonProblem(c.path, git("show", `${head}:${c.path}`)); if (problem) problems.push(problem); } return problems; } if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { const [command] = process.argv.slice(2); const arg = (name: string) => { const i = process.argv.indexOf(`--${name}`); return i > 0 ? process.argv[i + 1] : undefined; }; const ref = arg("ref") ?? "origin/i18n"; if (command === "sync" && arg("dir")) sync(path.resolve(arg("dir") as string), ref); else if (command === "stage") stage(path.resolve(ROOT, arg("out") ?? ".i18n"), ref, arg("summary")); else if (command === "check-pr" && arg("head")) { const problems = prProblems(ref, arg("head") as string); if (problems.length) { console.error(`Not merged into i18n:\n ${problems.join("\n ")}`); process.exit(1); } console.log("only translation files change"); } else { console.error( "usage: i18n-branch.ts sync --dir [--ref origin/i18n]\n" + " i18n-branch.ts stage [--ref origin/i18n] [--out .i18n] [--summary ]\n" + " i18n-branch.ts check-pr --head [--ref origin/i18n]", ); process.exit(2); } }