Skip to content

Commit

Permalink
Feature/check translation script (#905)
Browse files Browse the repository at this point in the history
* Add: Script to check translation files

Added checkTranslation file to check for translation file for missing keys or missing file for better UX and DX

* Added JS script for translation checking

* Add: check:translation script in package.json

* Update package.json

Add missing comma to line 21 as required to run by json format rules.

---------

Co-authored-by: Kylee Fields <[email protected]>
  • Loading branch information
anmol-fzr and kyleecodes committed May 17, 2024
1 parent 303a108 commit 4d1f716
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"test:watch": "jest --watch",
"cypress": "cypress open",
"cypress:headless": "cypress run --headless true --browser chrome",
"check:translations": "node ./scripts/checkTranlation.js",
"postinstall": "husky",
"prepare": "husky"
},
Expand Down
60 changes: 60 additions & 0 deletions scripts/checkTranlation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const fs = require('node:fs')
const path = require('path')

const languages = ['de', 'en', 'es', 'fr', 'hi', 'pt']

const getKeys = (file, keys, prefix = '') => {
keys ??= new Set();
for (const key of Object.keys(file)) {
if (typeof file[key] === 'object') {
getKeys(file[key], keys, key)
}
else {
keys.add(prefix ? prefix + '.' + key : key)
}
}
return keys
}

const getFileByPath = (dir, fileName) => {
const filePath = path.join(__dirname, '..', 'messages', dir, fileName);
return require(filePath)
}

const directories = fs.readdirSync(path.join(__dirname, '..', 'messages'));

for (const directory of directories) {
if (languages.includes(directory)) continue;

const files = fs.readdirSync(
path.join(__dirname, '..', 'messages', directory)
);

const langFiles = files.filter(file => languages.includes(file.split('.')[0]));

if (langFiles.length !== languages.length) {
console.warn(`Missing translation files for directory ${directory}:`,
languages.filter(lang => !langFiles.includes(lang + '.json')));
continue;
}

let expectedKeys = new Set();
for (const langFile of langFiles) {
const file = getFileByPath(directory, langFile)
const prevLen = expectedKeys.size
const newKeys = getKeys(file)
const newLen = newKeys.size
if (newLen > prevLen) {
expectedKeys = newKeys
}
}

for (const langFile of langFiles) {
const filePath = path.join(__dirname, '..', 'messages', directory, langFile);
const currKeys = getKeys(require(filePath))

if (expectedKeys.size !== currKeys.size) {
console.log(`${directory}/${langFile} file is missing required keys: ${Array.from(expectedKeys).filter(key => !currKeys.has(key))}`);
}
}
}

0 comments on commit 4d1f716

Please sign in to comment.