-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (54 loc) · 2.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require("dotenv").config();
const fs = require("fs");
const openai = require("openai");
const openAi = new openai({ apiKey: process.env.API_KEY });
async function getTranslatedText(text, targetLanguage) {
try {
const res = await openAi.chat.completions.create({
model: "gpt-4o",
messages: [{
role: "user",
content: `Translate ‘${text}’ into ${targetLanguage} language with no extra text, only English translation`
}],
});
return res.choices[0].message.content;
} catch (error) {
console.error("Error:", error);
}
}
async function translateTexts(files) {
for (const file of files) {
const data = fs.readFileSync(file, "utf-8");
const texts = JSON.parse(data);
const translatedRu = {};
const translatedSp = {};
const translatedPo = {};
const translatedGe = {};
const translatedFr = {};
for (const key in texts) {
if (texts.hasOwnProperty(key)) {
const originalText = texts[key];
console.log(`Translating text with key ${key} in file ${file}...`);
const translatedToRu = await getTranslatedText(originalText, "russian");
const translatedToSp = await getTranslatedText(originalText, "spanish");
const translatedToPo = await getTranslatedText(originalText, "portuguese");
const translatedToGe = await getTranslatedText(originalText, "german");
const translatedToFr = await getTranslatedText(originalText, "french");
translatedRu[key] = translatedToRu || "Translation error";
translatedSp[key] = translatedToSp || "Translation error";
translatedPo[key] = translatedToPo || "Translation error";
translatedGe[key] = translatedToGe || "Translation error";
translatedFr[key] = translatedToFr || "Translation error";
}
}
const fileNameWithoutExtension = file.split(".")[0];
fs.writeFileSync(`${fileNameWithoutExtension}Ru.json`, JSON.stringify(translatedRu, null, 2), "utf-8");
fs.writeFileSync(`${fileNameWithoutExtension}Sp.json`, JSON.stringify(translatedSp, null, 2), "utf-8");
fs.writeFileSync(`${fileNameWithoutExtension}Po.json`, JSON.stringify(translatedPo, null, 2), "utf-8");
fs.writeFileSync(`${fileNameWithoutExtension}Ge.json`, JSON.stringify(translatedGe, null, 2), "utf-8");
fs.writeFileSync(`${fileNameWithoutExtension}Fr.json`, JSON.stringify(translatedFr, null, 2), "utf-8");
console.log(`Переводы для файла ${file} завершены и сохранены.`);
}
}
const filesToTranslate = ["texts.json", "texts2.json"];
translateTexts(filesToTranslate);