forked from vekexasia/android-i18n-csv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.js
111 lines (94 loc) · 2.83 KB
/
import.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
var fs = require('fs');
var argv = require('optimist').argv;
var eyes = require('eyes');
var xml2js = require('xml2js');
var csv = require('csv');
var builder = require('xmlbuilder');
var path = require('path');
if (typeof(argv.csv) == 'undefined' || typeof(argv.deflang) == 'undefined' || typeof(argv.folderout) == 'undefined') {
printUsage();
} else {
var config = {
'csvFile': argv.csv,
'deflang': argv.origlang,
'outDir': argv.folderout
};
fs.stat(config.csvFile, function (err, stats) {
if (err) {
console.log("File not found or not readable");
} else {
fs.readFile(config.csvFile, function(err, data) {
if (err) {
console.log("File not readable");
} else {
var converted = [];
csv()
.from(data, {'columns': true})
.on('data',function(data,index) {
converted.push(data);
})
.on('end', function() {
saveTranslations(converted);
});
}
});
}
} );
}
function parseContentForAndroidCompliancy(str) {
return str.replace(/'/g,'\\\'').replace(/\\\\'/g, '\\\'');
}
function saveTranslations(translationData) {
var columns = Object.keys(translationData[0]);
var nLanguages = columns.length-2;
for (var i=0; i<nLanguages; i++) {
var languageIdx = i+2;
var languageKey = columns[languageIdx];
var doc = "<resources>\n";
var stringArrays = {};
for (var row=0; row<translationData.length; row++) {
var item = translationData[row];
if (item.type == 'string-array') {
if ( typeof(stringArrays[item.Key]) == 'undefined') {
stringArrays[item.Key] = [];
}
stringArrays[item.Key].push(parseContentForAndroidCompliancy(item[languageKey]));
} else {
if (typeof(item[languageKey]) == 'undefined' || item[languageKey] == null ) {
console.log("Skipped "+item.Key+" Language: "+languageKey);
} else {
var extraAttributes = '';
if (parseContentForAndroidCompliancy(item[languageKey]).indexOf('%s') != -1) {
extraAttributes = ' formatted="false" ';
}
doc+="\t<string name=\""+item.Key+"\""+extraAttributes+">"+parseContentForAndroidCompliancy(item[languageKey])+"</string>\n";
}
}
}
// Adding stringarrays
for (key in stringArrays) {
doc +="\t<string-array name=\""+key+"\">\n";
for (item in stringArrays[key]) {
doc +="\t\t<item>"+stringArrays[key][item]+"</item>\n";
}
doc +="\t</string-array>\n";
}
doc+='</resources>';
writeXml(languageKey, doc);
}
}
function writeXml(langKey, xmlDoc) {
try {
fs.mkdirSync(path.join(config.outDir, 'values-'+langKey));
} catch (e) {}
try {
fs.writeFileSync(path.join(config.outDir, 'values-'+langKey, 'strings.xml'), xmlDoc,'utf8');
} catch (e) {
console.log("E. writing");
}
}
function printUsage() {
console.log("Usage:");
console.log("");
console.log("node ./export.js --csv path/to/strings.csv --deflang en --folderout path/to/folder");
}