Commit 089e73bd authored by dragondean@qq.com's avatar dragondean@qq.com

完善翻译

parent 18983f23
...@@ -40,7 +40,7 @@ const translateText = (text, fromLang = 'zh', toLang = 'en') => { ...@@ -40,7 +40,7 @@ const translateText = (text, fromLang = 'zh', toLang = 'en') => {
'Content-Length': querystring.stringify(params).length 'Content-Length': querystring.stringify(params).length
} }
}; };
try{
const req = https.request(options, (res) => { const req = https.request(options, (res) => {
let data = ''; let data = '';
...@@ -60,6 +60,10 @@ const translateText = (text, fromLang = 'zh', toLang = 'en') => { ...@@ -60,6 +60,10 @@ const translateText = (text, fromLang = 'zh', toLang = 'en') => {
req.write(querystring.stringify(params)); req.write(querystring.stringify(params));
req.end(); req.end();
}catch (e) {
console.log(text + " 翻译失败:" + e)
}
}) })
}; };
...@@ -96,11 +100,19 @@ async function translate(obj){ ...@@ -96,11 +100,19 @@ async function translate(obj){
// 对未翻译内容调用百度翻译进行翻译操作,有变量的不做处理 // 对未翻译内容调用百度翻译进行翻译操作,有变量的不做处理
for(let text in obj){ for(let text in obj){
if(obj[text] || text.includes('{'))continue if(obj[text] || text.includes('{'))continue
try{
const textEn = await translateText(text) const textEn = await translateText(text)
console.log(`${text} => ${textEn}\n`) console.log(`${text} => ${textEn}\n`)
obj[text] = textEn obj[text] = textEn
autoTransCnt ++ autoTransCnt ++
// 每次翻译后保存一次,防止意外退出导致的数据丢失
fs.writeFileSync(savePath, JSON.stringify(obj, null, 4))
}catch (e) {
console.log(text + " 翻译失败:" + e)
}
// 账号请求频率限制1ps // 账号请求频率限制1ps
await new Promise(resolve => setTimeout(resolve, 1000)) await new Promise(resolve => setTimeout(resolve, 1000))
} }
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment