forked from polkadot-js/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18nLint.cjs
94 lines (74 loc) · 2.24 KB
/
i18nLint.cjs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2017-2021 @polkadot/apps authors & contributors
// SPDX-License-Identifier: Apache-2.0
const fs = require('fs');
const path = require('path');
const defaults = {};
const i18nRoot = path.join(__dirname, '../packages/apps/public/locales');
function getEntries (langRoot) {
return fs
.readdirSync(langRoot)
.filter((entry) =>
!['.', '..'].includes(entry) &&
fs.lstatSync(path.join(langRoot, entry)).isFile() &&
entry.endsWith('.json') &&
!['index.json', 'translation.json'].includes(entry)
)
.sort();
}
function checkLanguage (lang) {
console.log(`*** Checking ${lang}`);
const langRoot = path.join(i18nRoot, lang);
const entries = getEntries(langRoot);
const roots = Object.keys(defaults);
const missing = roots.filter((entry) => !entries.includes(entry));
if (missing.length) {
console.log(`\ttop-level missing ${missing.length}: ${missing.join(', ')}`);
}
entries.forEach((entry) => {
const json = require(path.join(langRoot, entry));
const keys = Object.keys(json);
const root = defaults[entry];
if (!root) {
console.log(`\t> ${entry} not found in default, not checking`);
return;
}
const missing = root.filter((key) => !keys.includes(key));
const extra = keys.filter((key) => !root.includes(key));
if (missing.length) {
console.log(`\t> ${entry} ${missing.length} keys missing`);
missing.forEach((key) =>
console.log(`\t\t${key}`)
);
}
if (extra.length) {
console.log(`\t> ${entry} ${extra.length} keys extra`);
extra.forEach((key) =>
console.log(`\t\t${key}`)
);
}
});
}
function checkLanguages () {
fs
.readdirSync(i18nRoot)
.filter((entry) =>
!['.', '..'].includes(entry) &&
fs.lstatSync(path.join(i18nRoot, entry)).isDirectory() &&
entry !== 'en'
)
.sort()
.forEach(checkLanguage);
}
function initDefault () {
const enRoot = path.join(i18nRoot, 'en');
getEntries(enRoot).forEach((entry) => {
const json = require(path.join(enRoot, entry));
const keys = Object.keys(json);
// if (keys.length > 0) {
// console.log(`${entry} ${keys.length} keys`);
// }
defaults[entry] = keys;
});
}
initDefault();
checkLanguages();