Redirect to browser language based subdomain

Asked

Viewed 265 times

1

What is the correct method to direct a site’s navigation to a specific page based on the user’s browser language?

I have a website (www.site.com) with the default language in English and I need that, when accessed in a browser with the Portuguese language (en), it redirects navigation to the subdomain I created and already hosted the translated files (br.site.com).

The same for Spanish (es.site.com) and Italian (it.site.com).

What should I do? What kind of code should I enter?

PS: I already have the pages translated and hosted in the respective subdomains. I just need to know how to identify the browser language and direct automatically.

How is this done in code? Javascript? PHP? Ajax? Via htacess? Someone has this line of code to share?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My site</title>
</head>
<body>
    <p>The book is on the table.</p>
</body>
</html>

  • http://answall.com/questions/81073/how-pega-o-idioma-do-usu%C3%A1rio-do-navegador

2 answers

3


You can do the following (put this only on the site in English, which is the default, to not go into infinite loop):

const sites = {
  'pt-br': 'http://br.site.com',
  'it': 'http://it.site.com',
  'es': 'http://es.site.com'
};

var lang = navigator.language || navigator.userLanguage;
lang = lang.toLowerCase(); // é possível a lang do navegador ser por ex: pt-BR
if(typeof sites[lang] !== 'undefined') {
  // window.location.replace(sites[lang]);
  alert('redirecionar para ' +sites[lang]);
}

Codes for browser languages

I commented on the redirect, but that’s what you should use.

I transform the language of the browser returned to minuscula because it is very possible that it is for ex: pt-BR, en-US, and we must have in accordance with the keys of our object sites

  • Hello, Miguel! Thank you so much for your contribution. That’s exactly how I expected the answer. Exactly: The default will be English. Where do I put this code?

  • @Will, do you want to redirect from all of them? If you put this code in all of them you will go into an infinite loop. I will edit to avoid this

  • It will be better, Miguel! But where do I insert this code in the sample code of the question? No head? Do I create a <php> tag and paste this code inside? I’m a beginner. I only work with html, css and jquery so far. I don’t know anything about PHP

  • @Will puts this code only on the site in English, yes it can be on the head. so: <script>CODIGO POSTADO EM CIMA</script>

  • Perfect, Miguel! I’ll do it and I’ll give you some feedback!

  • I uploaded the Portuguese html to br.nutryfruty.com and entered the above code in the head of www.nutryfruty.com, as can be seen in F12. But it did not direct. I did something wrong?

  • Is it exactly as it is on top? I made an edit that might be the @Will key if you do alert(lang); before the if what gives?

Show 3 more comments

0

You can use javascript for this, see below for example:

var lang = navigator.language;
alert(lang);

  • Hello, Matheus! Thank you so much for your contribution! In this case, would I enter this code from the head of the default page? (www.site.com) like this: <script> var lang = Navigator.language; Alert(lang);</script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.