-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslate.js
134 lines (106 loc) · 3.87 KB
/
translate.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const fs = require('fs');
const path = require('path');
const targetDir = path.join(__dirname, 'docs');
const skipPattern = /\b(ru|pt-br)\b/;
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function isDirectory(filePath) {
return fs.statSync(filePath).isDirectory();
}
function enumerateMdFiles(dir) {
const files = fs.readdirSync(dir);
let mdFiles = [];
const excludePattern = /[\\\/](ru|cn|pt-br)[\\\/]/i;
for (const file of files) {
const filePath = path.join(dir, file);
if (isDirectory(filePath)) {
if (skipPattern.test(file)) {
continue;
}
mdFiles = mdFiles.concat(enumerateMdFiles(filePath));
} else if (file.endsWith('.md') || file.endsWith('.yml')) {
if (!excludePattern.test(filePath))
mdFiles.push(filePath);
}
}
return mdFiles;
}
async function translate(text, prompt) {
const apiKey = process.env.GROQ_API_KEY;
if (!apiKey) {
console.error("GROQ_API_KEY environment variable is not set.");
process.exit(1);
}
const response = await fetch('https://api.groq.com/openai/v1/chat/completions', {
method: 'POST',
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'gemma2-9b-it',
messages: [{
role: 'system',
content: prompt.trim().replace(/\s+/g, ' ')
}, {
role: 'user',
content: text
}],
temperature: 0.1,
top_p: 0.5,
max_tokens: 8192
})
});
if (!response.ok) {
console.error("Failed to translate the markdown file.");
console.error(await response.text());
process.exit(1);
}
const data = await response.json();
return data.choices[0].message.content;
}
const mdFiles = enumerateMdFiles(targetDir);
if (process.argv.length < 4) {
console.error("Usage: node translate.js <language> <dest>");
console.error("Also, make sure to have your groq api key at your GROQ_API_KEY environment variable.");
process.exit(1);
}
const toLanguage = process.argv[2];
const dest = process.argv[3];
const prompt = `
You're an translator AI helper. Your goal is to translate the given markdown code language
into another language. You should translate texts, code comments, but not code symbols or
variables. You should NOT translate markdown warning boxes tags. You must translate the input
text to ${toLanguage}. You must reply only with the translated text, no greetings or
anything. You should not translate markdown warning tags. You're translating a piece of
documentation of the Sisk Framework, an .NET web-server written in C#. You must preserve
links targets.
`;
(async () => {
var translatedCount = 0;
for (const mdFile of mdFiles) {
const fileContents = fs.readFileSync(mdFile, 'utf8');
const fileName = mdFile.replace(targetDir, '');
const translationPath = path.join(targetDir, dest, fileName);
const translationDir = path.dirname(translationPath);
if (fs.existsSync(translationPath)) {
continue;
}
const translated = await translate(fileContents, prompt);
fs.mkdirSync(translationDir, { recursive: true });
fs.writeFileSync(translationPath, translated);
console.log("Translated: ", fileName);
// wait 5s (rate-limit)
await sleep(10_000);
translatedCount++;
if (translatedCount % 5 === 0) {
await sleep(30_000);
}
}
if (translatedCount == 0) {
console.log("No files to translate.");
} else {
console.log(`${translatedCount} files translated.`);
}
})().then(console.log);