-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write code to programmatically check if our translations are up-to-da…
…te (#902) create update tracker that checks if translations are up-to-date
- Loading branch information
Showing
3 changed files
with
49 additions
and
0 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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(); |