forked from boxyhq/saas-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-locale.js
96 lines (84 loc) · 2.54 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
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
95
96
const fs = require('fs');
const path = require('path');
const regExp = /\bt\('(.*?)'/gm;
const altRegExp = /\bi18nKey="(.*?)"/gm;
// const authLayoutRegExp =
// /\bAuthLayout.*\bheading="(.*?)".*\bdescription="(.*?)"/gm;
const authHeadingRegExp = /\bheading="(.*?)"/gm;
const authDescriptionRegExp = /\bdescription="(.*?)"/gm;
const exceptionList = [
'email-verified',
'allow-only-work-email',
'verify-account-expired',
'confirm-your-email',
'exceeded-login-attempts',
'account-unlocked',
'invalid-credentials',
'no-credentials',
'token-not-found',
]
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}`
);
}
});
[authHeadingRegExp, authDescriptionRegExp].forEach((regExp) => {
const authGroups = fileContent.match(regExp) || [];
authGroups.forEach((match) => {
const parts = match.replace('AuthLayout ', '');
parts.split(' ').forEach((part) => {
const id = part.startsWith('heading=')
? part.replace('heading="', '').replace('"', '')
: part.replace('description="', '').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] && !exceptionList.includes(key)) {
error = true;
console.error(`Unused key: ${key}`);
}
});
if (error) {
process.exit(1);
}