-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (129 loc) · 3.6 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
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
const fs = require('fs');
const util = require('util');
const _ = require('lodash');
const commandLineArgs = require('command-line-args')
const ProgressBar = require('progress');
const {
translate,
} = require('deepl-client');
const delay = 100;
const optionDefinitions = [
{ name: 'verbose', alias: 'v', type: Boolean },
{ name: 'help', alias: 'h', type: Boolean },
{ name: 'lang', alias: 'l', type: String },
{ name: 'source_lang', alias: 's', type: String },
{ name: 'input', alias: 'i', type: String },
{ name: 'output', alias: 'o', type: String },
{ name: 'key', alias: 'k', type: String },
{ name: 'engine', alias: 'e', type: String },
];
const options = commandLineArgs(optionDefinitions);
let data;
let moc;
let lang;
if (!options.input) {
throw new Error('input is not set');
} else {
data = fs.readFileSync(options.input);
moc = JSON.parse(data);
}
if (!options.lang) {
throw new Error('Lang is not set');
} else {
lang = options.lang.toUpperCase();
}
if (!options.source_lang) {
options.source_lang = 'EN';
}
if (!options.output) {
throw new Error('ouput is not set');
}
if (!options.key) {
throw new Error('API key is not set');
}
if (!options.engine) {
throw new Error('Please specify an engine (google or deepl)');
} else {
if (!['google', 'deepl'].includes(options.engine)) {
options.engine = 'google';
}
}
// Google API
const googleOptions = {};
const googleTranslate = require('google-translate')(options.key, googleOptions);
let numberOfCall = 0;
let numOfCallResolved = 0;
let bar;
translateObj = (params, outputLang, callback) => {
_.forIn(params, (value, key) => {
if (_.isObject(value)) {
translateObj(value, outputLang, callback);
} else {
numberOfCall++; // count for number of call
setTimeout(() => {
if (options.engine == 'deepl') {
const config = {
auth_key: options.key,
text: value,
source_lang: options.source_lang,
target_lang: outputLang,
}
translate(config)
.then(res => {
if (!bar) {
bar = new ProgressBar('translating [:bar] :percent :etas', {
complete: '=',
incomplete: ' ',
width: 50,
total: numberOfCall
});
bar.tick();
} else {
bar.tick();
}
numOfCallResolved++; // count for number of callback
params[key] = res.translations[0].text;
if (numberOfCall === numOfCallResolved) {
callback(null, moc); // once all callback arrived means that process ended
}
})
.catch((e) => {
callback(e, null);
})
} else { // This is for google API. Should be refactored
const translate = util.promisify(googleTranslate.translate);
translate(value, options.source_lang.toLowerCase(), outputLang.toLowerCase())
.then(res => {
if (!bar) {
bar = new ProgressBar('translating [:bar] :percent :etas', {
complete: '=',
incomplete: ' ',
width: 50,
total: numberOfCall
});
bar.tick();
} else {
bar.tick();
}
numOfCallResolved++; // count for number of callback
params[key] = res.translatedText;
if (numberOfCall === numOfCallResolved) {
callback(null, moc); // once all callback arrived means that process ended
}
})
.catch((e) => {
callback(e, null);
})
}
}, numberOfCall * delay) // avoid the api call limit
}
});
};
translateObj(moc, lang, (err, result) => {
if (err) {
console.error(err);
} else {
console.log(`Translation file available at ${options.output}`);
fs.writeFileSync(options.output, JSON.stringify(result, null, 4));
}
});