i18next / i18next-http-middleware

i18next-http-middleware is a middleware to be used with Node.js web frameworks like express or Fastify and also for Deno.
MIT License
150 stars 28 forks source link

Language detector seems to always fall back to English #74

Closed grug closed 4 months ago

grug commented 4 months ago

🐛 Bug Report

It seems that no matter how the language detector is configured, English is always being used.

To Reproduce

I've got two sets of translation files (one for English and one for Welsh) in the right place.

I have initialised things like this

  await i18next
    .use(i18nextMiddleware.LanguageDetector)
    .use(Backend)
    .init({
      preload: ['en', 'cy'],
      fallbackLng: 'en',
      supportedLngs: ['en', 'cy'],
      ns: ['common'],
      backend: {
        loadPath: `${path.join(__dirname, 'locales')}/{{lng}}/{{ns}}.json`,
      },
      detection: {
        order: ['querystring', 'cookie', 'header'],
        lookupQuerystring: 'lng',
        lookupCookie: 'i18next',
        lookupHeader: 'accept-language',
      },
    });

  app.use(i18nextMiddleware.handle(i18next));

At this point, I'd have thought specifying lng in the URL would allow me to switch between English and Welsh (i.e. localhost:8000?lng=en gives English and localhost:8000?lng=cy gives Welsh). However, it just gives English either way.

I then tried explicitly setting lng: 'cy' in my init block to make sure it could successfully load the Welsh translations and that I hadn't done something silly - which loaded Welsh fine. So at this point I can't switch languages via URL.

I then added the following block right after my init code to set a cookie-specified language to Welsh:

  //... same code from before is just above this
  app.use((req, res, next) => {
    res.cookie('i18next', 'cy', { maxAge: 900000, httpOnly: true });
    next();
  });

At this point I can see the cookie is set on the client correctly (right name and a value of cy) but the language being shown is still English.

I then tried setting the browser's language to Welsh, hoping that this would affect things, but it also didn't.

I have no other middleware in the project that could interfere with this, so I imagine it's a configuration issue, or a bug.

Expected behavior

I'd expect that given the configuration options that I'd set in my code, that I could switch between languages using the URL/cookies/setting the accept-language header.

Your Environment

adrai commented 4 months ago

Please provide a minimal reproducible example repository like for example here: https://github.com/i18next/i18next-http-middleware/tree/master/example/basic

btw: it seems you have not defined the caches option: https://github.com/i18next/i18next-http-middleware?tab=readme-ov-file#detector-options

image
grug commented 4 months ago

I made a repro repo and in doing so everything worked properly so it must be something with my project setup and not this library. I'll link the repo here anyways as maybe it might be useful to someone: https://github.com/grug/i18next-language-bug-repro

I'll close this now - sorry that this turned out to be a waste of time!

mina4gerges commented 4 months ago

+1