-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (72 loc) · 2.25 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
var xlsxj = require("xlsx-to-json");
const fs = require("fs");
const inputExcelFilePath = "./files/i/dictionary.xlsx";
const inputLanguageJSONFileToConvert = "./files/i/locale-ES_spa.json";
const outputExcelToJSONFilePath = "./files/o/outfile.json";
const outputLanguageJSONFileConverted = "./files/o/OUTPUT-locale-ES_spa.json";
const outputNotTranslatedTxtFile = "./files/o/notfoundfile.txt";
var totalReplaced = 0;
var totalNotReplaced = 0;
var notFoundStringsArray = [];
xlsxj(
{
input: inputExcelFilePath,
output: outputExcelToJSONFilePath,
lowerCaseHeaders: true
},
replaceStrings
);
function replaceStrings(err, dictionary) {
if (err) {
console.error(err);
} else {
let found;
function parseObject(obj, keyVal, outerObj) {
if (Object.keys(obj).length > 1 && typeof obj !== "string") {
for (key in obj) {
parseObject(obj[key], key, obj);
}
} else if (Object.keys(obj).length === 1 && typeof obj === "object") {
for (key in obj) {
parseObject(obj[key], key, obj);
}
} else {
found = false;
for (i = 0; i < dictionary.length; i++) {
if (dictionary[i].english === obj && dictionary[i].spanish !== "") {
totalReplaced++;
outerObj[keyVal] = dictionary[i].spanish;
found = true;
}
}
if (found === false) {
notFoundStringsArray.push(outerObj[keyVal]);
totalNotReplaced++;
}
}
return obj;
}
let rawdata = fs.readFileSync(inputLanguageJSONFileToConvert);
let JSONInputContent = JSON.parse(rawdata);
var returnJSON = parseObject(JSONInputContent);
console.log("Total translations made ----->", totalReplaced);
fs.writeFile(
outputLanguageJSONFileConverted,
JSON.stringify(returnJSON),
err => {
if (err) throw err;
console.log("File saved!");
}
);
console.log("Total not translated ----->", totalNotReplaced);
var file = fs.createWriteStream(outputNotTranslatedTxtFile);
file.on("error", function(err) {
console.log(err);
});
notFoundStringsArray.forEach(function(v) {
file.write(v + "\n");
});
file.end();
console.log("Done writing to file.");
}
}