Home
Support Forums

Custom language redirect logic

Hey guys,
Bit of a netlify-newbie here.

I have a gatsby app where I set up my pages to be example.com/hu/random-page, example.com/it/random-page etc. (the default english language remains example.com/random-page).

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.

Hi @electronic33,

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.

Hi @hrishikesh , thank you for the answer! :slight_smile:

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.

Have a good day! :slight_smile:

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:

  1. Set a cookie
  2. Reload the page
  3. Browser will send the request with the cookie automatically
  4. Netlify will receive the request
  5. Netlify will look for the file to serve and parse the redirects configuration
  6. If the cookie value corresponds to a redirect rule, you’d get a redirected response, or else the rule will be ignored.

Thank you for the detailed answer, I understand the concept now.

I’m already satisfied with the support I got, but I would be even happier if you could provide me with a snippet of configuration about how you would set this up with this example:

I have 3 supported languages (hu,ro,en) with the default being en and they would map in terms of url like this: hu → example.com/hu/any-page; ro → example.com/ro/any-page; → en: example.com/any-page. I would need a country based automatic redirect and a language cookie manual redirect (both following the same rules).

Thank you in advance :slight_smile:

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:

/* /hu/:splat 302! Country=hu
/* /ro/:splat 302! Country=ro

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.

Yes, that is perfect.

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 :sweat_smile:

Yeah I believe that would happen.

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.

Great, I’ll let you know then :slight_smile: Thank you for your help