-
Notifications
You must be signed in to change notification settings - Fork 0
/
yml.mjs
47 lines (42 loc) · 1.49 KB
/
yml.mjs
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
import fs from 'fs'
import YAML from 'yaml'
import https from 'https'
function translate(srcLng, targetLng, text) {
return new Promise((resolve) => {
https
.get(`https://clients5.google.com/translate_a/t?client=dict-chrome-ex&sl=${srcLng}&tl=${targetLng}&q=${text}`, (response) => {
let result = '';
response.on('data', (chunk) => {
result += chunk;
});
response.on('end', () => {
const target = JSON.parse(result)[0]
console.log(`Success: ${text} => ${target}`)
resolve(target)
});
})
.on("error", (error) => {
console.log("Error: " + error.message)
resolve("需要手动翻译:" + text)
});
});
}
const srcLng = 'zh-cn'
const targetLngArray = ['zh-TW']
const file = fs.readFileSync('./zh-cn.yml', 'utf8')
const src = YAML.parseDocument(file)
targetLngArray.forEach(async targetLng => {
const target = src.clone()
for (let i = 0; i < target.contents.items.length; i++) {
const element = target.contents.items[i];
const result = await translate(srcLng, targetLng, element.value.value)
target.set(element.key.value, result)
}
console.log(YAML.stringify(target))
fs.writeFile(`./${targetLng}.yml`, YAML.stringify(target), err => {
if (err) {
console.error(err)
return
}
})
})