# Automatic Redirect Based on Browser Language

**URL:** <https://community.hubspot.com/t/automatic-redirect-based-on-browser-language/35050>\
**Category:** CMS Development\
**Tags:** local-development\
**Created:** [April 7, 2021, 7:40am UTC](https://community.hubspot.com/t/automatic-redirect-based-on-browser-language/35050 "2021-04-07T07:40:37Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![BrandonSeerene](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/brandonseerene/32/57633_2.png) [@BrandonSeerene](https://community.hubspot.com/u/BrandonSeerene)\
**Post date:** [April 7, 2021, 7:40am UTC](https://community.hubspot.com/t/automatic-redirect-based-on-browser-language/35050/1 "2021-04-07T07:40:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [April 7, 2021, 3:31pm UTC](https://community.hubspot.com/t/automatic-redirect-based-on-browser-language/35050/2 "2021-04-07T15:31:37Z")

</div>

Hello @BrandonSeerene

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

---

<div class="post-metadata">

**Author:** ![piersg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/piersg/32/99782_2.png) [@piersg](https://community.hubspot.com/u/piersg)\
**Post date:** [April 7, 2021, 4:58pm UTC](https://community.hubspot.com/t/automatic-redirect-based-on-browser-language/35050/3 "2021-04-07T16:58:17Z")

</div>

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

```lia-code-sample
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.

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [April 7, 2021, 8:24pm UTC](https://community.hubspot.com/t/automatic-redirect-based-on-browser-language/35050/4 "2021-04-07T20:24:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![piersg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/piersg/32/99782_2.png) [@piersg](https://community.hubspot.com/u/piersg)\
**Post date:** [April 8, 2021, 8:16am UTC](https://community.hubspot.com/t/automatic-redirect-based-on-browser-language/35050/5 "2021-04-08T08:16:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Girdhari](https://avatars.discourse-cdn.com/v4/letter/g/c4cdca/32.png) [@Girdhari](https://community.hubspot.com/u/Girdhari)\
**Post date:** [June 7, 2022, 9:49am UTC](https://community.hubspot.com/t/automatic-redirect-based-on-browser-language/35050/6 "2022-06-07T09:49:29Z")

</div>

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/](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](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/](https://www.example.com/de/)”  
} });

Thanks in Advanced.

---

<div class="post-metadata">

**Author:** ![piersg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/piersg/32/99782_2.png) [@piersg](https://community.hubspot.com/u/piersg)\
**Post date:** [November 10, 2022, 10:30am UTC](https://community.hubspot.com/t/automatic-redirect-based-on-browser-language/35050/7 "2022-11-10T10:30:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Sascha](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Sascha](https://community.hubspot.com/u/Sascha)\
**Post date:** [November 10, 2022, 11:37am UTC](https://community.hubspot.com/t/automatic-redirect-based-on-browser-language/35050/8 "2022-11-10T11:37:10Z")

</div>

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](https://d2qpr2jzcazej7.cloudfront.net/original/3X/a/9/a9cb48ee266e119dcc6ae4d291ba7bd17e27072c.jpeg)

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!

---

<div class="post-metadata">

**Author:** ![piersg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/piersg/32/99782_2.png) [@piersg](https://community.hubspot.com/u/piersg)\
**Post date:** [November 10, 2022, 3:33pm UTC](https://community.hubspot.com/t/automatic-redirect-based-on-browser-language/35050/9 "2022-11-10T15:33:53Z")

</div>

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

```lia-code-sample
<script>
 $( document ).ready(function(){
 var userLang = navigator.language || navigator.userLanguage;
 if (userLang == "de") {
 window.location.href = "https://www.example.com/de/"
 } });
</script>

```

---

<div class="post-metadata">

**Author:** ![Sascha](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Sascha](https://community.hubspot.com/u/Sascha)\
**Post date:** [November 10, 2022, 4:24pm UTC](https://community.hubspot.com/t/automatic-redirect-based-on-browser-language/35050/10 "2022-11-10T16:24:30Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![piersg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/piersg/32/99782_2.png) [@piersg](https://community.hubspot.com/u/piersg)\
**Post date:** [November 11, 2022, 10:15am UTC](https://community.hubspot.com/t/automatic-redirect-based-on-browser-language/35050/11 "2022-11-11T10:15:06Z")

</div>

@Sascha Yes, it would

```lia-code-sample
<script>
const lang = navigator.language
if (lang.includes('de')) {
 window.location = 'https://www.example.com/de'
}
</script>

```

---

<div class="post-metadata">

**Author:** ![Sascha](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Sascha](https://community.hubspot.com/u/Sascha)\
**Post date:** [November 11, 2022, 1:42pm UTC](https://community.hubspot.com/t/automatic-redirect-based-on-browser-language/35050/12 "2022-11-11T13:42:01Z")

</div>

@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!

---

<div class="post-metadata">

**Author:** ![piersg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/piersg/32/99782_2.png) [@piersg](https://community.hubspot.com/u/piersg)\
**Post date:** [November 14, 2022, 2:50pm UTC](https://community.hubspot.com/t/automatic-redirect-based-on-browser-language/35050/13 "2022-11-14T14:50:48Z")

</div>

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

```lia-code-sample
<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>

```

---

<div class="post-metadata">

**Author:** ![Sascha](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Sascha](https://community.hubspot.com/u/Sascha)\
**Post date:** [November 14, 2022, 4:57pm UTC](https://community.hubspot.com/t/automatic-redirect-based-on-browser-language/35050/14 "2022-11-14T16:57:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sascha](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Sascha](https://community.hubspot.com/u/Sascha)\
**Post date:** [November 9, 2022, 3:14pm UTC](https://community.hubspot.com/t/automatic-redirect-based-on-browser-language/35050/15 "2022-11-09T15:14:49Z")

</div>

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