-
Notifications
You must be signed in to change notification settings - Fork 23
/
translate-json.js
168 lines (154 loc) · 5.78 KB
/
translate-json.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env node
const fs = require('fs');
const moment = require('moment');
const _ = require('lodash');
const path = require('path');
const agent = require('superagent-promise')(require('superagent'), Promise);
const { translate } = require('google-translate-api-browser');
let dicc = {};
//Lang Codes https://ctrlq.org/code/19899-google-translate-languages
if (process.argv.length >= 4) {
//Args
const apiKey = process.argv[2];
const inputFile = process.argv[3];
const destinationCodes = process.argv[4].split(',');
const apiUrl = _.template(
'https://www.googleapis.com/language/translate/v2?key=<%= apiKey %>&q=<%= value %>&source=en&target=<%= languageKey %>'
);
const transformResponse = (res) => {
return _.get(
JSON.parse(res.text),
['data', 'translations', 0, 'translatedText'],
''
);
};
const getCache = (languageKey) => {
try {
dicc[languageKey] = {};
let fileContent = fs
.readFileSync(`./translateCache-${languageKey}.txt`, 'utf-8')
.split('\n');
fileContent.map((line) => {
let cached = line.split('|');
if (cached[0]) dicc[languageKey][cached[0]] = cached[1];
});
} catch (error) {}
};
const cachedIndex = (key, value, languageKey) => {
const line = key + '|' + value + '\n';
dicc[languageKey][key] = value;
fs.appendFileSync(`./translateCache-${languageKey}.txt`, line);
return value;
};
function iterLeaves(value, keyChain, accumulator, languageKey) {
accumulator = accumulator || {};
keyChain = keyChain || [];
if (_.isObject(value)) {
return _.chain(value)
.reduce((handlers, v, k) => {
return handlers.concat(
iterLeaves(
v,
keyChain.concat(k),
accumulator,
languageKey
)
);
}, [])
.flattenDeep()
.value();
} else {
if (typeof value !== 'string') return value;
return function () {
if (!(value in dicc[languageKey])) {
console.log(
_.template(
'Translating <%= value %> to <%= languageKey %>'
)({ value, languageKey })
);
let prom;
//Translates individual string to language code
if (apiKey != '') {
//using apiKey
prom = agent(
'GET',
apiUrl({
value: encodeURI(value),
languageKey,
apiKey,
})
).then(transformResponse);
} else {
//using free api key
prom = translate(value, { to: languageKey });
}
return prom
.then((res) => cachedIndex(value, res, languageKey))
.catch((err) => console.log(err))
.then((text) => {
//Sets the value in the accumulator
_.set(accumulator, keyChain, text);
//This needs to be returned to it's eventually written to json
return accumulator;
});
} else {
console.log(value + ' cached: ' + dicc[languageKey][value]);
_.set(accumulator, keyChain, dicc[languageKey][value]);
return accumulator;
}
};
}
}
Promise.all(
_.reduce(
destinationCodes,
(sum, languageKey) => {
const fileName = _.template(
'./<%= languageKey %>-<%= timeStamp %>.json'
)({
languageKey,
timeStamp: moment().unix(),
});
//read languageKey Cache.
getCache(languageKey);
//Starts with the top level strings
return sum.concat(
_.reduce(
iterLeaves(
JSON.parse(
fs.readFileSync(
path.resolve(inputFile),
'utf-8'
)
),
undefined,
undefined,
languageKey
),
(promiseChain, fn) => {
return promiseChain.then(fn);
},
Promise.resolve()
)
.then((payload) => {
fs.writeFileSync(fileName, JSON.stringify(payload));
})
.then(
_.partial(
console.log,
'Successfully translated all nodes, file output at ' +
fileName
)
)
);
},
[]
)
).then(() => {
process.exit();
});
} else {
console.error(
'You must provide an input json file and a comma-separated list of destination language codes.'
);
}