-
-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support auto translate to english #821
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ffebb3f
Support auto translate to english
huangdijia 54d74af
docs: update code block syntax in encryption documentation to use PHP
huangdijia fb884b7
fix: update translation command in GitHub Actions workflow
huangdijia f53d9bf
docs: update translation for cache documentation to improve clarity
huangdijia 85708e5
Update docs and translate
huangdijia c3fc727
ci: add dependency on translate-zh job for English translation
huangdijia b29705b
docs: update wording for clarity in cache and fast-paginate documenta…
huangdijia fa44465
Update docs and translate
huangdijia 20fe4e6
ci: add git pull command before translation job
huangdijia 0fdbc9a
ci: enhance translation workflow with environment setup and package i…
huangdijia 7717da1
ci: rename translation job from translate-zh to translate
huangdijia 067b9d7
ci: reorder steps in translation workflow for clarity
huangdijia 52205fb
ci: implement retry logic and batch processing for translation
huangdijia 0742887
docs: update comments in cache.md to provide Chinese translations
huangdijia 45e99ef
docs: update cache.md for improved clarity in reference section
huangdijia 4a45330
Update docs and translate
huangdijia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import OpenAI from "openai"; | ||
import { promises as fs } from 'fs'; | ||
import path from 'path'; | ||
|
||
const token = process.env["GITHUB_TOKEN"]; | ||
const endpoint = "https://models.inference.ai.azure.com"; | ||
const modelName = "o1-mini"; | ||
|
||
async function translateFiles() { | ||
const client = new OpenAI({ | ||
baseURL: endpoint, | ||
apiKey: token, | ||
dangerouslyAllowBrowser: true | ||
}); | ||
|
||
const docsPath = path.join(process.cwd(), 'docs/zh-cn'); | ||
|
||
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 processDirectory(fullPath); | ||
} else if (file.name.endsWith('.md')) { | ||
await translateFile(fullPath); | ||
} | ||
} | ||
} | ||
|
||
await processDirectory(docsPath); | ||
} | ||
|
||
translateFiles().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,70 @@ | ||
import OpenAI from "openai"; | ||
import { promises as fs } from 'fs'; | ||
import { readdir, readFile, writeFile, mkdir } from 'fs/promises'; | ||
import path from 'path'; | ||
|
||
const token = process.env["GITHUB_TOKEN"]; | ||
const endpoint = "https://models.inference.ai.azure.com"; | ||
const modelName = "o1-mini"; | ||
const endpoint = "https://api.deepseek.com"; | ||
const token = process.env["DEEPSEEK_API_KEY"]; | ||
const MAX_CONCURRENT = 5; // 最大并发数 | ||
const MAX_RETRIES = 3; // 最大重试次数 | ||
|
||
async function translateFiles() { | ||
const client = new OpenAI({ | ||
const openai = new OpenAI({ | ||
baseURL: endpoint, | ||
apiKey: token, | ||
dangerouslyAllowBrowser: true | ||
}); | ||
}); | ||
|
||
const docsPath = path.join(process.cwd(), 'docs/zh-cn'); | ||
|
||
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 }); | ||
async function translateWithRetry(content, retries = 0) { | ||
try { | ||
const completion = await openai.chat.completions.create({ | ||
messages: [ | ||
{ role: "system", content: "You are a professional translator. Translate the following Chinese markdown content to English. Keep all markdown formatting intact." }, | ||
{ role: "user", content: content } | ||
], | ||
model: "deepseek-chat", | ||
}); | ||
return completion.choices[0].message.content; | ||
} catch (error) { | ||
if (retries < MAX_RETRIES) { | ||
await new Promise(resolve => setTimeout(resolve, 1000 * (retries + 1))); | ||
return translateWithRetry(content, retries + 1); | ||
} | ||
throw error; | ||
} | ||
} | ||
|
||
for (const file of files) { | ||
const fullPath = path.join(dirPath, file.name); | ||
async function processFile(srcPath, destPath) { | ||
const destFolder = path.dirname(destPath); | ||
await mkdir(destFolder, { recursive: true }); | ||
|
||
const content = await readFile(srcPath, 'utf8'); | ||
const translatedContent = await translateWithRetry(content); | ||
const finalContent = translatedContent.replace(/\/zh-cn\//g, '/en/'); | ||
await writeFile(destPath, finalContent); | ||
console.log(`Translated: ${path.basename(srcPath)}`); | ||
} | ||
|
||
if (file.isDirectory()) { | ||
await processDirectory(fullPath); | ||
} else if (file.name.endsWith('.md')) { | ||
await translateFile(fullPath); | ||
} | ||
async function translateFiles(srcDir, destDir) { | ||
try { | ||
const files = await readdir(srcDir, { recursive: true }); | ||
const mdFiles = files.filter(file => file.endsWith('.md')); | ||
|
||
// 将文件分批处理 | ||
for (let i = 0; i < mdFiles.length; i += MAX_CONCURRENT) { | ||
const batch = mdFiles.slice(i, i + MAX_CONCURRENT); | ||
const promises = batch.map(file => { | ||
const srcPath = path.join(srcDir, file); | ||
const destPath = path.join(destDir, file); | ||
return processFile(srcPath, destPath).catch(error => { | ||
console.error(`Error translating ${file}:`, error); | ||
}); | ||
}); | ||
|
||
await Promise.all(promises); | ||
} | ||
|
||
console.log('All translations completed!'); | ||
} catch (error) { | ||
console.error('Translation error:', error); | ||
} | ||
} | ||
|
||
await processDirectory(docsPath); | ||
} | ||
|
||
translateFiles().catch(console.error); | ||
translateFiles('docs/zh-cn', 'docs/en'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
建议增加翻译步骤的错误处理
翻译步骤需要增加错误处理机制,确保翻译失败时能够及时发现并处理。
建议修改如下:
📝 Committable suggestion