mashpie / i18n-node

Lightweight simple translation module for node.js / express.js with dynamic json storage. Uses common __('...') syntax in app and templates.
MIT License
3.09k stars 421 forks source link

Duplicate `lang` query param in the url causes `request.language.toLowerCase is not a function` error #422

Closed nick-fytros closed 4 years ago

nick-fytros commented 4 years ago

Hello,

If there is more than one url query parameter that sets the language (req.query.lang for example) the request.language.toLowerCase is not a function error is thrown. The error is thrown in:

// a query parameter overwrites all
if (queryParameter && request.url) {
  var urlObj = url.parse(request.url, true);
  if (urlObj.query[queryParameter]) {
    logDebug('Overriding locale from query: ' + urlObj.query[queryParameter]);
    request.language = urlObj.query[queryParameter];

    if (preserveLegacyCase) {
      request.language = request.language.toLowerCase();
    }

    return i18n.setLocale(request, request.language);
  }
 }

at the request.language = request.language.toLowerCase(); line. One solution would be to check if the query param is an array and always take the last element but I would like to hear opinions on it.

mashpie commented 4 years ago

fix in PR #442 + regression test (pending)

mashpie commented 4 years ago

released as 0.11.0 to npm