nuxt-modules / i18n

I18n module for Nuxt
https://i18n.nuxtjs.org
MIT License
1.75k stars 483 forks source link

Redirects not working for translated routes #2939

Open baijii opened 6 months ago

baijii commented 6 months ago

Environment

Reproduction

Reproduction repo: https://github.com/baijii/nuxt_i18n_router_bug

// nuxt.config.ts
export default defineNuxtConfig({
  devtools: { enabled: true },
  modules: ["@nuxtjs/i18n"],
  i18n: {
    locales: [
      { code: 'en', iso: 'en-US', file: 'en.json' },
      { code: 'de', iso: 'de-DE', file: 'de.json' },
    ],
    baseUrl: 'http://localhost:3000',
    defaultLocale: 'en',
    strategy: 'prefix',
    langDir: 'locales/',
    detectBrowserLanguage: {
      useCookie: true,
    }
  }
})

// inside page component
defineI18nRoute({
  paths: {
    en: '/about-us',
    de: '/ueber-uns',
  }
})

Describe the bug

With a German browser (without locale cookie) I navigate to /about-us.

Is: I get redirected to /en/about-us

Should: I get redirected to /de/ueber-uns

With a German browser (with locale cookie de) I navigate to /about-us.

Is: I get a 404 error

Should: I get redirected to /de/ueber-uns

Additional context

I added some cypress tests, that can executed by firstly starting the server:

npm run dev

and then start the cypress tests

npx cypress run

Logs

No response

baijii commented 6 months ago

Just tried out at the @nuxtjs/i18n module repo. Also there it's similar. I set my Browser to french, add strategy: 'prefix' in nuxt.config.ts, open http://localhost:3000 and will be redirected to http://localhost:3000/en instead of http://localhost:3000/fr.