-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·94 lines (72 loc) · 2.69 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
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
#! /usr/bin/env node
const csv = require('csvtojson');
const fs = require('fs');
const xml2js = require('xml2js');
if (process.argv.length != 4) {
console.log(
"You need to pass in two arguments:\n"
+ "\n"
+ "1. CSV file\n"
+ "2. Path to project resource directory\n"
+ "\n"
+ "Example:\n"
+ "\n"
+ "\ttranslation-copier mycsv.csv ~/Workspace/MyProject/app/src/main/res\n"
);
return;
}
const CSVFile = process.argv[2];
const resFolder = process.argv[3];
csv().fromFile(CSVFile)
.on('json', (item) => {
const language = item[Object.keys(item)[0]];
delete item[Object.keys(item)[0]];
const valuesFolder = (language == "en" ? resFolder + "/values" : resFolder + "/values-" + language);
if (!fs.existsSync(valuesFolder)) {
fs.mkdirSync(valuesFolder);
}
const stringsFile = valuesFolder + "/strings.xml";
if (!fs.existsSync(stringsFile)) {
fs.writeFileSync(stringsFile,
'<resources></resources>',
'utf8');
}
const stringsXMLContent = fs.readFileSync(stringsFile, 'utf8');
const isFourSpacesTabbed = stringsXMLContent.indexOf(' ') != -1
&& (stringsXMLContent.indexOf('string-array') == -1
|| stringsXMLContent.indexOf(' ') < stringsXMLContent.indexOf('string-array'));
xml2js.parseString(stringsXMLContent, (error, xmlJSON) => {
Object.keys(item).forEach(key => {;
if (!xmlJSON.resources) {
xmlJSON.resources = {};
}
if (!xmlJSON.resources.string) {
xmlJSON.resources.string = [];
}
xmlJSON.resources.string = xmlJSON.resources.string.filter(item => {
if (item.$.name == key) {
return false;
}
return true;
});
xmlJSON.resources.string.push({
"_": item[key],
"$": {
"name": key
}
});
});
var xml = new xml2js.Builder({
'xmldec': '<?xml version="1.0" encoding="UTF-8"?>'
}).buildObject(xmlJSON);
xml = '<?xml version="1.0" encoding="UTF-8"?>\n'
+ xml.replace('<?xml version="1.0"?>\n', '');
if (isFourSpacesTabbed) {
xml = xml.split(' <').join(' <');
}
fs.writeFileSync(stringsFile, xml, 'utf8');
});
})
.on('done', (error) => {
console.log('Done!')
});