i18nSort.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2017-2020 @polkadot/apps authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. const fs = require('fs');
  5. const path = require('path');
  6. const i18nRoot = path.join(__dirname, '../packages/apps/public/locales');
  7. const SKIP_NS = ['app-123code', 'app-dashboard', 'app-i18n', 'translation'].map((f) => `${f}.json`);
  8. function getEntries (langRoot) {
  9. return fs
  10. .readdirSync(langRoot)
  11. .filter((entry) =>
  12. !['.', '..'].includes(entry) &&
  13. fs.lstatSync(path.join(langRoot, entry)).isFile() &&
  14. entry.endsWith('.json') &&
  15. !['index.json'].includes(entry)
  16. )
  17. .sort();
  18. }
  19. function sortLanguage (lang) {
  20. const langRoot = path.join(i18nRoot, lang);
  21. const entries = getEntries(langRoot);
  22. const hasKeys = {};
  23. entries.forEach((entry) => {
  24. const filename = path.join(langRoot, entry);
  25. const json = require(filename);
  26. const sorted = Object.keys(json).sort().reduce((result, key) => {
  27. result[key] = json[key];
  28. return result;
  29. }, {});
  30. hasKeys[entry] = Object.keys(sorted).length !== 0;
  31. fs.writeFileSync(filename, JSON.stringify(sorted, null, 2));
  32. });
  33. if (lang === 'en') {
  34. const filtered = entries
  35. .filter((entry) => !SKIP_NS.includes(entry))
  36. .filter((entry) => hasKeys[entry]);
  37. fs.writeFileSync(
  38. path.join(langRoot, 'index.json'),
  39. JSON.stringify(filtered, null, 2)
  40. );
  41. }
  42. }
  43. function checkLanguages () {
  44. const languages = fs
  45. .readdirSync(i18nRoot)
  46. .filter((entry) =>
  47. !['.', '..'].includes(entry) &&
  48. fs.lstatSync(path.join(i18nRoot, entry)).isDirectory()
  49. )
  50. .sort();
  51. languages.forEach(sortLanguage);
  52. fs.writeFileSync(path.join(i18nRoot, 'index.json'), JSON.stringify(languages, null, 2));
  53. }
  54. checkLanguages();