-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtranslate.js
62 lines (50 loc) · 1.78 KB
/
translate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const fetch = require('node-fetch');
const { readFileSync, appendFileSync, unlinkSync, existsSync } = require('fs');
const LIMIT_FILE_CONTENT = 3000;
async function trasnlate(pathname) {
console.log('translate:', pathname);
const fileContent = readFileSync(pathname, { encoding: 'utf-8' });
const enFilePath =
pathname.slice(0, pathname.indexOf('.md')) +
'.en-US' +
pathname.slice(pathname.indexOf('.md'));
async function fetchTranslate(content, delEnglishFile) {
const url = `https://translate.googleapis.com/translate_a/single?oe=UTF-8&i=UTF-8&client=gtx&dt=t&hl=zh-CN&sl=zh-CN&tl=en&text=${encodeURIComponent(
content,
)}`;
try {
const res = await fetch(url);
const data = await res.json();
if (existsSync(enFilePath) && delEnglishFile) {
unlinkSync(enFilePath);
}
if (data && data[0]) {
data[0].forEach(item => {
if (item && item[0]) {
let str = item[0] || '';
// Deal with '- xxxxxxx' for md.translation will lose spaces.The exclusion of '---'.
if (str.charAt(0) === '-' && str.charAt(1) !== '-') {
str = '- ' + str.slice(1, str.length);
}
// Deal with english path.
str = str
.replace(/\]\(\//g, '](/en-US/')
.replace(/href="\//g, 'href="/en-US/');
appendFileSync(enFilePath, str);
}
});
}
console.log(pathname, ' success.');
} catch (e) {
console.log(pathname, ' failed.');
console.log(e);
}
}
for (let i = 0; i < Math.ceil(fileContent.length / LIMIT_FILE_CONTENT); i++) {
await fetchTranslate(
fileContent.slice(LIMIT_FILE_CONTENT * i, LIMIT_FILE_CONTENT * (i + 1)),
i === 0,
);
}
}
module.exports = trasnlate;