Fix placeholders in machine translations and add Russian translation
Local Build and Deploy / deploy (push) Successful in 54s
Local Build and Deploy / deploy (push) Successful in 54s
This commit is contained in:
1 parent
b5179c682f
commit
6ddfcafa67
19 files changed
+29430
-31739
No files matched your search
@@ -68,6 +68,7 @@
|
||||
"@types/react": "^19.2.0",
|
||||
"@types/react-dom": "^19.2.0",
|
||||
"@types/sanitize-html": "^2.16.1",
|
||||
"@vitalets/google-translate-api": "^9.2.1",
|
||||
"dotenv": "^16.4.0",
|
||||
"eslint": "^10.6.0",
|
||||
"eslint-config-prettier": "^10.1.8",
|
||||
|
||||
Generated
+3480
-5872
File diff suppressed because it is too large.
Load diff
@@ -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);
|
||||
+1699
-1701
File diff suppressed because it is too large.
Load diff
+1693
-1695
File diff suppressed because it is too large.
Load diff
+1511
-1513
File diff suppressed because it is too large.
Load diff
+1684
-1686
File diff suppressed because it is too large.
Load diff
+1654
-1656
File diff suppressed because it is too large.
Load diff
+1689
-1691
File diff suppressed because it is too large.
Load diff
+1629
-1631
File diff suppressed because it is too large.
Load diff
+1723
-1725
File diff suppressed because it is too large.
Load diff
+1695
-1697
File diff suppressed because it is too large.
Load diff
+1651
-1653
File diff suppressed because it is too large.
Load diff
+846
-848
File diff suppressed because it is too large.
Load diff
+1681
-1683
File diff suppressed because it is too large.
Load diff
+1607
-1609
File diff suppressed because it is too large.
Load diff
+1642
-1644
File diff suppressed because it is too large.
Load diff
+1728
-1730
File diff suppressed because it is too large.
Load diff
+1703
-1705
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user