Skip to content

Commit

Permalink
Merge pull request #218 from TrigenSoftware/master
Browse files Browse the repository at this point in the history
res -> locals -> res recursion escaping
  • Loading branch information
mashpie committed Mar 3, 2016
2 parents 57ebc5f + a8849db commit 3bbf62c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
26 changes: 24 additions & 2 deletions i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,12 +436,26 @@ module.exports = (function() {

// consider res
if (targetObject.res && !skipImplicitObjects) {
i18n.setLocale(targetObject.res, targetObject.locale);

// escape recursion
if (targetObject.res.locals) {
i18n.setLocale(targetObject.res, targetObject.locale, true);
i18n.setLocale(targetObject.res.locals, targetObject.locale, true);
} else {
i18n.setLocale(targetObject.res, targetObject.locale);
}
}

// consider locals
if (targetObject.locals && !skipImplicitObjects) {
i18n.setLocale(targetObject.locals, targetObject.locale);

// escape recursion
if (targetObject.locals.res) {
i18n.setLocale(targetObject.locals, targetObject.locale, true);
i18n.setLocale(targetObject.locals.res, targetObject.locale, true);
} else {
i18n.setLocale(targetObject.locals, targetObject.locale);
}
}

return i18n.getLocale(targetObject);
Expand Down Expand Up @@ -574,13 +588,16 @@ module.exports = (function() {
*/
var applyAPItoObject = function(object) {

var alreadySetted = true;

// attach to itself if not provided
for (var method in api) {
if (api.hasOwnProperty(method)) {
var alias = api[method];

// be kind rewind, or better not touch anything already existing
if (!object[alias]) {
alreadySetted = false;
object[alias] = i18n[method].bind(object);
}
}
Expand All @@ -591,6 +608,11 @@ module.exports = (function() {
object.locale = defaultLocale;
}

// escape recursion
if (alreadySetted) {
return;
}

// attach to response if present (ie. in express)
if (object.res) {
applyAPItoObject(object.res);
Expand Down
14 changes: 14 additions & 0 deletions test/i18n.configureApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,18 @@ describe('configure api', function() {
});
should.equal(typeof customObject.__, 'undefined');
});

it('should escape res -> locals -> res recursion', function() {
var customObject = {};
customObject.locals = { res: customObject };
reconfigure({
locales: ['en', 'de'],
register: customObject,
api: {
'__': 't'
}
});
should.equal(typeof customObject.t, 'function');
should.equal(typeof customObject.locals.t, 'function');
});
});
14 changes: 14 additions & 0 deletions test/i18n.setLocale.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,18 @@ describe('Locale switching should work on req and res', function() {
res.locals.__('Hello').should.equal('Bonjour');
});


it('setLocale(object) should escape res -> locals -> res recursion', function() {
// add locals to res
res.locals = { res: res };

// add res to req to simulate express 4.x schema
req.res = res;
i18n.init(req, res);
i18n.setLocale(req, 'fr');

res.locale.should.equal('fr');
res.locals.locale.should.equal('fr');
});

});

0 comments on commit 3bbf62c

Please sign in to comment.