Created
April 13, 2020 09:24
-
-
Save one-more/2b8b9760d3380b895779bdf46d566290 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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