CMS Development

BrandonSeerene
メンバー

Automatic Redirect Based on Browser Language

解決

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?

1件の承認済みベストアンサー
piersg
解決策
キーアドバイザー

Automatic Redirect Based on Browser Language

解決

Ahhh haha of course. That's my fault, sorry.

<script>
const lang = navigator.language;
const langRedirected = localStorage.getItem("languageRedirected");
if (lang.includes('de') && (langRedirected === null || langRedirected === false)) {
  localStorage.setItem("languageRedirected", true);
  window.location = 'https://www.example.com/de'
}
</script>

元の投稿で解決策を見る

14件の返信
piersg
キーアドバイザー

Automatic Redirect Based on Browser Language

解決

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.

Sascha
投稿者

Automatic Redirect Based on Browser Language

解決

Hi, where would I put this snippet? I tried the usual place but it doesn't seem to work. Appreciate any assistance 🙂

 

0 いいね!
Girdhari
メンバー

Automatic Redirect Based on Browser Language

解決

Hello Creators, 

I need quick help. I have a language switcher dropdown in my header. Now I want Automatic Redirect Based on Browser Language. Please share relevant js code related this.What I need is the possibility to redirect visitors to the appropriate language of my page. So if my browser is set to German language and I visit my site, I am automatically redirected to the German https://www.example.com/de/ site. If the browser settings are set to the English language, the visitor is automatically redirected to the https://www.example.com/enI have tried this but there is no luck.        

    $( document ).ready(function(){
        var userLang = navigator.language || navigator.userLanguage;
        if (userLang == "de") {
           window.location.href = "https://www.example.com/de/"
        }    });
 

Thanks in Advanced.

0 いいね!
piersg
キーアドバイザー

Automatic Redirect Based on Browser Language

解決

Hmm...I don't see any reason why this wouldn't work (for German only, obviously, you haven't added English to this so if it's not working for your English pages that would be why). Where are you adding this code?

0 いいね!
Sascha
投稿者

Automatic Redirect Based on Browser Language

解決

I have tried adding the code snippet to Settings > Pages > Templates > Site Header HTML. There I get this error:

 

Screenshot 2022-11-10 at 12.35.09@2x.jpg

 

When I try adding it to Site Footer HTML instead, I don't get an error, but it does not seem to do anything. When I browse to the English (primary language) page, it does not automatically switch to the german version. 

 

I am probably adding this at the wrong place?

 

Thanks for your help!

 
0 いいね!
piersg
キーアドバイザー

Automatic Redirect Based on Browser Language

解決

Did you add it with wrapping script tags? As you're adding it to HTML, you'd need them

<script>
 $( document ).ready(function(){
        var userLang = navigator.language || navigator.userLanguage;
        if (userLang == "de") {
           window.location.href = "https://www.example.com/de/"
        }    });
</script>
0 いいね!
Sascha
投稿者

Automatic Redirect Based on Browser Language

解決

Aha, that makes sense, thank you. Would the same also apply to the snippet you shared (which is what I wanted to use)? This one:

 

const lang = navigator.language
if (lang.includes('de')) {
  window.location = 'https://www.example.com/de'
}
0 いいね!
piersg
キーアドバイザー

Automatic Redirect Based on Browser Language

解決

@Sascha Yes, it would

<script>
const lang = navigator.language
if (lang.includes('de')) {
  window.location = 'https://www.example.com/de'
}
</script>
Sascha
投稿者

Automatic Redirect Based on Browser Language

解決

@piersg thank you! I have now tried both scripts (inside script tags) and both were accepted by Hubspot. However, the first one doesn't do anything, and the second one (starting with the const statement) leads to a reload-loop of the page. It does switch to the German version, but than constantly and rapidly reloads the page and never stops doing so. Really odd. 

 

Any ideas? If you don't have time for this, I would totally understand 🙂

 

Thanks!

0 いいね!
piersg
解決策
キーアドバイザー

Automatic Redirect Based on Browser Language

解決

Ahhh haha of course. That's my fault, sorry.

<script>
const lang = navigator.language;
const langRedirected = localStorage.getItem("languageRedirected");
if (lang.includes('de') && (langRedirected === null || langRedirected === false)) {
  localStorage.setItem("languageRedirected", true);
  window.location = 'https://www.example.com/de'
}
</script>
Sascha
投稿者

Automatic Redirect Based on Browser Language

解決

Woohoo... that worked. Thank you so much! I definitely owe you a beer or two 🙂

 

0 いいね!
dennisedson
HubSpot製品開発チーム
HubSpot製品開発チーム

Automatic Redirect Based on Browser Language

解決

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

piersg
キーアドバイザー

Automatic Redirect Based on Browser Language

解決

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

dennisedson
HubSpot製品開発チーム
HubSpot製品開発チーム

Automatic Redirect Based on Browser Language

解決

Hello @BrandonSeerene 

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