forked from zaphosting/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate.js
More file actions
164 lines (135 loc) · 4.32 KB
/
translate.js
File metadata and controls
164 lines (135 loc) · 4.32 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const { OpenAI } = require('openai');
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
const SOURCE_DIR = 'docs';
const TARGET_LANGUAGES = ['de', 'fr', 'es', 'ar', 'pt', 'th', 'pl', 'ja', 'it', 'sv', 'nl', 'zh'];
const CONCURRENCY = 5;
let totalPromptTokens = 0;
let totalCompletionTokens = 0;
let totalTokens = 0;
function buildGlossary(targetLang) {
const glossaryRaw = process.env.TRANSLATION_GLOSSARY;
if (!glossaryRaw) return "";
try {
const glossaryObj = JSON.parse(glossaryRaw);
if (!glossaryObj[targetLang]) return "";
const entries = Object.entries(glossaryObj[targetLang])
.map(([src, tgt]) => `- Translate "${src}" as "${tgt}"`)
.join("\n");
return `\n\nGlossary rules for ${targetLang}:\n${entries}`;
} catch (err) {
console.error("Glossary parsing failed:", err);
return "";
}
}
function buildSystemPrompt(targetLang) {
const fromSecret = process.env.TRANSLATION_PROMPT;
if (!fromSecret || !fromSecret.trim()) {
throw new Error("TRANSLATION_PROMPT secret is missing or empty!");
}
return fromSecret.replaceAll("${targetLang}", targetLang) + buildGlossary(targetLang);
}
function getChangedMarkdownFiles() {
try {
const output = execSync('git diff --name-only origin/master...HEAD', { encoding: 'utf8' });
return output
.split('\n')
.filter(file => file.startsWith(SOURCE_DIR) && file.endsWith('.md') && fs.existsSync(file));
} catch (error) {
return getAllMarkdownFiles();
}
}
function getAllMarkdownFiles() {
const output = execSync(
`find ${SOURCE_DIR} -name "*.md" -not -path "./node_modules/*" -not -path "./.git/*"`,
{ encoding: 'utf8' }
);
return output.split('\n').filter(file => file.trim() !== '');
}
async function translateContent(content, targetLang) {
const systemPrompt = buildSystemPrompt(targetLang);
const response = await openai.chat.completions.create({
model: 'gpt-4.1-mini',
messages: [
{
role: 'system',
content: systemPrompt
},
{
role: 'user',
content: content
}
],
temperature: 0.1
});
if (response.usage) {
totalPromptTokens += response.usage.prompt_tokens || 0;
totalCompletionTokens += response.usage.completion_tokens || 0;
totalTokens += response.usage.total_tokens || 0;
}
return response.choices[0].message.content;
}
async function runWithConcurrency(tasks, worker, concurrency) {
let index = 0;
async function next() {
const current = index++;
if (current >= tasks.length) return;
const task = tasks[current];
await worker(task);
await next();
}
const workers = [];
const workerCount = Math.min(concurrency, tasks.length);
for (let i = 0; i < workerCount; i++) {
workers.push(next());
}
await Promise.all(workers);
}
async function main() {
const changedFiles = getChangedMarkdownFiles();
console.log(`Found ${changedFiles.length} markdown files to translate`);
const tasks = [];
for (const file of changedFiles) {
const content = fs.readFileSync(file, 'utf8');
const relPath = path.relative(SOURCE_DIR, file);
for (const lang of TARGET_LANGUAGES) {
tasks.push({
file,
relPath,
lang,
content
});
}
}
console.log(`Created ${tasks.length} translation tasks`);
console.log(`Running with concurrency = ${CONCURRENCY}`);
await runWithConcurrency(
tasks,
async ({ file, relPath, lang, content }) => {
console.log(`Translating ${file} to ${lang}...`);
try {
const translatedContent = await translateContent(content, lang);
const outputFile = path.join(
'i18n',
lang,
'docusaurus-plugin-content-docs/current',
relPath
);
fs.mkdirSync(path.dirname(outputFile), { recursive: true });
fs.writeFileSync(outputFile, translatedContent);
} catch (error) {
console.error(`Error translating ${file} to ${lang}:`, error);
}
},
CONCURRENCY
);
console.log('\n--- Translation Token Usage ---');
console.log(`Prompt tokens: ${totalPromptTokens}`);
console.log(`Completion tokens: ${totalCompletionTokens}`);
console.log(`Total tokens: ${totalTokens}`);
}
main().catch(console.error);