Detect browser language and redirect - English Main, Polish additional

Hey guys!

I am making a simple landing page that uses English as its primary language (the main version is set as the homepage). However, if we detect that a visitor has a Polish browser, we want to redirect them to the Polish version of the site.

I tried a few solutions from the forum, but they didn’t work - the last one I tried was:

 var userLang = navigator.language || navigator.userLanguage;
 if(userLang = "pl"){

Also tried solutions form this topic: Detect Browser Language and Redirect

It always redirects users to the “pl/home” instead of staying on the English version (even though they have English OS and Browser language).

Here is my site Read-Only: Webflow - Petscope Alert Sales Page

Hi @Kacper_Bieda as you see in image language request returns string in format "en-gb" (on my computer) and your condition contain only two letters. IMO you can use RegEx or slice to compare these first two letters. Something like this

userLang.slice(0,2) === "pl"

you can find other solutions with more complex conditions on net.

Note: You are using in your code example assignment = instead comparator == || === you can also use negative != || !==

I appreciate your help, thanks! It’s a shame, but I am a complete idiot when it comes to javascript or just coding - know only the basics of CSS & HTML - I would be grateful for being more specific. To be honest, I found this custom codes hoping that it will be working easily, but it can’t be so easy. :smiley:

Hey @Kacper_Bieda , stemming from what @Stan mentioned, you would need to splice your userLang variable.

Give this code a try!!


Thanks! I tried this one, but now it’s showing website only in English version and there is no redirection at all. :frowning:

I have just a thought, that maybe I’m placing this custom code in wrong place - I am adding it in custom code area for the main page in section before .

Since this is a global code for any page of your website, head to the Project Settings > Custom Code > and paste in in the </body> tag section. This will now run on all pages for you.

It’s working now, but just in case - when I’ve pasted it in project settings custom code the website was refreshing all the time.

Heading to solution:

  1. Code from you works perfectly fine! Thanks.
  2. It was working all the time, turned out that from some reason it treats my browser (which is in Polish) as English, but on every other device with Polish language the redirection is working perfectly fine.
  3. To make the website stop refreshing, I had to put it back to before </ body> tag on the homepage custom code.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.