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

完善翻译

parent 18983f23
...@@ -40,26 +40,30 @@ const translateText = (text, fromLang = 'zh', toLang = 'en') => { ...@@ -40,26 +40,30 @@ 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 = '';
res.on('data', (chunk) => { res.on('data', (chunk) => {
data += chunk; data += chunk;
});
res.on('end', () => {
console.log('翻译结果:', data);
resolve(JSON.parse(data).trans_result[0].dst)
});
}); });
res.on('end', () => { req.on('error', (err) => {
console.log('翻译结果:', data); console.error(err);
resolve(JSON.parse(data).trans_result[0].dst)
}); });
});
req.on('error', (err) => { req.write(querystring.stringify(params));
console.error(err); req.end();
}); }catch (e) {
console.log(text + " 翻译失败:" + e)
}
req.write(querystring.stringify(params));
req.end();
}) })
}; };
...@@ -96,10 +100,18 @@ async function translate(obj){ ...@@ -96,10 +100,18 @@ 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
const textEn = await translateText(text) try{
console.log(`${text} => ${textEn}\n`) const textEn = await translateText(text)
obj[text] = textEn console.log(`${text} => ${textEn}\n`)
autoTransCnt ++ obj[text] = textEn
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