forked from boxyhq/jackson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-locale.js
54 lines (44 loc) · 1.35 KB
/
check-locale.js
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
const fs = require('fs');
const path = require('path');
const regExp = /\bt\('(.*?)'/gm;
const altRegExp = /\bi18nKey='(.*?)'/gm;
const allStrings = {};
const localeFile = require('./locales/en/common.json');
const files = fs.readdirSync('./', { recursive: true, withFileTypes: true });
let error = false;
files.forEach((file) => {
if (file.isDirectory()) {
return;
}
if (file.path.includes('node_modules')) {
return;
}
if (['.ts', '.tsx'].includes(path.extname(file.name).toLowerCase())) {
const fileContent = fs.readFileSync(path.join(file.path, file.name), 'utf8');
(fileContent.match(regExp) || []).forEach((match) => {
const id = match.replace("t('", '').replace("'", '');
allStrings[id] = true;
if (!localeFile[id]) {
error = true;
console.error(`Missing key: ${path.join(file.path, file.name)} - ${id}`);
}
});
(fileContent.match(altRegExp) || []).forEach((match) => {
const id = match.replace("i18nKey='", '').replace("'", '');
allStrings[id] = true;
if (!localeFile[id]) {
error = true;
console.error(`Missing key: ${path.join(file.path, file.name)} - ${id}`);
}
});
}
});
Object.keys(localeFile).forEach((key) => {
if (!allStrings[key]) {
error = true;
console.error(`Unused key: ${key}`);
}
});
if (error) {
process.exit(1);
}