-
Notifications
You must be signed in to change notification settings - Fork 6
/
presets_extract_labels.js
67 lines (58 loc) · 1.41 KB
/
presets_extract_labels.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
/*
* Creates a JSON file for Transifex to allow translation of XML presets.
*/
const fs = require('fs');
const parseString = require('xml2js').parseString;
const Hash = require("object-hash");
const PRESETS_DIR = "./public/presets";
const LOCALES_DIR = "./src/config/locales/presets";
const XML_RGX = /^[A-Za-z0-9_\-]+\.xml$/;
const foundLabels = {};
const entryToLabels = entry => {
Object.entries(entry).forEach(e => {
const [ k, v ] = e;
if(k === "$") {
Object.entries(v).forEach(ve => {
const [ vk, vv ] = ve;
if([ "text", "name", "display_values", "display_value"].includes(vk)) {
foundLabels[Hash(vv)] = vv;
}
});
}
else {
entryToLabels(v);
}
});
};
//Read translation files
fs.readdirSync(PRESETS_DIR).forEach((file) => {
if(XML_RGX.test(file)) {
try {
const xml = fs.readFileSync(PRESETS_DIR+"/"+file, 'utf8');
// Parse XML content
parseString(xml, (err, result) => {
if (err) {
throw new Error("Parse error", e.message);
}
else {
entryToLabels(result);
}
});
}
catch(e) {
throw new Error("Can't read file: "+file+" ("+e.message+")");
}
}
else {
console.log("[INFO] Ignored file "+file);
}
});
// Export found labels
fs.writeFile(LOCALES_DIR+"/en.json", JSON.stringify({ "en": foundLabels }, null, 2), function(err) {
if(err) {
throw new Error(err);
}
else {
console.log("[INFO] Translation file for presets updated");
}
});