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.08k stars 419 forks source link

setLocale not working #306

Closed jonathandion closed 3 years ago

jonathandion commented 7 years ago

Hi,

I try to set the locale dynamically using the setLocale function but when accessing the updated locale with getLocale, I always getting the same output why?

I try i18n.setLocale(req, 'fr'); req.setLocale(req, 'fr');

My code in order

var config = {
    i18n: {
        locales:['en', 'fr'],
        directory: __dirname + '/locales',
        cookie: 'lang',
    }
}
i18n.configure(config.i18n);
app.use(i18n.init);

My middleware before the routing:

app.use(function(req, res, next) {
    var lang_matches = req.url.match( url_has_lang_regex );
       // this is working when the url is /fr
    if((config.i18n.locales).indexOf(lang_matches[1]) >= 0) {
        console.log(lang_matches[1]) // fr
        req.setLocale('fr');
    } else {
        req.setLocale('en');
    }
    console.log(req.getLocale()) // en ?
    next();
});
router.get('/:lang', function(req, res, next) {
  res.render('home.html', {
    message : "Hello World",
    lang : req.getLocale(), // en ???
    htmlClass : 'home'
  });
});

Thanks.

mashpie commented 7 years ago

hm, as far as I can see... you've implemented i18n in a way like in it's test:

did you try using app.get instead of router.get? I thought router could refer to an isolated instance, see http://expressjs.com/en/4x/api.html#router:

A router object is an isolated instance of middleware and routes. You can think of it as a “mini-application,” capable only of performing middleware and routing functions. Every Express application has a built-in app router.

or initialize i18n within your router?

martijnbeets commented 6 years ago

Same issue here. I'm using the most simple script:

const i18n = require('i18n');

i18n.configure({
  locales: ['en_UK', 'nl_NL', 'de_DE', 'es_ES'],
  defaultLocale: 'en_EN',
  updateFiles: false,
  directory: './locales'
});

i18n.setLocale('nl_NL');

console.log(i18n.getLocale());

This logs en_EN to the console instead of nl_NL. Using Node v8.10.0 and i18n v0.8.3

alieslamifard commented 6 years ago

in your app.js

  1. if you want to set locale globally for your all path and restfull api

    i18n.configure({
    locales: ['fa'],
    directory: path.resolve('./locales')
    });
    app.use(i18n.init);
    app.use((req, res, next) => {
    res.setLocale('fa');
    next();
    });
  2. if you want to set it just in one method, just do this:

    router.get('/', async (req, res) => {
    res.setLocale('fa');
    // your code is here
    }

is it clear enough?

mashpie commented 3 years ago

should be :)

as written here https://github.com/mashpie/i18n-node#i18nsetlocale

and https://github.com/mashpie/i18n-node/blob/master/test/i18n.setLocale.js + https://github.com/mashpie/i18n-node/blob/master/test/i18n.setLocaleDefaultLanguage.js

and https://github.com/mashpie/i18n-node/blob/master/examples/express4-setLocale/index.js