Auto-Detect Language Based off Browser (SOLVED!)

Haha thank you so much! Glad it helped!

Thanks,
Luke

Does this has to be applied to site or just the home page?

Hi philippe12,

The code needs to be applied to each page, making sure you update the URL so that it correctly reflects the page that you are editing.

Thanks,
Luke

Thanks for the help Luke!!
I’ve tested this on test page before applying it to all my sites but in the testing phase is not working. I guess it needs to be apply directly on a purchased domain?

Hi philippe12,

You don’t need to have a purchased domain for this to work. Some common mistakes that people make with this code:

  1. Check the URL you are specifing in your code. It has to be EXACTLY what will be displayed in your browser search bar. Keep an eye out for “/” or “https” vs “http”
  2. Remove “www.” from the URL you specify in your code… Don’t need it.
  3. Make sure you’re publishing your site and testing the code on a live site (doesn’t have to be premium. Free works just fine).

Let me know if any of those work and if not, feel free to copy/paste your code and I’ll take a look.

Thanks,
Luke

Hello Luke,
First of all, thank you so much, your codes really really helped a lot.
I had a same problem like yours from the beginning, last night I was struggling with your first code as it reloads again and again endlessly, then I found these replies and your second code which was really helpful, I adjusted it to my site, it worked pretty well though my site is with 10 pages. (Main English, Sub Korean)
However I faced with another issue that even if I click the English language button, then it redirects again to Korean. Is there any way that clicking language button goes first than checking browser’s locale? So what I want is redirection happens only once when user first comes into my site then after that it doesn’t check again so that language button actually work well.

Hi contact,

You’re welcome! Glad to help any way I can.

So it shouldn’t be doing that with the code that I have (I’ve tested it and it works fine). The reason why I have that if statement is for that very reason. It’s basically asking the question “Is the URL the standard one without ?lang=en (or whatever language) and if so, then run the code.” So by selecting the button, it will add ?lang=<> at the end of your URL and thus the code will no longer run. Does that make sense? So I’d check to make sure your if statement is correct and using === (equal to). If you’re still confused, feel free to copy/paste your code and I’ll take a look at it.

Thanks,
Luke

@lukefrostzw Thanks a lot Luke, I will try again with your advice. I will reach you soon.
Thanks,

My website is https://www.heyyacht.com/ Main language is in English, so when user comes in, it shows in English, however what I found now is, if I click the language menu tab and change into Korean, it goes to https://www.heyyacht.com/?lang=ko which is very normal up to now. Then I click the language menu tab and change into English, it goes to https://www.heyyacht.com/ it doesn’t have ?lang=en at the end. Maybe this is my problem?

@lukefrostzw I copied and pasted your second code and it works ok. 2 more problems came up.
At first, my website https://www.heyyacht.com/ has several subpages, let’s say https://www.heyyacht.com/marinas if someone comes to https://www.heyyacht.com/marinas then it doesn’t redirect to Korean version, of course it’s because I didn’t put any code on https://www.heyyacht.com/marinas page. Do you think it’s good to put the codes into all the subpages as well? My website is already too slow and I’m afraid if it slows down more.

Secondly, this is a big problem. Now the website redirects to Korean to Korean users, it’s good but when I click the language menu button and change it into English, in a second, it redirects to Korean, I understand why it goes like that. Is there any way I can resolve this problem?
What is the reason I don’t have ?lang=en on my English website, if I had ?lang=en on my English website, it wouldn’t cause any problem.

@contact75439 Yeah so for your first question, put the code on every page. Only way you’ll get it to work correctly. It shouldn’t slow your site down very much at all, it’s pretty basic code.

With the second thing there I’m not sure why it wouldn’t put ?lang=en at the end of the URL when you click the english option manually from the menu. You may have to ask Wix about that and see what they say because to me it seems like the button isn’t functioning as it should (or at least as I’m familiar with how it should).

Thanks,
Luke

I wrote the smallest (I think) source code to automatically redirect an english-language browser to the english version of the Wix multilingual site.

You should be able to adapt different language (it-IT, es-ES, fr-FR, etc).

Remember to put the code on the first/home page replace the variable with your domain name ( without www).

import wixLocation from ‘wix-location’;
import wixWindow from ‘wix-window’;

$w.onReady( function () {
let url = wixLocation.url;
let browserLocale = wixWindow.browserLocale;

if ((url === ‘https://www.’) && ((browserLocale === ‘en-GB’) || (browserLocale === ‘en-US’))) {
wixLocation.to(‘https://en.’);
}
});

Thanks Alessandro. Let me double check because I tested it and something is not right…
I have one browser in english and the other in spanish. Both of them open the page in english and then re open it in spanish with the following code:

import wixLocation from 'wix-location';
import wixWindow from 'wix-window';

$w.onReady(function () {

 let url = wixLocation.url;
 let browserLocale = wixWindow.browserLocale;
 if ((url === 'https://www.xxx.com') && ((browserLocale === 'es-ES') || (browserLocale === 'en-US'))) {
    wixLocation.to('https://xxx.com/?lang=es');
  }
});

Then I tried this one because of that issue, but in this case there’s no re direction:

import wixLocation from 'wix-location';
import wixWindow from 'wix-window';

$w.onReady(function () {

 let url = wixLocation.url;
 let browserLocale = wixWindow.browserLocale;
 if ((url === 'https://www.xxx.com') && ((browserLocale === 'es-ES'))) {
    wixLocation.to('https://xxx.com/?lang=es');
  }
});