-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
66 lines (65 loc) · 2 KB
/
index.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
63
64
65
66
const Exceljs = require("exceljs");
const fs = require("fs");
const path = require("path");
const axios = require("axios");
const _ = require("lodash");
const bluebird = require("bluebird");
const getLanguage = (text) => {
return new Promise((resolve) => {
import("franc").then(({ franc }) => resolve(franc(text)));
});
};
const excel = new Exceljs.Workbook();
const createExcelInstance = new Exceljs.Workbook();
const translateText = async (text, mode) => {
try {
const returnData = await axios.get(
`https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&sl=${mode}&tl=zh-CN&q=${text}`
);
return _.get(returnData, "data.0", [])
?.map((v) => v[0])
?.join("");
} catch (error) {
return text
}
};
const main = async () => {
await excel.xlsx.readFile(path.join(__dirname, "translate.xlsx"));
const sheetList = excel.worksheets;
sheetList.forEach((sheet) => {
if (/^\d+$/g.test(sheet.name)) {
const createNewSheet = createExcelInstance.addWorksheet(sheet.name);
let textArr = [];
sheet.eachRow((row, rowNum) => {
let arr = row.values.map((v) => String(v));
if (rowNum === 1) {
createNewSheet.addRow(row.values);
} else {
const needTranslateText = row.getCell("I").value;
textArr.push({ row, needTranslateText, arr });
}
});
bluebird
.map(
textArr,
async (v) => {
const language = await getLanguage(v.needTranslateText);
let mode = "en";
if (language === "jpn") {
mode = "ja";
console.log("----->", v.needTranslateText);
}
const text = await translateText(v.needTranslateText, mode);
createNewSheet.addRow(v.arr.concat(text));
console.log(text);
return text;
},
{ concurrency: 3 }
)
.then(() => {
createExcelInstance.xlsx.writeFile(`qtcreator-target.xlsx`)
});
}
});
};
main();