i18next / i18next-express-middleware

[deprecated] can be replaced with i18next-http-middleware
https://github.com/i18next/i18next-http-middleware
MIT License
206 stars 52 forks source link

Changing Language doesn't work #120

Closed jonaslund closed 8 years ago

jonaslund commented 8 years ago

Hi

Nice middleware!!

Question.

Trying to set the current language through this

app.get("/lang/:lan?", function(req, res) {  
  let lan = req.params.lan;  
  req.i18n.changeLanguage(newlan);
...

But it doesn't work? This how I initialise i18n

i18next
  .use(i18nextmiddleware.LanguageDetector)
  .use(filesystemBackend)
  .init({
    // debug: true,
    saveMissing: true,
    lngs: ['en', "pt", "pt-BR", "en-US"],
    load: 'all',
    fallbackLng : "en",
    backend: {
      loadPath: __dirname + '/locales/{{lng}}/{{ns}}.json'
    }
});

what am I missing?

jamuhl commented 8 years ago

you might not need to use the languagedetector if setting it yourself - or remove you setting it and let the detector do its job: https://github.com/i18next/i18next-express-middleware#detector-options (enabling the path in order array)

jonaslund commented 8 years ago

I was misunderstanding the logic, and thought that

req.i18n.changeLanguage(newlan);

would set the language and keep it in a session, which is not the default behaviour, and the

  req.i18n.changeLanguage(newlan);

needs to be called directly before rendering any output