This repository was archived by the owner on May 7, 2025. It is now read-only.
forked from meedan/i18n-translate-json
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·79 lines (67 loc) · 1.97 KB
/
cli.js
File metadata and controls
executable file
·79 lines (67 loc) · 1.97 KB
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
#!/usr/bin/env node
'use strict';
var translate = require("./translate");
var path = require("path");
var getJSON = require("get-json");
var argv = require("yargs")
.usage("Usage: $0 <apiKey> <startDir> <sourceLang> <targetLang1,targetLang2,..>")
.boolean("includeHtml")
.describe("includeHtml", "Include HTML entries in the translation")
.help("h")
.alias("h", "help")
.example("$0 YOUR_API_KEY examples/ en nl,de", "Translate the file examples/en.json to examples/nl.json and examples/de.json")
.argv;
if (argv._.length < 4) {
console.error("Usage: i18-translate-json <apiKey> <startDir> <sourceLang> <targetLang1,targetLang2,..>");
return 1;
}
// get the start directory from parameters
var apiKey = argv._[0];
var startDir = argv._[1];
var sourceLang = argv._[2];
var targetLang = argv._[3];
// append / at the end of directory
if (startDir[startDir.length - 1] != "/") {
startDir += "/";
}
// run translation
var run = function() {
path.resolve(__dirname, startDir);
translate.run(apiKey, startDir, sourceLang, targetLang, argv.includeHtml, function(err, result) {
if (err) {
console.log("ERROR:");
console.log(err);
process.exit(0);
}
process.exit(0);
});
}
// if target languages are not provided, get all languages supported by Google Translate
if (!targetLang) {
targetLang = [];
getJSON('https://translation.googleapis.com/language/translate/v2/languages?key=' + apiKey, function(error, response) {
if (error) {
console.log("ERROR:");
console.log(error);
process.exit(0);
}
else {
var langs = response.data.languages;
for (var i = 0; i < langs.length; i++) {
var lang = langs[i].language;
if (lang.length === 2) {
targetLang.push(lang);
}
}
run();
}
});
}
else {
targetLang = targetLang.split(',');
// trim whitespaces for targetlangs
for (var i = 0; i < targetLang.length; i++) {
targetLang[i] = targetLang[i].trim();
}
run();
}