-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (44 loc) · 1.52 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
// Dependencies
const fs = require('fs');
const readline = require('readline');
const Stream = require('stream');
const env = require('env-var');
// Parameters
const apiKey = env('APIKEY').required().asString();
const inputFile = env('INPUT').asString() || 'resource/test.txt';
const outputFile = env('OUTPUT').asString() || 'resource/out.txt';
const scoreOption = env('SCORE').asBool();
const strictMode = env('STRICT').asBool();
const googleTranslate = require('google-translate')(apiKey);
readFileLineByLine(inputFile, outputFile);
function readFileLineByLine(inputFile, outputFile) {
var instream = fs.createReadStream(inputFile);
fs.writeFileSync(outputFile, '');
var outstream = new Stream();
outstream.readable = true;
outstream.writable = true;
var rl = readline.createInterface({
input: instream,
output: outstream,
terminal: false
});
rl.on('line', function (line) {
var score = '';
var word = line;
if (scoreOption) {
score = line.slice(line.length -4, line.length);
word = line.substring(0, line.length -4 );
}
googleTranslate.translate(word, 'en', 'fr', function(err, translation) {
if (err) {
console.error(err);
if (strictMode) {
process.exit(1);
}
return;
}
var text = translation.translatedText.toLowerCase();
fs.appendFileSync(outputFile, text + score + '\n');
});
});
}