i18nLint.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 defaults = {};
  7. const i18nRoot = path.join(__dirname, '../packages/apps/public/locales');
  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', 'translation.json'].includes(entry)
  16. )
  17. .sort();
  18. }
  19. function checkLanguage (lang) {
  20. console.log(`*** Checking ${lang}`);
  21. const langRoot = path.join(i18nRoot, lang);
  22. const entries = getEntries(langRoot);
  23. const roots = Object.keys(defaults);
  24. const missing = roots.filter((entry) => !entries.includes(entry));
  25. if (missing.length) {
  26. console.log(`\ttop-level missing ${missing.length}: ${missing.join(', ')}`);
  27. }
  28. entries.forEach((entry) => {
  29. const json = require(path.join(langRoot, entry));
  30. const keys = Object.keys(json);
  31. const root = defaults[entry];
  32. if (!root) {
  33. console.log(`\t> ${entry} not found in default, not checking`);
  34. return;
  35. }
  36. const missing = root.filter((key) => !keys.includes(key));
  37. const extra = keys.filter((key) => !root.includes(key));
  38. if (missing.length) {
  39. console.log(`\t> ${entry} ${missing.length} keys missing`);
  40. missing.forEach((key) =>
  41. console.log(`\t\t${key}`)
  42. );
  43. }
  44. if (extra.length) {
  45. console.log(`\t> ${entry} ${extra.length} keys extra`);
  46. extra.forEach((key) =>
  47. console.log(`\t\t${key}`)
  48. );
  49. }
  50. });
  51. }
  52. function checkLanguages () {
  53. fs
  54. .readdirSync(i18nRoot)
  55. .filter((entry) =>
  56. !['.', '..'].includes(entry) &&
  57. fs.lstatSync(path.join(i18nRoot, entry)).isDirectory() &&
  58. entry !== 'en'
  59. )
  60. .sort()
  61. .forEach(checkLanguage);
  62. }
  63. function initDefault () {
  64. const enRoot = path.join(i18nRoot, 'en');
  65. getEntries(enRoot).forEach((entry) => {
  66. const json = require(path.join(enRoot, entry));
  67. const keys = Object.keys(json);
  68. // if (keys.length > 0) {
  69. // console.log(`${entry} ${keys.length} keys`);
  70. // }
  71. defaults[entry] = keys;
  72. });
  73. }
  74. initDefault();
  75. checkLanguages();