Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

res -> locals -> res recursion escaping #218

Merged
merged 3 commits into from
Mar 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});

});