build-locales-coverage.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. const { readdirSync, writeFileSync } = require("fs");
  2. const files = readdirSync(`${__dirname}/../src/locales`);
  3. const flatten = (object = {}, result = {}, extraKey = "") => {
  4. for (const key in object) {
  5. if (typeof object[key] !== "object") {
  6. result[extraKey + key] = object[key];
  7. } else {
  8. flatten(object[key], result, `${extraKey}${key}.`);
  9. }
  10. }
  11. return result;
  12. };
  13. const locales = files.filter(
  14. (file) => file !== "README.md" && file !== "percentages.json",
  15. );
  16. const percentages = {};
  17. for (let index = 0; index < locales.length; index++) {
  18. const currentLocale = locales[index];
  19. const data = flatten(require(`${__dirname}/../src/locales/${currentLocale}`));
  20. const allKeys = Object.keys(data);
  21. const translatedKeys = allKeys.filter((item) => data[item] !== "");
  22. const percentage = Math.floor((100 * translatedKeys.length) / allKeys.length);
  23. percentages[currentLocale.replace(".json", "")] = percentage;
  24. }
  25. writeFileSync(
  26. `${__dirname}/../src/locales/percentages.json`,
  27. `${JSON.stringify(percentages, null, 2)}\n`,
  28. "utf8",
  29. );