Skip to content

Commit

Permalink
Merge pull request #425 from funktionswerk/master
Browse files Browse the repository at this point in the history
Added support for WHATWG URL API urls
  • Loading branch information
mashpie authored Jan 24, 2020
2 parents 01aa965 + 35de7b1 commit 6d99cc3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,8 @@ module.exports = (function() {

// a query parameter overwrites all
if (queryParameter && request.url) {
var urlObj = url.parse(request.url, true);
var urlAsString = typeof request.url === 'string' ? request.url : request.url.toString();
var urlObj = url.parse(urlAsString, true);
if (urlObj.query[queryParameter]) {
logDebug('Overriding locale from query: ' + urlObj.query[queryParameter]);
request.language = urlObj.query[queryParameter];
Expand Down
7 changes: 7 additions & 0 deletions test/i18n.configureQueryParameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,11 @@ describe('Locale switching should work queryParameter', function() {
res.__('Hello').should.equal('Bonjour');
res.locals.__('Hello').should.equal('Bonjour');
});

it('should support WHATWG URL API', function() {
req.url = new URL('/test?lang=fr', 'http://localhost');
i18n.init(req, res);
i18n.getLocale(req).should.equal('fr');
i18n.getLocale(res).should.equal('fr');
});
});

0 comments on commit 6d99cc3

Please sign in to comment.