move middleware.locals only where it is needed and transform it into a common function, simplify translations and adds automatic language detection

This commit is contained in:
Matteo Pagliazzi
2013-11-12 14:49:01 +01:00
parent b6adc8e1d8
commit 1d9148eebb
5 changed files with 33 additions and 31 deletions
+13 -14
View File
@@ -102,9 +102,14 @@ var getTranslatedString = function(locale, string){
return 'String not found!';
}
module.exports.locals = function(req, res, next) {
res.locals.habitrpg = res.locals.habitrpg || {}
_.defaults(res.locals.habitrpg, {
var getUserLanguage = function(langs){
return _(langs).map(function(lang){
return lang.slice(0, 2);
}).uniq().value()[0];
}
module.exports.locals = function(req) {
return {
NODE_ENV: nconf.get('NODE_ENV'),
BASE_URL: nconf.get('BASE_URL'),
PAYPAL_MERCHANT: nconf.get('PAYPAL_MERCHANT'),
@@ -112,14 +117,8 @@ module.exports.locals = function(req, res, next) {
STRIPE_PUB_KEY: nconf.get('STRIPE_PUB_KEY'),
getManifestFiles: getManifestFiles,
getBuildUrl: getBuildUrl,
getTranslatedString: getTranslatedString
});
next()
}
/*
// translate = (req, res, next) ->
// model = req.getModel()
// # Set locale to bg on dev
// #model.set '_i18n.locale', 'bg' if process.env.NODE_ENV is "development"
// next()
*/
t: function(string){
return getTranslatedString(getUserLanguage(req.acceptedLanguages), string);
}
}
}