Skip to content

Commit

Permalink
fix(tools): validate keys in all objects
Browse files Browse the repository at this point in the history
Validates the keys in all of the translation JSON objects. Note that
this will return some false positives for dynamic keys (like the
superblock intros). This is okay as the script is not meant to be
used as blocking CI, but only to help us clean up keys as we update
our client/API.
  • Loading branch information
Nicholas Carrigan committed Feb 17, 2021
1 parent 49de5f1 commit 4d4e59a
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions client/i18n/validate-keys.js
@@ -1,6 +1,10 @@
const fs = require('fs');
const path = require('path');
const translationsObject = require('./locales/english/translations.json');
const introObject = require('./locales/english/intro.json');
const metaObject = require('./locales/english/meta-tags.json');
const motivationObject = require('./locales/english/motivation.json');
const trendingObject = require('./locales/english/trending.json');

/**
* Function to flatten a nested object. Written specifically for
Expand All @@ -26,9 +30,11 @@ const flattenAnObject = (obj, namespace = '') => {
return flattened;
};

const flattenedSchema = flattenAnObject(translationsObject);

const keyStrings = Object.keys(flattenedSchema);
const translationKeys = Object.keys(flattenAnObject(translationsObject));
const metaKeys = Object.keys(flattenAnObject(metaObject));
const motivationKeys = Object.keys(flattenAnObject(motivationObject));
const introKeys = Object.keys(flattenAnObject(introObject));
const trendingKeys = Object.keys(flattenAnObject(trendingObject));

/**
* Recursively read through the directory, grabbing .js files
Expand Down Expand Up @@ -58,8 +64,28 @@ const serverCodebase = readComponentCode(
path.join(process.cwd() + '/../api-server/server')
);

for (const key of keyStrings) {
for (const key of translationKeys) {
if (!clientCodebase.includes(key) && !serverCodebase.includes(key)) {
console.warn(`The translation key '${key}' appears to be unused.`);
}
}
for (const key of motivationKeys) {
if (!clientCodebase.includes(key) && !serverCodebase.includes(key)) {
console.warn(`The motivation key '${key}' appears to be unused.`);
}
}
for (const key of metaKeys) {
if (!clientCodebase.includes(key) && !serverCodebase.includes(key)) {
console.warn(`The meta key '${key}' appears to be unused.`);
}
}
for (const key of introKeys) {
if (!clientCodebase.includes(key) && !serverCodebase.includes(key)) {
console.warn(`The intro key '${key}' appears to be unused.`);
}
}
for (const key of trendingKeys) {
if (!clientCodebase.includes(key) && !serverCodebase.includes(key)) {
console.warn(`The trending key '${key}' appears to be unused.`);
}
}

0 comments on commit 4d4e59a

Please sign in to comment.