-
Notifications
You must be signed in to change notification settings - Fork 852
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df390d8
commit 682a583
Showing
5 changed files
with
107 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
locale-check.js | ||
node_modules | ||
.next | ||
out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
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', | ||
] | ||
|
||
const allStrings = {}; | ||
|
||
const localeFile = require('./locales/en/common.json'); | ||
|
||
const files = fs.readdirSync('./', { recursive: true, withFileTypes: true }); | ||
//console.log('files:', files); | ||
|
||
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("'", ''); | ||
// console.log('match:', match); | ||
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('"', ''); | ||
// console.log('match:', match, id); | ||
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) => { | ||
// console.log('match:', match); | ||
const parts = match.replace('AuthLayout ', ''); | ||
parts.split(' ').forEach((part) => { | ||
const id = part.startsWith('heading=') | ||
? part.replace('heading="', '').replace('"', '') | ||
: part.replace('description="', '').replace('"', ''); | ||
// console.log('part:', part, id); | ||
|
||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters