mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-08-03 08:21:07 +00:00
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:
parent
b6adc8e1d8
commit
1d9148eebb
5 changed files with 33 additions and 31 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,23 +2,36 @@ var nconf = require('nconf');
|
|||
var express = require('express');
|
||||
var router = new express.Router();
|
||||
var _ = require('lodash');
|
||||
var middleware = require('../middleware')
|
||||
|
||||
// -------- App --------
|
||||
router.get('/', function(req, res) {
|
||||
return res.render('index', {
|
||||
title: 'HabitRPG | Your Life, The Role Playing Game',
|
||||
env: res.locals.habitrpg
|
||||
env: middleware.locals(req)
|
||||
});
|
||||
});
|
||||
|
||||
// -------- Marketing --------
|
||||
|
||||
router.get('/splash.html', function(req, res) {
|
||||
res.redirect('/static/front');
|
||||
router.get('/static/front', function(req, res) {
|
||||
var env = middleware.locals(req);
|
||||
env.isFrontPage = true;
|
||||
res.render('static/front', {env: env});
|
||||
});
|
||||
|
||||
router.get('/static/front', function(req, res) {
|
||||
res.render('static/front', {env: res.locals.habitrpg, isFrontPage: true});
|
||||
router.get('/static/privacy', function(req, res) {
|
||||
res.render('static/privacy', {env: middleware.locals(req)});
|
||||
});
|
||||
|
||||
router.get('/static/terms', function(req, res) {
|
||||
res.render('static/terms', {env: middleware.locals(req)});
|
||||
});
|
||||
|
||||
// --------- Redirects --------
|
||||
|
||||
router.get('/splash.html', function(req, res) {
|
||||
res.redirect('/static/front');
|
||||
});
|
||||
|
||||
router.get('/static/about', function(req, res) {
|
||||
|
|
@ -37,13 +50,4 @@ router.get('/static/faq', function(req, res) {
|
|||
res.redirect('http://community.habitrpg.com/faq-page');
|
||||
});
|
||||
|
||||
router.get('/static/privacy', function(req, res) {
|
||||
res.render('static/privacy', {env: res.locals.habitrpg});
|
||||
});
|
||||
|
||||
router.get('/static/terms', function(req, res) {
|
||||
res.render('static/terms', {env: res.locals.habitrpg});
|
||||
});
|
||||
|
||||
|
||||
module.exports = router;
|
||||
|
|
@ -94,7 +94,6 @@ app.use(express.cookieParser());
|
|||
app.use(express.cookieSession({ secret: nconf.get('SESSION_SECRET'), httpOnly: false, cookie: { maxAge: TWO_WEEKS }}));
|
||||
//app.use(express.session());
|
||||
app.use(middleware.splash);
|
||||
app.use(middleware.locals);
|
||||
|
||||
// Initialize Passport! Also use passport.session() middleware, to support
|
||||
// persistent login sessions (recommended).
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ footer.footer(ng-controller='FooterCtrl')
|
|||
.span3
|
||||
h4 Company
|
||||
ul.unstyled
|
||||
if (!isFrontPage)
|
||||
if (!env.isFrontPage)
|
||||
li
|
||||
.btn.btn-small.btn-success(ng-click='modals.buyGems = true')
|
||||
i.icon-heart.icon-white
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ script(id='templates/habitrpg-tasks.html', type="text/ng-template")
|
|||
// Ads
|
||||
div(ng-if='main && !user.purchased.ads && list.type!="reward"')
|
||||
span.pull-right
|
||||
a(ng-click='modals.buyGems=true', tooltip=env.getTranslatedString('it', 'removeAds'))
|
||||
a(ng-click='modals.buyGems=true', tooltip=env.t('removeAds'))
|
||||
i.icon-remove
|
||||
// Habit3
|
||||
ins.adsbygoogle(ng-init='initAds()', style='display: inline-block; width: 234px; height: 60px;', data-ad-client='ca-pub-3242350243827794', data-ad-slot='9529624576')
|
||||
|
|
|
|||
Loading…
Reference in a new issue