mirror of
https://github.com/HeyPuter/puter
synced 2024-11-14 22:06:00 +00:00
Check that all uses of i18n() use keys that exist
If code uses `i18n('foo')` and 'foo' is not in the dictionary for the en translation, then this script will now report it as an error. This has already helped catch a few mistakes.
This commit is contained in:
parent
29b3b4ecba
commit
8f8b7f0fbf
@ -53,6 +53,7 @@ async function checkTranslationRegistrations() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure that translations only contain keys that exist in the en dictionary
|
||||||
function checkTranslationKeys() {
|
function checkTranslationKeys() {
|
||||||
const enDictionary = translations.en.dictionary;
|
const enDictionary = translations.en.dictionary;
|
||||||
|
|
||||||
@ -71,8 +72,40 @@ function checkTranslationKeys() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure that all keys passed to i18n() exist in the en dictionary
|
||||||
|
async function checkTranslationUsage() {
|
||||||
|
const enDictionary = translations.en.dictionary;
|
||||||
|
|
||||||
|
const sourceDirectories = [
|
||||||
|
'./src/helpers',
|
||||||
|
'./src/UI',
|
||||||
|
];
|
||||||
|
|
||||||
|
// Looks for i18n() calls using either ' or " for the key string.
|
||||||
|
// The key itself is at index 2 of the result.
|
||||||
|
const i18nRegex = /i18n\((['"])(.*?)\1\)/g;
|
||||||
|
|
||||||
|
for (const dir of sourceDirectories) {
|
||||||
|
const files = await fs.promises.readdir(dir, { recursive: true });
|
||||||
|
for (const relativeFileName of files) {
|
||||||
|
if (!relativeFileName.endsWith('.js')) continue;
|
||||||
|
const fileName = `${dir}/${relativeFileName}`;
|
||||||
|
|
||||||
|
const fileContents = await fs.promises.readFile(fileName, { encoding: 'utf8' });
|
||||||
|
const i18nUses = fileContents.matchAll(i18nRegex);
|
||||||
|
for (const use of i18nUses) {
|
||||||
|
const key = use[2];
|
||||||
|
if (!enDictionary.hasOwnProperty(key)) {
|
||||||
|
reportError(`Unrecognized i18n key: call ${use[0]} in ${fileName}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await checkTranslationRegistrations();
|
await checkTranslationRegistrations();
|
||||||
checkTranslationKeys();
|
checkTranslationKeys();
|
||||||
|
await checkTranslationUsage();
|
||||||
|
|
||||||
if (hadError) {
|
if (hadError) {
|
||||||
process.stdout.write('Errors were found in translation files.\n');
|
process.stdout.write('Errors were found in translation files.\n');
|
||||||
|
Loading…
Reference in New Issue
Block a user