lbry-desktop/build/extractLocals.js

50 lines
1.3 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-20 16:37:19 +01:00
const path = require("path");
2017-06-21 14:23:21 +02:00
2017-12-20 16:37:19 +01:00
const outputDir = `${__dirname}/../static/locales`;
const outputPath = `${outputDir}/en.json`;
2017-06-21 14:23:21 +02:00
2017-12-20 16:37:19 +01:00
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
2017-06-21 14:23:21 +02:00
}
2017-12-20 16:37:19 +01:00
fs.writeFile(outputPath, "{}", "utf8", err => {
2017-06-21 14:23:21 +02:00
if (err) {
return console.log(err);
}
2017-12-20 16:37:19 +01:00
const enLocale = require(outputPath);
2017-06-21 14:23:21 +02:00
2017-12-20 16:37:19 +01:00
const keys = extract.extractFromFiles("src/**/*.{js,jsx}", {
2017-06-21 14:23:21 +02:00
marker: "__",
});
let reports = [];
reports = reports.concat(extract.findMissing(enLocale, keys));
if (reports.length > 0) {
2017-12-20 16:37:19 +01:00
fs.readFile(outputPath, "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
2017-12-20 16:37:19 +01:00
fs.writeFile(outputPath, 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
}
});
}
});