Automatic Redirect Based on Browser Language

SOLVE
BrandonSeerene
Member

We have both a German and English version of our website with a manual language switcher installed. We would like to automatically re-direct traffic from Germany/Austria/Switzerland to the German language website by using the visitor's browser language settings. When we asked customer support, they replied we would need to use a Javascript Function. What's the best way of going about this?

0 Upvotes
1 Accepted solution

Accepted Solutions
piersg
Solution
Key Advisor

Hi @BrandonSeerene (sorry to jump in @Anton 😉). You could do this:

 

const lang = navigator.language
if (lang.includes('de')) {
  window.location = 'https://www.example.com/de'
}

 

The navigator language code for German in Germany/Austria/Switzerland all include 'de' (e.g. de-CH for Swiss German) so you just test if the navigator language includes that substring or not; if it does, load your German site.

View solution in original post

4 Replies 4
dennisedson
Community Manager

Hello @BrandonSeerene 

I feel like this is something @Anton could help you out with 😊

Thanks,

Dennis




Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content
0 Upvotes
piersg
Solution
Key Advisor

Hi @BrandonSeerene (sorry to jump in @Anton 😉). You could do this:

 

const lang = navigator.language
if (lang.includes('de')) {
  window.location = 'https://www.example.com/de'
}

 

The navigator language code for German in Germany/Austria/Switzerland all include 'de' (e.g. de-CH for Swiss German) so you just test if the navigator language includes that substring or not; if it does, load your German site.

View solution in original post

dennisedson
Community Manager

I bet@piersg also cuts in line at the theme parks 😜

Thanks,

Dennis




Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content
piersg
Key Advisor

@dennisedson, I'm British; queueing is sacred in our society and cutting in or jumping the queue is punishable by exile.