Here is the language picking logic I would like to achieve:
Until the users don’t touch the language selector on the website, they should have the page delivered in the language based on the accept-language header (through netlify redirect).
After they explicitly switch their language on the website, I would like to cache this choice and on the consequent visits I would like to deliver them the page in last picked language (through netlify redirect) - not relying anymore on the accept-language header.
Is this something that is possible or should I just rely on the accept-language header all the time? My thinking is that someone has it set up wrongly but doesn’t now how to fix it and just wants his site to automatically be in hungarian language - but according to the accept-language header he will always get the english one first and always has to switch it manually.
It’s possible to achieve this, however not advisable because of this:
With that being said, you can choose to use Country based redirects if you see fit or continue using Language based redirects depending on your use case.
I believe you’re not having issues with automatic redirects, are you? If it’s just the manual redirects that you need to take care of, you simply need to set a cookie with the name nf_lang and the language code and the user would automatically be redirected accordingly.
Let me reflect to make sure I understand the situation: accept-language redirects are not working well with multiple language preferences, only with one.
Country is fine for the the automatic language then. How does country behave if it doesn’t match any available language? Is it possible to set up a fallback?
If the user explicitly selects a language in the website, I should set the nf_lang cookie with the country code then. How can I configure the redirects for this? (how would netlify know how to choose redirect logic between for example: example.com/hu/article-1 or hu.example.com/article-1 or example.hu/article-1). Also same question regarding a fallback language if the language code from the nf_lang cookie is no longer available.
Country based redirects work on user’s IP address. So as long as the IP address reports the country as hu, it would redirect fine. If the IP address doesn’t match that country, the rule is ignored.
The language code, not the country code. You need to use country code if you use country-based redirects and language code if you use language based redirects. The cookie name changes too. nf_country and nf_lang.
No extra configuration needed - the same redirects that you set for language/country based redirects would handle the cookie too.
This is what happens:
Set a cookie
Reload the page
Browser will send the request with the cookie automatically
Netlify will receive the request
Netlify will look for the file to serve and parse the redirects configuration
If the cookie value corresponds to a redirect rule, you’d get a redirected response, or else the rule will be ignored.
I think there’s some confusion. Before I provide you with a snippet, it’s better to get that cleared out.
You should probably not configure both (language as well as country based) redirects. It’s not required in your case. Both work automatically in the absence of a cookie and the cookie takes preference when it’s set.
So, considering you choose to use the country based redirects, you simply need to create a _redirects file like:
The above would automatically redirect all requests to the required destination when a user visits from that country (IP based detection). You don’t need to add any fallback rule.
Now, in the frontend, you simply need to set a cookie with the name nf_cookie and value as any of the country codes. If someone chooses English, you should still set a cookie with en as the value. When that cookie is sent with the request Netlify will respect that cookie regardless from where the request was made. So, as long as the cookie exists, anyone from HU can also access English content.
What the end UX should be: If a user with an IP based in HU explicitly chooses EN language on the website, I set a cookie and after that the same user from HU should be automatically redirected to the EN page regardless of his country. So basically the cookie’s value should override the IP based redirect’s value after that. I think you understood it well, I just clarified to make sure we are on the same page
I’m saying “I believe” because I’m not perfectly sure what would happen when you set a cookie with the name nf_country and value as us and not add a rule for that country. I would think it would behave as we expect it to, but let us know your findings.