0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-09 08:20:45 -05:00
penpot/frontend/scripts/find-unused-translations.js

104 lines
2.8 KiB
JavaScript
Raw Normal View History

2024-07-01 03:28:40 -05:00
const fs = require("fs").promises;
const gt = require("gettext-parser");
2024-07-01 03:28:40 -05:00
const path = require("path");
const util = require("node:util");
const execFile = util.promisify(require("node:child_process").execFile);
async function processMsgId(msgId) {
return execFile("grep", ["-r", "-o", msgId, "./src"]).catch(() => {
return msgId;
});
}
async function processFile(f) {
const content = await fs.readFile(f);
2024-07-01 03:28:40 -05:00
const data = gt.po.parse(content, "utf-8");
const translations = data.translations[""];
const badIds = [];
for (const property in translations) {
const data = await processMsgId(translations[property].msgid);
2024-07-01 03:28:40 -05:00
if (data != null && data.stdout === undefined) {
badIds.push(data);
}
}
return badIds;
}
async function cleanFile(f, badIds) {
2024-07-01 03:28:40 -05:00
console.log("\n\nDoing automatic cleanup");
const content = await fs.readFile(f);
const data = gt.po.parse(content, "utf-8");
2024-07-01 03:28:40 -05:00
const translations = data.translations[""];
const keys = Object.keys(translations);
for (const key of keys) {
property = translations[key];
2024-07-01 03:28:40 -05:00
if (badIds.includes(property.msgid)) {
console.log("----> deleting", property.msgid);
delete data.translations[""][key];
}
}
2024-07-01 03:28:40 -05:00
const buff = gt.po.compile(data, { sort: true });
await fs.writeFile(f, buff);
}
async function findExecutionTimeTranslations() {
2024-07-01 03:28:40 -05:00
const { stdout } = await execFile("grep", [
"-r",
"-h",
"-F",
"(tr (",
"./src",
]);
console.log(stdout);
}
async function welcome() {
2024-07-01 03:28:40 -05:00
console.log(
"####################################################################",
);
console.log(
"# UNUSED TRANSLATIONS FINDER #",
);
console.log(
"####################################################################",
);
console.log("\n");
console.log(
"DISCLAIMER: Some translations are only available at execution time.",
);
console.log(" This finder can't process them, so there can be");
console.log(" false positives.\n");
console.log(" If you want to do an automatic clean anyway,");
console.log(" call the script with:");
console.log(" npm run find-unused-translations -- --clean");
console.log(" For example:");
console.log(
"--------------------------------------------------------------------",
);
await findExecutionTimeTranslations();
2024-07-01 03:28:40 -05:00
console.log(
"--------------------------------------------------------------------",
);
}
const doCleanup = process.argv.slice(2)[0] == "--clean";
2024-07-01 03:28:40 -05:00
(async () => {
await welcome();
const target = path.normalize("./translations/en.po");
const badIds = await processFile(target);
2024-07-01 03:28:40 -05:00
if (doCleanup) {
cleanFile(target, badIds);
} else {
2024-07-01 03:28:40 -05:00
for (const badId of badIds) {
console.log(badId);
}
}
2024-07-01 03:28:40 -05:00
})();