generateI18n.ts 695 Bytes
Newer Older
Viacheslav Pavlov's avatar
i18n  
Viacheslav Pavlov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
const fg = require('fast-glob');
const _ = require('lodash');
const fs = require('fs');


fg([`./src/**/translations.json`, `./src/translations.json`])
    .then((entries) => entries.map((path) => getTranslations(path)))
    .then((content) => fs.writeFileSync(`locales/en/translations.json`, `{\n${content}}`));


const getTranslations = (path) => {
  const prefix = path.substring(path.indexOf('./src/') + './src/'.length, path.indexOf('/translations.json'));
  console.log('Loading translations of module', prefix);
  const fileContent = fs.readFileSync(path, 'utf8');

  return  prefix !== '/' ? _(`"${prefix}": ${fileContent}\n`).value() : fileContent.substr(1, fileContent.length - 2);
};