Skip to content

Commit

Permalink
refactor(translate): update translation logic to process files indivi…
Browse files Browse the repository at this point in the history
…dually and change model name
  • Loading branch information
huangdijia committed Dec 28, 2024
1 parent f909888 commit 051f31b
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions bin/doc-translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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 += `<!-- {{${fullPath}}} -->\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(/<!--\s*\{\{(.*?)\}\}\s*-->/);

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);
translateFiles().catch(console.error);

0 comments on commit 051f31b

Please sign in to comment.