Skip to content

Commit

Permalink
Set cookie at language url detection and keep it otherwise
Browse files Browse the repository at this point in the history
  • Loading branch information
edouardkombo committed Nov 12, 2015
1 parent 3ba2dff commit 9ddcd88
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions app.js
Expand Up @@ -41,7 +41,8 @@ var gaikan = require('gaikan'),
captcha = require('./core/' + config.captcha),
multer = require('multer'),
bodyParser = require('body-parser'),
mailer = require('./core/mailer')(app);
mailer = require('./core/mailer')(app),
langCookieName = config.cookie.prefix + 'lang';

/* Enable trusted proxy */

Expand Down Expand Up @@ -139,7 +140,7 @@ for (var md in modules) app.use(express.static(path.join(__dirname, 'modules/' +

I18n.expressBind(app, {
locales: config.locales.avail,
cookieName: config.cookie.prefix,
cookieName: langCookieName,
directory: path.join(__dirname, 'core', 'lang'),
extension: '.js',
devMode: app.get('config').locales.dev_mode
Expand Down Expand Up @@ -213,10 +214,6 @@ app.use(function(req, res, next) {
next(err);
return;
}
/* Set locales from query and from cookie */
if (app.get('config').locales.detect_from_cookie) req.i18n.setLocaleFromCookie();
if (app.get('config').locales.detect_from_subdomain) req.i18n.setLocaleFromSubdomain();
if (app.get('config').locales.detect_from_query) req.i18n.setLocaleFromQuery();
/* Logging */
logger.info(req.ip + " " + res.statusCode + " " + req.method + ' ' + req.url, {});
/* Clear auth_redirect if already authorized */
Expand All @@ -231,8 +228,24 @@ app.use(function(req, res, next) {
var _timestamp_settings_query = {};

app.use(function(req, res, next) {
var _lng = req.i18n.getLocale();
req.session.current_locale = _lng;

/* Get current language depending on cookies, url or subdomain */
var _lng;
if (req.query.lang) {
if (app.get('config').locales.detect_from_query) req.i18n.setLocaleFromQuery();
_lng = req.query.lang;
res.cookie(langCookieName, _lng);
} else {
/* Set locales from query and from cookie */
if (app.get('config').locales.detect_from_cookie) req.i18n.setLocaleFromCookie();
if (app.get('config').locales.detect_from_subdomain) req.i18n.setLocaleFromSubdomain();
_lng = req.i18n.getLocale();
}
if (req.session) {
req.session.current_locale = _lng;
}


if (_timestamp_settings_query._lng && Date.now() - _timestamp_settings_query._lng <= 60000) {
next();
return;
Expand Down

0 comments on commit 9ddcd88

Please sign in to comment.