99 lines
3.8 KiB
JavaScript
99 lines
3.8 KiB
JavaScript
import { readdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
|
|
const RAW_KEY_ENGLISH = {
|
|
cancel: "Cancel",
|
|
canvasRatio: "Canvas ratio",
|
|
clipDuration: "Clip duration",
|
|
clipStart: "Clip start",
|
|
layer: "Layer",
|
|
lock: "Lock",
|
|
visualBasic: "Basic",
|
|
};
|
|
|
|
const TARGET_CODES = { zh: "zh-CN", en: "en", ja: "ja", ko: "ko", es: "es", fr: "fr", de: "de", pt: "pt", th: "th", vi: "vi", ru: "ru" };
|
|
const keys = new Set();
|
|
|
|
globalThis.__GENERATING_I18N__ = true;
|
|
const { APP_LANGUAGES, UI_COPY, createTranslator } = await import("../src/i18n.js");
|
|
for (const key of Object.keys(UI_COPY.en)) keys.add(key);
|
|
|
|
async function collectKeys(directory) {
|
|
for (const name of await readdir(directory)) {
|
|
const path = join(directory, name);
|
|
const info = await stat(path);
|
|
if (info.isDirectory()) await collectKeys(path);
|
|
else if (/\.(?:js|jsx)$/.test(name) && !/\.test\.[^.]+$/.test(name) && name !== "i18nCompletion.js") {
|
|
const source = await readFile(path, "utf8");
|
|
for (const match of source.matchAll(/\bt\(\s*["']([^"']+)["']/g)) keys.add(match[1]);
|
|
}
|
|
}
|
|
}
|
|
|
|
function chunksFor(entries, maximumLength = 3500) {
|
|
const chunks = [];
|
|
let current = [];
|
|
let length = 0;
|
|
for (const entry of entries) {
|
|
const line = `@@${entry.index}@@ ${entry.text.replace(/\s+/g, " ").trim()}`;
|
|
if (current.length && length + line.length + 1 > maximumLength) {
|
|
chunks.push(current);
|
|
current = [];
|
|
length = 0;
|
|
}
|
|
current.push({ ...entry, line });
|
|
length += line.length + 1;
|
|
}
|
|
if (current.length) chunks.push(current);
|
|
return chunks;
|
|
}
|
|
|
|
async function translateChunk(chunk, target) {
|
|
const url = new URL("https://translate.googleapis.com/translate_a/single");
|
|
url.searchParams.set("client", "gtx");
|
|
url.searchParams.set("sl", "en");
|
|
url.searchParams.set("tl", target);
|
|
url.searchParams.set("dt", "t");
|
|
url.searchParams.set("q", chunk.map(({ line }) => line).join("\n"));
|
|
const response = await fetch(url);
|
|
if (!response.ok) throw new Error(`Translation failed: ${response.status}`);
|
|
const payload = await response.json();
|
|
const translated = payload[0].map((part) => part[0]).join("");
|
|
const values = new Map();
|
|
for (const match of translated.matchAll(/@@(\d+)@@\s*([\s\S]*?)(?=\s*@@\d+@@|$)/g)) {
|
|
values.set(Number(match[1]), match[2].trim());
|
|
}
|
|
for (const entry of chunk) {
|
|
if (!values.has(entry.index)) throw new Error(`Missing translated entry K${entry.index} for ${target}`);
|
|
}
|
|
return values;
|
|
}
|
|
|
|
await collectKeys(new URL("../src", import.meta.url).pathname);
|
|
const sortedKeys = [...keys].sort();
|
|
const english = createTranslator("en");
|
|
const output = {};
|
|
|
|
for (const { id } of APP_LANGUAGES) {
|
|
const current = createTranslator(id);
|
|
const entries = sortedKeys.flatMap((key, index) => {
|
|
const englishText = english(key) === key ? RAW_KEY_ENGLISH[key] : english(key);
|
|
if (!englishText) throw new Error(`No English source text for ${key}`);
|
|
const needsCompletion = current(key) === key || (id !== "en" && current(key) === english(key));
|
|
return needsCompletion ? [{ key, index, text: englishText }] : [];
|
|
});
|
|
output[id] = {};
|
|
if (id === "en") {
|
|
for (const entry of entries) output[id][entry.key] = entry.text;
|
|
continue;
|
|
}
|
|
for (const chunk of chunksFor(entries)) {
|
|
const translated = await translateChunk(chunk, TARGET_CODES[id]);
|
|
for (const entry of chunk) output[id][entry.key] = translated.get(entry.index);
|
|
}
|
|
process.stdout.write(`${id}: ${entries.length} completed\n`);
|
|
}
|
|
|
|
const source = `// Generated by scripts/generate-i18n-completion.mjs.\n// Regenerate after adding user-visible t(\"…\") keys.\nexport const I18N_COMPLETION_COPY = ${JSON.stringify(output, null, 2)};\n`;
|
|
await writeFile(new URL("../src/i18nCompletion.js", import.meta.url), source);
|