diff --git a/lib/translate/tool.js b/lib/translate/tool.js index 3dc001e2e0..56ef4e68a0 100644 --- a/lib/translate/tool.js +++ b/lib/translate/tool.js @@ -11,6 +11,7 @@ const enableEn = args.includes("-e"); const inputFile = args.includes("-i") ? args[args.indexOf("-i") + 1] : ""; const uuid = args.includes("-u"); const pure = args.includes("-p"); +const ai = args.includes("--ai") ? args[args.indexOf("--ai") + 1] : ""; const source = require(path.join(rootDir, "./source.json")); @@ -33,7 +34,10 @@ const srcCommit = { "zh-HANT": { id: "eff1861", finishId: [] }, // 繁体中文 }; -if (lang) { +/** + * @param {string} lang + */ +function getToTranslate(lang) { let l = {}; try { l = require(path.join(rootDir, `./${lang}.json`)); @@ -52,6 +56,7 @@ if (lang) { } catch (error) {} const t = []; + const tt = []; const w = args.includes("-a"); @@ -62,6 +67,7 @@ if (lang) { t.push( `"${source[i]}","${i}","${enLan[source[i]] || ""}","${pure ? "" : l[source[i]] || ""}"`, ); + tt.push(i); } if (!w) { @@ -94,6 +100,11 @@ if (lang) { fs.writeFileSync(csvFilePath, csv); console.log(csvFilePath); + return tt; +} + +if (lang) { + getToTranslate(lang); } if (inputFile) { @@ -130,3 +141,68 @@ if (uuid) { } fs.writeFileSync("source.json", `${JSON.stringify(source, null, 4)}\n`); } + +if (ai) { + const tt = getToTranslate(ai); + console.log(tt); + fetch("https://api.chatanywhere.tech/v1/chat/completions", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${process.env.gpt}`, + }, + body: JSON.stringify({ + model: "gpt-4o-mini", + stream: false, + messages: [ + { + role: "system", + content: "你将翻译提供的JSON,不需要任何解释", + }, + { + role: "user", + content: `把下面这些JSON翻译成 en\n${JSON.stringify(["hello", "bye"])}`, + }, + { + role: "assistant", + content: `${JSON.stringify(["你好", "再见"])}`, + }, + { + role: "user", + content: `把下面这些JSON翻译成 ${ai}\n${JSON.stringify(tt)}`, + }, + ], + }), + }) + .then((res) => res.json()) + .then((data) => { + console.log(data.choices[0].message); + const x = JSON.parse(data.choices[0].message.content); + const v = {}; + for (const i in x) { + const zh = tt[i]; + if (!source[zh]) { + console.log(`no ${zh}`); + continue; + } + v[source[zh]] = x[i]; + } + + let l = {}; + try { + l = require(path.join(rootDir, `./${ai}.json`)); + } catch (error) { + console.log("\x1B[32m%s\x1B[0m", `+ ${ai}`); + } + + const out = {}; + for (const i in source) { + const id = source[i]; + out[id] = v[id] || l[id] || ""; + } + fs.writeFileSync( + `${ai}.json`, + `${JSON.stringify(out, null, 4)}\n`, + ); + }); +}