-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate-po.js
executable file
·55 lines (45 loc) · 1.42 KB
/
translate-po.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
#!/usr/bin/env node
var po = require('node-po');
var async = require('async');
var _ = require('lodash');
var debug = require('debug')('translate-po');
var googleTranslate = require('google-translate')(process.env.APIKEY);
var argv = require('minimist')(process.argv.slice(2));
var usage = 'Usage: translate-po.js <po-input-file> <output-lang> <po-output-file>\n'
+ 'e.g. translate-po.js en/messages.po it it/messages.po';
function main(cb) {
var poFile = argv._[0];
var lang = argv._[1];
var outputFile = argv._[2];
debug('poFile:', poFile);
debug('lang:', lang);
debug('outputFile:', outputFile);
if (!poFile || !lang || !outputFile) return cb(usage);
po.load(poFile, function(po){
translate(po.items, lang, function(err, translations){
if (err) return cb(err);
_.each(po.items, function(item, index) {
if (translations[index]) item.msgstr = translations[index].translatedText;
});
po.save(outputFile, cb);
});
});
};
function translate(items, lang, cb) {
var q = async.queue(doTranslate, 5);
function doTranslate (item, cb) {
debug('translating:', item);
googleTranslate.translate(item.msgstr, lang, cb);
};
var translations= [];
q.push(items, function(err, data) {
if (err) return cb(err);
translations.push(data);
});
q.drain = function() {
return cb(null, translations);
}
};
main(function(err) {
if (err) console.error(err);
});