Fix placeholders in machine translations and add Russian translation
Local Build and Deploy / deploy (push) Successful in 54s

This commit is contained in:
openhands committed 2026-07-12 22:37:52 +02:00
1 parent b5179c682f
commit 6ddfcafa67
19 files changed
+29430 -31739

No files matched your search

+114
View File
@@ -0,0 +1,114 @@
import { readFileSync, writeFileSync } from "fs";
import { resolve } from "path";
import { translate } from "@vitalets/google-translate-api";
const ROOT = resolve(import.meta.dirname, "..");
const SEP = "\n␟\n";
const TARGETS = [
{ code: "pt", to: "pt" }, { code: "pl", to: "pl" }, { code: "sv", to: "sv" },
{ code: "tr", to: "tr" }, { code: "ro", to: "ro" }, { code: "hu", to: "hu" },
{ code: "cs", to: "cs" }, { code: "sk", to: "sk" }, { code: "da", to: "da" },
{ code: "no", to: "no" }, { code: "el", to: "el" }, { code: "bg", to: "bg" },
{ code: "hr", to: "hr" }, { code: "sr", to: "sr" }, { code: "uk", to: "uk" },
{ code: "ru", to: "ru" },
];
function flatten(o, p) { const r={}; for(const[k,v]of Object.entries(o)){const f=p?`${p}.${k}`:k; typeof v==="object"&&v!==null&&!Array.isArray(v)?Object.assign(r,flatten(v,f)):r[f]=String(v??"");} return r; }
function unflatten(flat){const r={};for(const[k,v]of Object.entries(flat)){const p=k.split(".");let c=r;for(let i=0;i<p.length-1;i++){if(!c[p[i]]||typeof c[p[i]]!=="object")c[p[i]]={};c=c[p[i]];}c[p[p.length-1]]=v;}return r;}
// Replace all {balanced} groups with markers, return [text, [{marker, original}]]
function protect(text) {
const markers = [];
let result = "";
let depth = 0;
let start = -1;
let idx = 0;
for (let i = 0; i < text.length; i++) {
const ch = text[i];
if (ch === "{") {
if (depth === 0) start = i;
depth++;
} else if (ch === "}") {
depth--;
if (depth === 0 && start >= 0) {
const marker = `Ω${idx}Ω`;
markers.push({ marker, original: text.slice(start, i + 1) });
result += marker;
idx++;
start = -1;
continue;
}
}
if (depth === 0) result += ch;
}
return { text: result, markers };
}
function restore(text, markers) {
let result = text;
// Sort longest first
const sorted = [...markers].sort((a, b) => b.marker.length - a.marker.length);
for (const { marker, original } of sorted) {
result = result.replaceAll(marker, original);
}
return result;
}
async function main() {
const raw = JSON.parse(readFileSync(resolve(ROOT, "src/messages/en.json"), "utf-8"));
const flat = flatten(raw);
const keys = Object.keys(flat);
const vals = Object.values(flat);
console.log(`Source: en.json — ${keys.length} keys`);
// Protect all values
const protectedArr = vals.map(protect);
const allTexts = protectedArr.map((p) => p.text);
const allMarkers = protectedArr.map((p) => p.markers);
const batch = allTexts.join(SEP);
const batchLen = batch.length;
console.log(`Batch size: ${batchLen} chars`);
for (const { code, to } of TARGETS) {
const outPath = resolve(ROOT, "src/messages", `${code}.json`);
process.stdout.write(` ${code}... `);
try {
const { text } = await translate(batch, { to });
let parts = text.split(SEP);
if (parts.length !== keys.length) {
process.stdout.write(`split fail (${parts.length}), individual fallback... `);
parts = [];
for (let i = 0; i < keys.length; i++) {
try {
const { text: t } = await translate(allTexts[i], { to });
parts.push(t);
} catch {
parts.push(vals[i]);
}
await new Promise((r) => setTimeout(r, 200));
}
}
// Restore placeholders
const restored = parts.map((p, i) => restore(p, allMarkers[i]));
const flatResult = {};
keys.forEach((k, i) => { flatResult[k] = restored[i]; });
writeFileSync(outPath, JSON.stringify(unflatten(flatResult), null, 2) + "\n", "utf-8");
process.stdout.write(`✓\n`);
} catch (err) {
process.stdout.write(`FAIL: ${err.message.slice(0, 50)}\n`);
}
await new Promise((r) => setTimeout(r, 2000));
}
console.log("\nDone!");
}
main().catch(console.error);