Skip to content

Commit

Permalink
more tests and removal of (now) useless code
Browse files Browse the repository at this point in the history
  • Loading branch information
mashpie committed Feb 13, 2016
1 parent ea469df commit d23aff4
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 14 deletions.
21 changes: 7 additions & 14 deletions i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,10 @@ i18n.init = function i18nInit(request, response, next) {
if (typeof response === 'object') {
applyAPItoObject(response);

// init that locale to response too - @todo: refactor
if (!response.locale) response.locale = request.locale;

// and set that locale to response too
i18n.setLocale(response, request.locale);
}

// register locale to res.locals so hbs helpers know this.locale
if (response && typeof response.locals === 'object') {
applyAPItoObject(response.locals);

// init locale to res.locals so hbs helpers know this.locale - @todo: refactor
if (!response.locals.locale) response.locals.locale = request.locale;

// and set that locale to response.locals too
i18n.setLocale(response.locals, request.locale);
}

// head over to next callback when bound as middleware
if (typeof next === 'function') {
next();
Expand Down Expand Up @@ -411,13 +397,20 @@ function applyAPItoObject(object) {
}
});

// set initial locale if not set
if(!object.locale){
object.locale = defaultLocale;
}

// attach to response if present (ie. in express)
if(object.res){
applyAPItoObject(object.res);
}

// attach to locals if present (ie. in express)
if(object.locals){
applyAPItoObject(object.locals);
}
}

/**
Expand Down
143 changes: 143 additions & 0 deletions test/i18n.setLocale.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,147 @@ describe('Locale switching should work on req and res', function() {
res.__('Hello').should.equal('Bonjour');
});

it('setLocale() should switch locale for req and res implicitly', function() {
// add res to req to simulate express 4.x schema
req.res = res;
i18n.init(req, res);
req.setLocale('fr');

i18n.getLocale(req).should.equal('fr');
i18n.getLocale(res).should.equal('fr');

req.getLocale().should.equal('fr');
res.getLocale().should.equal('fr');

req.__('Hello').should.equal('Bonjour');
res.__('Hello').should.equal('Bonjour');
});

it('setLocale() should switch locale for req and res implicitly (alternative notation)', function() {
// add res to req to simulate express 4.x schema
req.res = res;
i18n.init(req, res);
i18n.setLocale(req, 'fr');

i18n.getLocale(req).should.equal('fr');
i18n.getLocale(res).should.equal('fr');

req.getLocale().should.equal('fr');
res.getLocale().should.equal('fr');

req.__('Hello').should.equal('Bonjour');
res.__('Hello').should.equal('Bonjour');
});

it('setLocale() should switch locale for req, res and res.locals implicitly', function() {
// add locals to res
res.locals = {};

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

i18n.getLocale(req).should.equal('fr');
i18n.getLocale(res).should.equal('fr');
i18n.getLocale(res.locals).should.equal('fr');

req.getLocale().should.equal('fr');
res.getLocale().should.equal('fr');
res.locals.getLocale().should.equal('fr');

req.__('Hello').should.equal('Bonjour');
res.__('Hello').should.equal('Bonjour');
res.locals.__('Hello').should.equal('Bonjour');
});

it('setLocale() should switch locale for req, res and res.locals implicitly when set on req', function() {
// add locals to res
res.locals = {};

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

i18n.getLocale(req).should.equal('fr');
i18n.getLocale(res).should.equal('fr');
i18n.getLocale(res.locals).should.equal('fr');

req.getLocale().should.equal('fr');
res.getLocale().should.equal('fr');
res.locals.getLocale().should.equal('fr');

req.__('Hello').should.equal('Bonjour');
res.__('Hello').should.equal('Bonjour');
res.locals.__('Hello').should.equal('Bonjour');
});

it('setLocale() should switch locale for req, res and res.locals implicitly when set on res', function() {
// add locals to res
res.locals = {};

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

i18n.getLocale(req).should.equal('de');
i18n.getLocale(res).should.equal('fr');
i18n.getLocale(res.locals).should.equal('fr');

req.getLocale().should.equal('de');
res.getLocale().should.equal('fr');
res.locals.getLocale().should.equal('fr');

req.__('Hello').should.equal('Hallo');
res.__('Hello').should.equal('Bonjour');
res.locals.__('Hello').should.equal('Bonjour');
});

it('setLocale() should switch locale for req, res and res.locals implicitly when set on res.locals', function() {
// add locals to res
res.locals = {};

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

i18n.getLocale(req).should.equal('de');
i18n.getLocale(res).should.equal('de');
i18n.getLocale(res.locals).should.equal('fr');

req.getLocale().should.equal('de');
res.getLocale().should.equal('de');
res.locals.getLocale().should.equal('fr');

req.__('Hello').should.equal('Hallo');
res.__('Hello').should.equal('Hallo');
res.locals.__('Hello').should.equal('Bonjour');
});


it('setLocale() should switch locale for req, res and res.locals implicitly when set as array', function() {
// add locals to res
res.locals = {};

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

i18n.getLocale(req).should.equal('fr');
i18n.getLocale(res).should.equal('fr');
i18n.getLocale(res.locals).should.equal('fr');

req.getLocale().should.equal('fr');
res.getLocale().should.equal('fr');
res.locals.getLocale().should.equal('fr');

req.__('Hello').should.equal('Bonjour');
res.__('Hello').should.equal('Bonjour');
res.locals.__('Hello').should.equal('Bonjour');
});

});

0 comments on commit d23aff4

Please sign in to comment.