diff --git a/bin/doc-translate.js b/bin/doc-translate.js index 362998bb2..bc0f12968 100644 --- a/bin/doc-translate.js +++ b/bin/doc-translate.js @@ -4,7 +4,7 @@ import path from 'path'; const token = process.env["GITHUB_TOKEN"]; const endpoint = "https://models.inference.ai.azure.com"; -const modelName = "gpt-4o"; +const modelName = "o1-mini"; async function translateFiles() { const client = new OpenAI({ @@ -14,43 +14,39 @@ async function translateFiles() { }); const docsPath = path.join(process.cwd(), 'docs/zh-cn'); - let combinedContent = ''; - async function combineFiles(dirPath) { + async function translateFile(filePath) { + const content = await fs.readFile(filePath, 'utf8'); + const response = await client.chat.completions.create({ + messages: [ + { role: "user", content: "You are a professional translator. Translate the following Markdown content from Chinese to English. Preserve all Markdown formatting." }, + { role: "user", content: content } + ], + model: modelName + }); + + const translatedContent = response.choices[0].message.content; + const englishPath = filePath.replace('/zh-cn/', '/en/'); + await fs.mkdir(path.dirname(englishPath), { recursive: true }); + await fs.writeFile(englishPath, translatedContent); + console.log(`Translated: ${filePath} -> ${englishPath}`); + } + + async function processDirectory(dirPath) { const files = await fs.readdir(dirPath, { withFileTypes: true }); for (const file of files) { const fullPath = path.join(dirPath, file.name); if (file.isDirectory()) { - await combineFiles(fullPath); + await processDirectory(fullPath); } else if (file.name.endsWith('.md')) { - const content = await fs.readFile(fullPath, 'utf8'); - combinedContent += `\n${content}\n\n`; + await translateFile(fullPath); } } } - await combineFiles(docsPath); - - const response = await client.chat.completions.create({ - messages: [ - { role: "system", content: "You are a professional translator. Translate the following Markdown content from Chinese to English. Preserve all Markdown formatting." }, - { role: "user", content: combinedContent } - ], - model: modelName - }); - - const translatedContent = response.choices[0].message.content; - const sections = translatedContent.split(//); - - for (let i = 1; i < sections.length; i += 2) { - const filePath = sections[i]; - const content = sections[i + 1].trim(); - const englishPath = filePath.replace('/zh-cn/', '/en/'); - await fs.mkdir(path.dirname(englishPath), { recursive: true }); - await fs.writeFile(englishPath, content); - } + await processDirectory(docsPath); } -translateFiles().catch(console.error); \ No newline at end of file +translateFiles().catch(console.error);