lbry-desktop/src/renderer/extractLocals.js

49 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-12-13 22:36:30 +01:00
const extract = require("i18n-extract");
2017-06-21 14:23:21 +02:00
const fs = require("fs");
2017-12-14 13:58:07 +01:00
const dir = `${__dirname}/../../static/locales`;
2017-12-13 22:36:30 +01:00
const path = `${dir}/en.json`;
2017-06-21 14:23:21 +02:00
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
2017-12-13 22:36:30 +01:00
fs.writeFile(path, "{}", "utf8", err => {
2017-06-21 14:23:21 +02:00
if (err) {
return console.log(err);
}
2017-12-13 22:36:30 +01:00
const enLocale = require(path);
2017-06-21 14:23:21 +02:00
const keys = extract.extractFromFiles(["js/**/*.{js,jsx}"], {
marker: "__",
});
let reports = [];
reports = reports.concat(extract.findMissing(enLocale, keys));
if (reports.length > 0) {
2017-12-13 22:36:30 +01:00
fs.readFile(path, "utf8", (err, data) => {
2017-06-21 14:23:21 +02:00
if (err) {
console.log(err);
} else {
localeObj = JSON.parse(data);
2017-12-13 22:36:30 +01:00
for (let i = 0; i < reports.length; i++) {
2017-06-21 14:23:21 +02:00
// no need to care for other types than MISSING because starting file will always be empty
if (reports[i].type === "MISSING") {
localeObj[reports[i].key] = reports[i].key;
}
}
2017-12-13 22:36:30 +01:00
const json = JSON.stringify(localeObj, null, "\t"); // convert it back to json-string
fs.writeFile(path, json, "utf8", err => {
2017-06-21 14:23:21 +02:00
if (err) {
throw err;
}
console.log("Extracted all strings!");
2017-06-12 12:20:50 +02:00
});
2017-06-21 14:23:21 +02:00
}
});
}
});