forked from vekexasia/android-i18n-csv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.js
107 lines (93 loc) · 2.44 KB
/
export.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
var fs = require('fs');
var argv = require('optimist').argv;
var eyes = require('eyes');
var xml2js = require('xml2js');
var parser = new xml2js.Parser();
if (typeof(argv.xml) == 'undefined' || typeof(argv.origlang) == 'undefined' || typeof(argv.wantedlangs) == 'undefined' ) {
printUsage();
//eyes.inspect(argv);
} else {
//eyes.inspect(argv);
var config = {
'xmlFile': argv.xml,
'origLangSlug': argv.origlang,
'wantedLangs': argv.wantedlangs.split(',')
};
//eyes.inspect(config);
fs.stat(config.xmlFile, function (err, stats) {
if (err) {
console.log("File not found or not readable");
} else {
fs.readFile(config.xmlFile, function(err, data) {
if (err) {
console.log("File not readable");
} else {
parser.parseString(data);
}
});
}
} );
}
parser.on('end', function(result) {
var tmpArrayForCsv = [];
// Headline
var headLine = ['Type', 'Key', config.origLangSlug];
for (k in config.wantedLangs) {
headLine.push(config.wantedLangs[k]);
}
tmpArrayForCsv.push(headLine);
for (k in result) {
if (k == 'string') {
for (entry in result[k]) {
if (result[k][entry]['#'].substr(0,8) == '@string/') {
//console.log('skipped'+result[k][entry]['@'].name);
} else {
//eyes.inspect(result[k][entry]);
var toAdd = [k, result[k][entry]['@'].name, result[k][entry]['#']];
for (wl in config.wantedLangs) {
toAdd.push('');
}
tmpArrayForCsv.push(toAdd);
}
}
} else if (k=='string-array') {
for (item in result[k].item) {
var toAdd = [k, result[k]['@'].name, result[k].item[item]];
for (wl in config.wantedLangs) {
toAdd.push('');
}
tmpArrayForCsv.push(toAdd);
}
} else {
eyes.inspect(k);
eyes.inspect(result[k]);
}
}
//eyes.inspect(tmpArrayForCsv);
writeToCsv(tmpArrayForCsv);
});
function writeToCsv(csvArr) {
var stringForFile = "";
for (row in csvArr) {
var rowStr = '';
for (entry in csvArr[row]) {
var str = csvArr[row][entry];
str = str.replace(/\\'/g,'\'');
if (str.indexOf('"') != -1) {
str = str.replace(/"/g,'\\"\\"');
str = '"'+str+'"';
} else if (str.indexOf(',') != -1) {
str = '"'+str+'"';
}
rowStr+=","+str;
}
rowStr= rowStr.substring(1);
stringForFile +=rowStr+"\n";
}
console.log(stringForFile);
}
function printUsage() {
console.log("Usage:");
console.log("");
console.log("node ./export.js --xml path/to/strings.xml --origlang en --wantedlangs it,de,fr,nl");
}