From 54960f3c7ad389cefb0a9395ed55ba6ba8a877ea Mon Sep 17 00:00:00 2001 From: Johannes Millan Date: Mon, 15 Jul 2019 16:36:23 +0200 Subject: [PATCH] chore: add custom watch task for translation extraction --- package.json | 3 +- tools/extract-i18n-single.js | 3 ++ tools/extract-i18n-watch.js | 7 +++++ tools/extract-i18n.js | 57 ++++++++++++++++++------------------ 4 files changed, 41 insertions(+), 29 deletions(-) create mode 100644 tools/extract-i18n-single.js create mode 100644 tools/extract-i18n-watch.js diff --git a/package.json b/package.json index 5d5145192..ef38e8415 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,8 @@ "release.changelog": "conventional-changelog -i CHANGELOG.md -s -p angular", "version": "yarn release.changelog && git add -A", "buildSchema": "cd tools/schematics && yarn build && cd ../../ && yarn add file:./tools/schematics", - "int": "node ./tools/extract-i18n.js", + "int": "node ./tools/extract-i18n-single.js", + "int:watch": "node ./tools/extract-i18n-watch.js", "intFind": "ngx-translate-extract --input ./src --output ./src/assets/i18n/*.json --sort --format namespaced-json --marker _", "intClean": "ngx-translate-extract --input ./src --output ./src/assets/i18n/*.json --clean --sort --format namespaced-json --marker _" }, diff --git a/tools/extract-i18n-single.js b/tools/extract-i18n-single.js new file mode 100644 index 000000000..649cd451b --- /dev/null +++ b/tools/extract-i18n-single.js @@ -0,0 +1,3 @@ +const extractI18n = require('./extract-i18n'); + +extractI18n(); diff --git a/tools/extract-i18n-watch.js b/tools/extract-i18n-watch.js new file mode 100644 index 000000000..dfec13158 --- /dev/null +++ b/tools/extract-i18n-watch.js @@ -0,0 +1,7 @@ +const extractI18n = require('./extract-i18n'); +const fs = require('fs'); +const MAIN_TRANSLATION_FILE = __dirname + '/../src/assets/i18n/en.json'; + +fs.watchFile(MAIN_TRANSLATION_FILE, () => { + extractI18n(); +}); diff --git a/tools/extract-i18n.js b/tools/extract-i18n.js index 5d6bee9b2..bdb86bc2c 100644 --- a/tools/extract-i18n.js +++ b/tools/extract-i18n.js @@ -1,37 +1,38 @@ const fs = require('fs'); const path = __dirname + '/../src/assets/i18n/en.json'; -console.log(path); -const _tr = fs.readFileSync(path); -const tr = JSON.parse(_tr); +module.exports = () => { + const _tr = fs.readFileSync(path); + const tr = JSON.parse(_tr); -const parse = (o, pref = '') => { - return Object.keys(o).reduce((acc, key) => { - console.log(key); + const parse = (o, pref = '') => { + return Object.keys(o).reduce((acc, key) => { + console.log(key); - if (typeof o[key] === 'object') { - return { - ...acc, - [key]: parse(o[key], key + '.'), - }; - } else if (typeof o[key] === 'string') { - return { - ...acc, - [key]: pref + key, - }; - } else { - throw 'Invalid Data'; - } - }, {}); -}; + if (typeof o[key] === 'object') { + return { + ...acc, + [key]: parse(o[key], pref + key + '.'), + }; + } else if (typeof o[key] === 'string') { + return { + ...acc, + [key]: pref + key, + }; + } else { + throw 'Invalid Data'; + } + }, {}); + }; -const parsed = parse(tr); -console.log(parsed); -const string = `export const T = ${JSON.stringify(parsed, null, 2)}; + const parsed = parse(tr); + console.log(parsed); + const string = `export const T = ${JSON.stringify(parsed, null, 2)}; `.replace(/"/g, '\''); -fs.writeFileSync(__dirname + '/../src/app/t.const.ts', string, { - overwrite: true, - flag: 'w' -}); + fs.writeFileSync(__dirname + '/../src/app/t.const.ts', string, { + overwrite: true, + flag: 'w' + }); +};