From 9b32c764ac65fc9da8ca59b301e9741be0fdf24c Mon Sep 17 00:00:00 2001 From: Mayesters Date: Mon, 12 Jun 2017 12:20:50 +0200 Subject: [PATCH] Extract Locals script basic setup --- ui/extractLocals.js | 78 +++++++++++++++++++++++++++++++++++++++++++++ ui/package.json | 4 ++- 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 ui/extractLocals.js diff --git a/ui/extractLocals.js b/ui/extractLocals.js new file mode 100644 index 000000000..542b6806a --- /dev/null +++ b/ui/extractLocals.js @@ -0,0 +1,78 @@ +var fs = require('fs'); +var path = require('path'); +var extract = require('i18n-extract'); +var enLocale = require('../app/dist/locales/en.json'); + +var walk = function(dir, done) { + var results = []; + fs.readdir(dir, function(err, list) { + if (err) return done(err); + var pending = list.length; + if (!pending) return done(null, results); + list.forEach(function(file) { + file = path.resolve(dir, file); + fs.stat(file, function(err, stat) { + if (stat && stat.isDirectory()) { + results.push(file + "/*.js"); + results.push(file + "/*.jsx"); + walk(file, function(err, res) { + results = results.concat(res); + if (!--pending) done(null, results); + }); + } else { + // results.push(file); + if (!--pending) done(null, results); + } + }); + }); + }); +}; + +walk('js/', function(err, results) { + if (err) throw err; + results.push('js/*.js') + results.push('js/*.jsx') + const keys = extract.extractFromFiles(results, { + marker: '__', + }); + + let reports = []; + reports = reports.concat(extract.findMissing(enLocale, keys)); + reports = reports.concat(extract.findUnused(enLocale, keys)); + reports = reports.concat(extract.findDuplicated(enLocale, keys)); + + if (reports.length > 0) { + fs.readFile('../app/dist/locales/en.json', 'utf8', function readFileCallback(err, data){ + if (err){ + console.log(err); + } else { + localeObj = JSON.parse(data); + + for (var i = 0; i < reports.length; i++) { + if (reports[i].type === 'MISSING') { + localeObj[reports[i].key] = reports[i].key; + } else if (reports[i].type === 'UNUSED') { + console.log("Found unused String in en.json, but beware: This may be a String from api.lbry.io, do not blindly delete!") + console.log(reports[i]); + } else if (reports[i].type == "DUPLICATED") { + console.log("Found duplicated String in en.json!") + console.log(reports[i]); + } else { + console.log("Found unknown type of String in en.json!") + console.log(reports[i]); + } + } + + var json = JSON.stringify(localeObj, null, '\t'); //convert it back to json + fs.writeFile('../app/dist/locales/en.json', json, 'utf8', function callback(err) { + if (err) throw err; + console.log('It\'s saved!'); + }); + } + }); + } +}); + + + + diff --git a/ui/package.json b/ui/package.json index 9c4907ba4..b78304ff6 100644 --- a/ui/package.json +++ b/ui/package.json @@ -6,7 +6,8 @@ "test": "echo \"Error: no test specified\" && exit 1", "dev": "webpack-dev-server --devtool eval --progress --colors --inline", "precommit": "lint-staged", - "prettier": "prettier --trailing-comma es5 --write js/**/*.{js,jsx}" + "prettier": "prettier --trailing-comma es5 --write js/**/*.{js,jsx}", + "extract-langs": "node extractLocals.js" }, "keywords": [ "lbry" @@ -55,6 +56,7 @@ "eslint-plugin-jsx-a11y": "^2.2.3", "eslint-plugin-react": "^6.7.1", "husky": "^0.13.4", + "i18n-extract": "^0.4.4", "json-loader": "^0.5.4", "lint-staged": "^3.6.0", "node-sass": "^3.13.0",