Extract Locals script basic setup

This commit is contained in:
Mayesters 2017-06-12 12:20:50 +02:00
parent e83ace19c5
commit 9b32c764ac
2 changed files with 81 additions and 1 deletions

78
ui/extractLocals.js Normal file
View file

@ -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!');
});
}
});
}
});

View file

@ -6,7 +6,8 @@
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack-dev-server --devtool eval --progress --colors --inline", "dev": "webpack-dev-server --devtool eval --progress --colors --inline",
"precommit": "lint-staged", "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": [ "keywords": [
"lbry" "lbry"
@ -55,6 +56,7 @@
"eslint-plugin-jsx-a11y": "^2.2.3", "eslint-plugin-jsx-a11y": "^2.2.3",
"eslint-plugin-react": "^6.7.1", "eslint-plugin-react": "^6.7.1",
"husky": "^0.13.4", "husky": "^0.13.4",
"i18n-extract": "^0.4.4",
"json-loader": "^0.5.4", "json-loader": "^0.5.4",
"lint-staged": "^3.6.0", "lint-staged": "^3.6.0",
"node-sass": "^3.13.0", "node-sass": "^3.13.0",