Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Setup a i18n scanner command - Closes #769
Browse files Browse the repository at this point in the history
  • Loading branch information
slaweet committed Sep 25, 2017
1 parent 742e826 commit d2679ba
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"./features/*/*.js",
"./src/**/stories.js",
"./src/tests.js"
"./src/i18n-scanner.js"
]
}
],
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"clean": "del app/dist -f",
"eslint": "eslint ./src/ ./app/main.js ./features/",
"storybook": "start-storybook -p 6006 -s ./src/",
"i18n-scanner": "node ./src/i18n-scanner.js",
"build-storybook": "build-storybook"
},
"author": "Lisk Foundation <admin@lisk.io>, lightcurve GmbH <admin@lightcurve.io>",
Expand Down Expand Up @@ -87,6 +88,8 @@
"exports-loader": "=0.6.3",
"extract-text-webpack-plugin": "=2.1.2",
"file-loader": "=0.9.0",
"glob": "=7.1.2",
"i18next-scanner": "=2.0.0",
"imports-loader": "=0.6.5",
"js-nacl": "=1.2.2",
"json-loader": "=0.5.4",
Expand Down
24 changes: 24 additions & 0 deletions src/i18n-scanner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const fs = require('fs');
const glob = require('glob');
const Parser = require('i18next-scanner').Parser;

const translationFunctionNames = ['i18next.t', 'props.t', 'this.props.t', 't'];
const outputFilePath = './src/locales/en/common.json';

const parser = new Parser();

const customHandler = function (key) {
parser.set(key, key);
};

const files = glob.sync('./src/**/*.js', {});
files.forEach((file) => {
const content = fs.readFileSync(file, 'utf-8');
parser.parseFuncFromString(content, { list: translationFunctionNames }, customHandler)
});

const translations = parser.get({ sort: true }).en.translation;
const count = Object.keys(translations).length;
const outputJSON = JSON.stringify(translations, null, 2);
fs.writeFileSync(outputFilePath, outputJSON);
console.log(`${count} translation keys parsed and written to '${outputFilePath}'`);

0 comments on commit d2679ba

Please sign in to comment.