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 async = require('async');
var Languages = {};
var Languages = module.exports;
var languagesPath = path.join(__dirname, '../build/public/language');
Languages.init = function (next) {
@ -27,10 +27,13 @@ Languages.get = function (language, namespace, callback) {
});
};
Languages.list = function (callback) {
var languages = [];
var codeCache = null;
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') {
return callback(null, []);
}
@ -38,43 +41,59 @@ Languages.list = function (callback) {
return callback(err);
}
async.each(files, function (folder, next) {
fs.stat(path.join(languagesPath, folder), function (err, stat) {
if (err) {
return next(err);
var parsed;
try {
parsed = JSON.parse(buffer.toString());
} catch (e) {
return callback(e);
}
if (!stat.isDirectory()) {
return next();
var langs = parsed.languages;
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');
fs.readFile(configPath, function (err, buffer) {
if (err && err.code !== 'ENOENT') {
if (err && err.code === 'ENOENT') {
return next();
}
if (err) {
return next(err);
}
if (buffer) {
try {
var lang = JSON.parse(buffer.toString());
if (lang.name && lang.code && lang.dir) {
languages.push(lang);
}
next(null, lang);
} catch (e) {
next(e);
}
next();
});
});
}, function (err) {
}, function (err, languages) {
if (err) {
return callback(err);
}
// Sort alphabetically
languages = languages.sort(function (a, b) {
return a.code > b.code ? 1 : -1;
// filter out invalid ones
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,
// run through core and all plugins to generate
// a full translation hash

Loading…
Cancel
Save