forked from polkadot-js/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18nSort.cjs
68 lines (53 loc) · 1.69 KB
/
i18nSort.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
// Copyright 2017-2021 @polkadot/apps authors & contributors
// SPDX-License-Identifier: Apache-2.0
const fs = require('fs');
const path = require('path');
const i18nRoot = path.join(__dirname, '../packages/apps/public/locales');
const SKIP_NS = ['translation'].map((f) => `${f}.json`);
function getEntries (langRoot) {
return fs
.readdirSync(langRoot)
.filter((entry) =>
!['.', '..'].includes(entry) &&
fs.lstatSync(path.join(langRoot, entry)).isFile() &&
entry.endsWith('.json') &&
!['index.json'].includes(entry)
)
.sort();
}
function sortLanguage (lang) {
const langRoot = path.join(i18nRoot, lang);
const entries = getEntries(langRoot);
const hasKeys = {};
entries.forEach((entry) => {
const filename = path.join(langRoot, entry);
const json = require(filename);
const sorted = Object.keys(json).sort().reduce((result, key) => {
result[key] = json[key];
return result;
}, {});
hasKeys[entry] = Object.keys(sorted).length !== 0;
fs.writeFileSync(filename, JSON.stringify(sorted, null, 2));
});
if (lang === 'en') {
const filtered = entries
.filter((entry) => !SKIP_NS.includes(entry))
.filter((entry) => hasKeys[entry]);
fs.writeFileSync(
path.join(langRoot, 'index.json'),
JSON.stringify(filtered, null, 2)
);
}
}
function checkLanguages () {
const languages = fs
.readdirSync(i18nRoot)
.filter((entry) =>
!['.', '..'].includes(entry) &&
fs.lstatSync(path.join(i18nRoot, entry)).isDirectory()
)
.sort();
languages.forEach(sortLanguage);
fs.writeFileSync(path.join(i18nRoot, 'index.json'), JSON.stringify(languages, null, 2));
}
checkLanguages();