nuxt-modules / i18n

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

Cookie Value Error #2822

Closed Fiyuva closed 6 months ago

Fiyuva commented 7 months ago

Environment

Reproduction

i18n: { baseUrl: process.env.BASE_URL, langDir: "locales/", strategy: "no_prefix", skipSettingLocaleOnNavigate: true, defaultDirection: "ltr", defaultLocale: "tr", detectBrowserLanguage: { useCookie: true, fallbackLocale: "tr", alwaysRedirect: true, cookieSecure: true, redirectOn: "root", cookieKey: "lng" }, locales: [ { code: "tr", iso: "tr-TR", name: "Türkçe", file: "tr.json", dir: "ltr" }, { code: "en", iso: "en-US", name: "English", file: "en.json", dir: "ltr" } ] }

Describe the bug

I work with 2 languages, En and Tr, and I use cookies. It works great when my cookie value is en or tr, but when I change the cookie value manually, it should be tr but it doesn't work. What is my mistake?

i18n: { baseUrl: process.env.BASE_URL, langDir: "locales/", strategy: "no_prefix", skipSettingLocaleOnNavigate: true, defaultDirection: "ltr", defaultLocale: "tr", detectBrowserLanguage: { useCookie: true, fallbackLocale: "tr", alwaysRedirect: true, cookieSecure: true, redirectOn: "root", cookieKey: "lng" }, locales: [ { code: "tr", iso: "tr-TR", name: "Türkçe", file: "tr.json", dir: "ltr" }, { code: "en", iso: "en-US", name: "English", file: "en.json", dir: "ltr" } ] }

Additional context

No response

Logs

No response

github-actions[bot] commented 7 months ago

Would you be able to provide a reproduction? 🙏

More info ### Why do I need to provide a reproduction? Reproductions make it possible for us to triage and fix issues quickly with a relatively small team. It helps us discover the source of the problem, and also can reveal assumptions you or we might be making. ### What will happen? If you've provided a reproduction, we'll remove the label and try to reproduce the issue. If we can, we'll mark it as a bug and prioritise it based on its severity and how many people we think it might affect. If `needs reproduction` labeled issues don't receive any substantial activity (e.g., new comments featuring a reproduction link), we'll close them. That's not because we don't care! At any point, feel free to comment with a reproduction and we'll reopen it. ### How can I create a reproduction? We have a couple of templates for starting with a minimal reproduction: 👉 [Reproduction starter (v8 and higher)](https://stackblitz.com/fork/github/BobbieGoede/nuxt-i18n-starter/tree/v8) 👉 [Reproduction starter (edge)](https://stackblitz.com/fork/github/BobbieGoede/nuxt-i18n-starter/tree/edge) A public GitHub repository is also perfect. 👌 Please ensure that the reproduction is as **minimal** as possible. See more details [in our guide](https://nuxt.com/docs/community/reporting-bugs/#create-a-minimal-reproduction). You might also find these other articles interesting and/or helpful: - [The Importance of Reproductions](https://antfu.me/posts/why-reproductions-are-required) - [How to Generate a Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve)
Fiyuva commented 7 months ago

image image

I tried to reproduce it but failed, so I showed it as a picture.

When the user sets the cookie value to a random value as in the picture and refreshes the page, it does not return to the default value.

If the cookie value is en or tr, the system works perfectly.

The part I don't understand here is why the default doesn't work when the user manually changes the cookie value.

BobbieGoede commented 7 months ago

Based on your description it doesn't sound like a bug, of course editing cookies by hand to unexpected values will result in undefined behaviour.

That said, I can see this happen in an edge case which could happen when a website removes a language which previously would have been saved in a cookie. In such cases it would make sense to redirect/fallback to the default language.

I tried to reproduce it but failed, so I showed it as a picture.

Did you try making it using stackblitz? You may need to open the preview in a new tab for the cookies to work. Even if I understood the issue correctly a reproduction would be useful for me to test a solution on (and add tests for it).

Fiyuva commented 7 months ago

I think I did.

https://stackblitz.com/edit/github-kvjw8r-jgxyrr?file=nuxt.config.ts

When the cookie value is changed manually, it does not return to the default value. How can I solve this problem?

Fiyuva commented 6 months ago

any update?

BobbieGoede commented 6 months ago

Unfortunately I was busy with other issues and my day job, will look into this when I have the time.

BobbieGoede commented 6 months ago

With #2841 merged the locale cookie should revert to the defaultLocale if configured (or undefined if not) if the locale cookie does not match any configured locales.

You can try it out on the edge channel (npm i -D @nuxtjs/i18n@npm:@nuxtjs/i18n npm i -D @nuxtjs/i18n@npm:@nuxtjs/i18n-edge), let me know if you run into any issues!

Fiyuva commented 6 months ago

I did the following steps.

1- npm i -D @nuxtjs/i18n@npm:@nuxtjs/i18n

2- I changed the cookie value to an irrelevant value and refreshed the page. I got the results in the picture above.

I checked what you wrote, but I couldn't correct the mistake. DefaultLocale doesn't seem to be working or I couldn't do it.

BobbieGoede commented 6 months ago

Oh I'm sorry, I made a mistake in the install command 😥 it should be npm i -D @nuxtjs/i18n@npm:@nuxtjs/i18n-edge.

Fiyuva commented 6 months ago

My problem is solved. Thanks