Skip to content

Commit

Permalink
Write code to programmatically check if our translations are up-to-da…
Browse files Browse the repository at this point in the history
…te (#902)

create update tracker that checks if translations are up-to-date
  • Loading branch information
psh0078 authored Jul 3, 2024
1 parent baaa496 commit 63bdfb3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"eslint-plugin-svelte": "^2.39.0",
"globals": "^13.24.0",
"graphql": "^16.8.1",
"json5": "^2.2.3",
"jwt-decode": "^4.0.0",
"postcss": "^8.4.38",
"rimraf": "^5.0.7",
Expand Down
3 changes: 3 additions & 0 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions frontend/src/lib/i18n/locales/update_tracker.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as fs from 'fs';
import * as path from 'path';
import json5 from 'json5';

function compileKeys(obj, prefix = '') {
let keys = [];
for (const key in obj) {
const fullKey = prefix ? `${prefix}.${key}` : key;
keys.push(fullKey);
const subKey = obj[key];
if (typeof subKey !== 'string') {
const subKeys = compileKeys(subKey, fullKey);
keys = keys.concat(subKeys);
}
}
return keys;
}

function getDiffKeys(stdKeys, lang) {
const langJson = json5.parse(fs.readFileSync(`${lang}.json`, 'utf-8'));
const langKeysSet = new Set(compileKeys(langJson, ''));
const diff = stdKeys.filter(x => !langKeysSet.has(x));
return diff;
}

function main() {
const dirname = import.meta.dirname;
const files = fs.readdirSync(dirname);
const locales = files
.filter(file => path.extname(file).toLowerCase() === '.json')
.map(file => path.basename(file, '.json'))
.filter(file => file !== 'en');

const enJson = json5.parse(fs.readFileSync('en.json', 'utf-8'));
const enAllKeys = compileKeys(enJson, '');

const output = {};
locales.forEach(lang => {
const diff = getDiffKeys(enAllKeys, lang);
output[lang] = diff;
});
console.log(output);
}

main();

0 comments on commit 63bdfb3

Please sign in to comment.