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') => {
'Content-Length': querystring.stringify(params).length
}
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
try{
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log('翻译结果:', data);
resolve(JSON.parse(data).trans_result[0].dst)
});
});
res.on('end', () => {
console.log('翻译结果:', data);
resolve(JSON.parse(data).trans_result[0].dst)
req.on('error', (err) => {
console.error(err);
});
});
req.on('error', (err) => {
console.error(err);
});
req.write(querystring.stringify(params));
req.end();
}catch (e) {
console.log(text + " 翻译失败:" + e)
}
req.write(querystring.stringify(params));
req.end();
})
};
......@@ -96,10 +100,18 @@ async function translate(obj){
// 对未翻译内容调用百度翻译进行翻译操作,有变量的不做处理
for(let text in obj){
if(obj[text] || text.includes('{'))continue
const textEn = await translateText(text)
console.log(`${text} => ${textEn}\n`)
obj[text] = textEn
autoTransCnt ++
try{
const textEn = await translateText(text)
console.log(`${text} => ${textEn}\n`)
obj[text] = textEn
autoTransCnt ++
// 每次翻译后保存一次,防止意外退出导致的数据丢失
fs.writeFileSync(savePath, JSON.stringify(obj, null, 4))
}catch (e) {
console.log(text + " 翻译失败:" + e)
}
// 账号请求频率限制1ps
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