Skip to content

Instantly share code, notes, and snippets.

@one-more
Created April 13, 2020 09:24
Show Gist options
  • Save one-more/2b8b9760d3380b895779bdf46d566290 to your computer and use it in GitHub Desktop.
Save one-more/2b8b9760d3380b895779bdf46d566290 to your computer and use it in GitHub Desktop.
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = require('fs');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const en = require('../resources/en.json');
function generateFromLangObj(lng, prefix = '') {
return Object.keys(lng)
.reduce((acc, key) => {
const i18nKey = (prefix ? prefix + '.' : '') + key;
if (typeof lng[key] === 'object') {
return acc.concat(generateFromLangObj(lng[key], i18nKey));
}
return acc.concat([`'${i18nKey}'`]);
}, [])
.join('|');
}
function generateEnDeclarations() {
return 'export type i18nKey = ' + generateFromLangObj(en.translations) + `|''`;
}
const enDeclarationsPath = path.resolve(__dirname, '../en.ts');
fs.writeFileSync(enDeclarationsPath, generateEnDeclarations());
fs.watchFile(enDeclarationsPath, generateEnDeclarations);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment