Save language metadata on build to avoid readdirs later

v1.18.x
Peter Jaszkowiak 8 years ago
parent 1028327f0a
commit 413bb4736c

@ -4,7 +4,7 @@ var fs = require('fs');
var path = require('path'); var path = require('path');
var async = require('async'); var async = require('async');
var Languages = {}; var Languages = module.exports;
var languagesPath = path.join(__dirname, '../build/public/language'); var languagesPath = path.join(__dirname, '../build/public/language');
Languages.init = function (next) { Languages.init = function (next) {
@ -27,10 +27,13 @@ Languages.get = function (language, namespace, callback) {
}); });
}; };
Languages.list = function (callback) { var codeCache = null;
var languages = []; Languages.listCodes = function (callback) {
if (codeCache && codeCache.length) {
return callback(null, codeCache);
}
fs.readdir(languagesPath, function (err, files) { fs.readFile(path.join(languagesPath, 'metadata.json'), function (err, buffer) {
if (err && err.code === 'ENOENT') { if (err && err.code === 'ENOENT') {
return callback(null, []); return callback(null, []);
} }
@ -38,43 +41,59 @@ Languages.list = function (callback) {
return callback(err); return callback(err);
} }
async.each(files, function (folder, next) { var parsed;
fs.stat(path.join(languagesPath, folder), function (err, stat) { try {
if (err) { parsed = JSON.parse(buffer.toString());
return next(err); } catch (e) {
return callback(e);
} }
if (!stat.isDirectory()) { var langs = parsed.languages;
return next(); codeCache = langs;
callback(null, langs);
});
};
var listCache = null;
Languages.list = function (callback) {
if (listCache && listCache.length) {
return callback(null, listCache);
} }
Languages.listCodes(function (err, codes) {
if (err) {
return callback(err);
}
async.map(codes, function (folder, next) {
var configPath = path.join(languagesPath, folder, 'language.json'); var configPath = path.join(languagesPath, folder, 'language.json');
fs.readFile(configPath, function (err, buffer) { fs.readFile(configPath, function (err, buffer) {
if (err && err.code !== 'ENOENT') { if (err && err.code === 'ENOENT') {
return next();
}
if (err) {
return next(err); return next(err);
} }
if (buffer) { try {
var lang = JSON.parse(buffer.toString()); var lang = JSON.parse(buffer.toString());
if (lang.name && lang.code && lang.dir) { next(null, lang);
languages.push(lang); } catch (e) {
} next(e);
} }
next();
}); });
}); }, function (err, languages) {
}, function (err) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
// Sort alphabetically
languages = languages.sort(function (a, b) { // filter out invalid ones
return a.code > b.code ? 1 : -1; languages = languages.filter(function (lang) {
return lang.code && lang.name && lang.dir;
}); });
callback(err, languages); listCache = languages;
callback(null, languages);
}); });
}); });
}; };
module.exports = Languages;

@ -102,6 +102,25 @@ function getTranslationTree(callback) {
}); });
}, },
// save a list of languages to `${buildLanguagesPath}/metadata.json`
// avoids readdirs later on
function (ref, next) {
async.waterfall([
function (next) {
mkdirp(buildLanguagesPath, next);
},
function (x, next) {
fs.writeFile(path.join(buildLanguagesPath, 'metadata.json'), JSON.stringify({
languages: ref.languages.sort(),
namespaces: ref.namespaces.sort(),
}), next);
},
function (next) {
next(null, ref);
},
], next);
},
// for each language and namespace combination, // for each language and namespace combination,
// run through core and all plugins to generate // run through core and all plugins to generate
// a full translation hash // a full translation hash

Loading…
Cancel
Save