This repository has been archived by the owner on Dec 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-flat-translation-file.js
64 lines (61 loc) · 1.73 KB
/
generate-flat-translation-file.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
/* eslint-disable no-console */
/* eslint-disable no-prototype-builtins */
const language = process.argv[2] || "en";
const data = require(`./locale/en/translation.json`);
const transData = require(`./locale/${language}/translation.json`);
const fs = require("fs");
const currentFolder = process.cwd();
const flattenObject = (obj) => {
let flattenKeys = {};
for (let i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == "object") {
let flatObject = flattenObject(obj[i]);
for (let j in flatObject) {
if (!flatObject.hasOwnProperty(j)) continue;
flattenKeys[i + "." + j] = flatObject[j];
}
} else {
flattenKeys[i] = obj[i];
}
}
return flattenKeys;
};
const flat = flattenObject(data);
const transFlat = flattenObject(transData);
let lines;
const keys = Object.keys(flat);
if (language == "en") {
lines = [`"key","en"`];
keys.forEach((key) => {
const value = flat[key].replace(/"/g, '""');
lines.push(`"${key}","${value}"`);
});
}
if (language != "en") {
lines = [`"key","en","${language}"`];
keys.forEach((key) => {
const transValue = (transFlat[key] || "").replace(/"/g, '""');
const value = flat[key].replace(/"/g, '""');
lines.push(
`"${key}","${value}","${
!value.startsWith("http") &&
!value.startsWith("/") &&
value == transValue
? ""
: transValue
}"`
);
});
}
const fileOutput = lines.join("\n");
fs.writeFile(
`${currentFolder}/locale/${language}/translation-${language}-extract.csv`,
fileOutput,
function (err) {
if (err) throw err;
console.log(
`\x1b[35m[filegen] locale/${language}/translation-${language}-extract.csv generated successfully.\x1b[0m`
);
}
);