-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (48 loc) · 1.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
var CSV = require('csv-string');
var fs = require('fs');
var filename = "translations.csv"; // TODO
function language(rows, index) {
return rows.map(function(x) {
return {
key: x[0],
string: x[index],
};
});
}
function addKey(languageObject, key, string) {
var parts = key.split(".");
var part = parts[0];
if (parts.length == 1) {
languageObject[part] = string;
} else {
if (!languageObject[part]) {
languageObject[part] = {};
}
addKey(languageObject[part], parts.splice(1).join("."), string);
}
}
function createFile(object) {
return "/* eslint-disable quotes */\nmodule.exports = " + JSON.stringify(object, 2, 2) + ";";
}
function exportStrings(object, language, output) {
var filename = output + "/translations." + language + ".js";
var file = createFile(object);
fs.writeFileSync(filename, file);
}
function exportStringInFile(filename, languages, output) {
var file = fs.readFileSync(filename, { encoding: "utf8" });
var rows = CSV.parse(file).splice(1).filter(function(row) {
return row[1] == "ja" && row[2] == "ja";
});
for (var i = 0; i < languages.length; i++) {
var identifier = languages[i].identifier;
var index = languages[i].index;
var strings = language(rows, index);
var languageObject = {};
for (var j = 0; j < strings.length; j++) {
addKey(languageObject, strings[j].key, strings[j].string);
}
exportStrings(languageObject, identifier, output);
}
}
module.exports = exportStringInFile;