-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpostsTranslator.js
More file actions
190 lines (182 loc) · 6.59 KB
/
postsTranslator.js
File metadata and controls
190 lines (182 loc) · 6.59 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
module.exports = translate
const marked = require("marked");
const fs = require("fs");
// const axiosRequest = require("axios").default;
const axiosRequest = require("axios-https-proxy-fix").default;
const tokenGenerator = require("translate-google-api/src/token");
// const tunnel = require("tunnel")
// const tunnelProxy = tunnel.httpsOverHttp({
// proxy: {
// host: "127.0.0.1",
// port: 7890
// }
// })
const G_API = "https://translate.google.cn/translate_a/single";
const proxy = {
host: "127.0.0.1",
port: 7890,
};
const sourceLanguage = "en";
const destLanguage = "zh-CN";
const insertAutoTransMark = true
const autoTranslationMark = "auto_translation: true\n"
const isDebug = true;
const articlePath = "./README.md";
const newArticlePath = "./";
translate(articlePath, newArticlePath);
/**
* @param {string} articlePath
* @param {string} newArticlePath
* @returns {boolean}
*/
function translate(articlePath, newArticlePath) {
fs.readFile(articlePath, "utf-8", (err, data) => {
if (err) {
console.error(err);
return false;
}
if (isDebug) console.log("Read markdown file:\n ", data);
articleTranslator(data).then((res) => {
if (isDebug) console.log("translated markdown:\n ", res);
fs.writeFile(
newArticlePath +
articlePath.split("/").pop().split(".")[0] +
`-${destLanguage}.md`,
res,
{ flag: "w+" },
(err) => {
if (err) {
console.error(err);
return false;
}
}
);
return true;
});
});
}
/**
* @param {string} article
* @returns {Promise(string)}
*/
async function articleTranslator(article) {
const paragraphRegex = /([^\n]+)\n/g;
const headingRegex = /(#+)([^\n]+)/g;
const inlineCodeRegex = /`{1,2}[^`](.*?)`{1,2}/g;
const codeBlockRegex = /```([\s\S]*?)```[\s]?/g;
const frontMatterRegex = /---([\s\S]*?)---[\s]?/g;
const listRegex = /[-|*|+][ ]+([^\s]*)/g;
const numberedListRegex = /^[\\s]*[0-9]+\\.(.*)/g;
const htmlTagRegex = /<("[^"]*"|'[^']*'|[^'">])*>/g
if (isDebug) console.log("Starting Translation");
const protectedAreas = [];
article
.replace(codeBlockRegex, (match, p, offset, raw) => {
console.log(`Found Code Block ${match}`)
protectedAreas.push({ start: offset, end: offset + match.length, type: "codeBlock", ref: match })
return match
})
.replace(frontMatterRegex, (match, p, offset, raw) => {
console.log(`Found Front Matter ${match}`)
protectedAreas.push({ start: offset, end: offset + match.length, type: "frontMatter", ref: match })
return match
})
.replace(inlineCodeRegex, (match, p, offset, raw) => {
console.log(`Found Inline Code ${match}`)
// protectedAreas.push({ start: offset, end: offset + match.length, type: "inlineCode" })
return match
})
.replace(htmlTagRegex, (match, p, offset, raw) => {
console.log(`Found Code Block ${match}`)
protectedAreas.push({ start: offset, end: offset + match.length, type: "htmlTag", ref: match })
return match
})
let translatedArticle = await replaceAsync(
article,
paragraphRegex,
async (match, body, offset, raw) => {
var failed = false
var protected = false
protectedAreas.forEach(protection => {
if (offset >= protection.start && offset < protection.end) {
if (isDebug) console.log(`protected: ${body}`)
protected = true
}
})
if (protected) return match
if (body.match(headingRegex)) {
let heading = headingRegex.exec(body)
if (isDebug) console.log(`Found a title ${heading[1]} with content ${heading[2]}`);
let dest = await translateG(
heading[2],
sourceLanguage,
destLanguage
).catch(e => {
if (isDebug) console.log(`Translate ${heading[2]} failed`)
failed = true
})
if (failed) dest = heading;
return [heading[1], " ", dest, '\n'].join("");
} else if (body.match(listRegex)) {
let listItem = listRegex.exec(body)
if (isDebug) console.log(`Found list item with content ${listItem[1]}`)
let dest = await translateG(listItem[1], sourceLanguage, destLanguage).catch(e => {
if (isDebug) console.log(`Translate ${listItem[1]} failed`)
failed = true
})
if (failed) dest = listItem[1]
return '- ' + dest + '\n';
} else {
let dest = await translateG(body, sourceLanguage, destLanguage).catch(e => {
if (isDebug) console.log(`Translate ${body} failed`, e)
failed = true
})
if (failed) dest = body
return dest + '\n'
}
}
);
return insertAutoTransMark ? translatedArticle.slice(0, 4) + autoTranslationMark + translatedArticle.slice(4) : translatedArticle
}
/**
* @param {string} str
* @param {RegExp} reg
* @param {async function} callback
*/
async function replaceAsync(str, regex, callback) {
const promiseQueue = [];
str.replace(regex, (...args) => {
promiseQueue.push(callback(...args));
});
let replaces = await Promise.all(promiseQueue);
return str.replace(regex, () => replaces.shift());
}
/**
* @param {string} snippet
* @param {string} sl
* @param {string} tl
* @returns {Promise}
*/
async function translateG(snippet, sl, tl) {
if (isDebug) console.log(`Translating Text: ${snippet}`);
let token = await tokenGenerator.get(snippet, { tld: "cn" });
let resp = await axiosRequest.get(G_API, {
params: {
client: "gtx",
sl: sl,
tl: tl,
dt: "t",
q: snippet,
otf: 1,
ssel: 0,
tsel: 0,
kc: 7,
[token.name]: token.value,
},
proxy: proxy,
});
// if (isDebug) console.log(`Raw response: ${resp}`);
let destBody = resp.data[0].map((resultStruct) => resultStruct[0]).join("");
if (isDebug) console.log(`Reconstructed paragraph: ${destBody}`);
return destBody;
}