All files / src/lang list_missing.js

0% Statements 0/41
0% Branches 0/12
0% Functions 0/4
0% Lines 0/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                   
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable global-require */
/* eslint-disable import/no-dynamic-require */
/* eslint-disable no-console */
const fs = require("fs");
const locale = process.argv[2];
 
Iif (!locale) {
  console.log("No language to check specified");
  console.log("");
  console.log("Run as:");
  console.log("npm run missing-translations pt-PT");
  console.log("");
  process.exit();
}
 
Iif (locale === "en") {
  console.log("Can't run script on English language file.");
  console.log("");
  process.exit();
}
 
console.log(`Check for missing translation keys in ${locale}.json`);
console.log("");
 
const en = require(`${__dirname}/en.json`);
let translation = {};
try {
  translation = require(`${__dirname}/${locale}.json`);
} catch (e) {
  console.log(`Translation file not found ${locale}.json`);
  console.log("Creating...");
  console.log("");
}
 
const newTranslation = {};
const removedKeys = [];
const untranslatedKeys = [];
Object.keys(en).forEach((key) => {
  Iif (
    !key.startsWith("//") &&
    typeof en[key] === "string" &&
    (translation[key] === undefined || translation[key] === en[key])
  ) {
    untranslatedKeys.push(key);
  }
  newTranslation[key] = translation[key] || en[key];
});
 
Object.keys(translation).forEach((key) => {
  Iif (en[key] === undefined) {
    removedKeys.push(key);
  }
});
 
Iif (removedKeys.length > 0) {
  console.log("Removed keys that were no longer in use");
  console.log(
    removedKeys.map((key) => `  ${key}: "${translation[key]}"`).join("\n")
  );
}
 
Iif (untranslatedKeys.length > 0) {
  console.log("Untranslated keys:");
  console.log(
    untranslatedKeys.map((key) => `  ${key}: "${en[key]}"`).join("\n")
  );
}
 
fs.writeFileSync(
  `${__dirname}/${locale}.json`,
  JSON.stringify(newTranslation, null, 2)
);