jeresig / i18n-node-2

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

setLocaleFromQuery() isn't working #121

Closed nazmul-prince closed 5 years ago

nazmul-prince commented 6 years ago

Hi,

I've added following middle ware to set locale in each request using query (?lang=fi):

var express = require('express'),
i18n = require('i18n-2');

i18n.expressBind(app,
    {
        locales:['en', 'fi', 'nb', 'es'],
        defaultLocale: 'en',
        cookieName: 'locale'
    });

app.use(function(req, res, next) {
    req.i18n.setLocaleFromQuery();
    next();
});

Then whenever I'm trying console.log(req.i18n.__("Hello")) then its always giving translation of "Hello" in en locale(English) although I'm using other locale in the query like the following myApi.com/getUserInfo?lang=fi

The locale is always set to en!!!

What am I missing? Thanks in advance

dzucconi commented 5 years ago

I believe you need to pass the req object into the method:

req.i18n.setLocaleFromQuery(req);