# Automatic language detection

**URL:** <https://community.hubspot.com/t/automatic-language-detection/98398>\
**Category:** CMS Development\
**Tags:** html\
**Created:** [January 23, 2024, 7:52am UTC](https://community.hubspot.com/t/automatic-language-detection/98398 "2024-01-23T07:52:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MartinaWi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/martinawi/32/103271_2.png) [@MartinaWi](https://community.hubspot.com/u/MartinaWi)\
**Post date:** [January 23, 2024, 7:52am UTC](https://community.hubspot.com/t/automatic-language-detection/98398/1 "2024-01-23T07:52:08Z")

</div>

Hi,

We set up the automatic language detection for our new website in the site header. If a customer is visiting [fairtiq.com/](http://fairtiq.com/) they are supposed to be redirected to either the German, French or English version, based on their browser language. ([FAIRTIQ - The easiest public transport ticket.](http://fairtiq.com/en), [FAIRTIQ - Die einfachste Fahrkarte.](http://fairtiq.com/de), [FAIRTIQ - Le billet le plus simple.](http://fairtiq.com/fr))

The language detection seems to work, but as you can see in the log below it fetches the 404 page in the background, which might have negative impact on SEO or other.

 ![](https://d2qpr2jzcazej7.cloudfront.net/original/3X/2/9/2985b0deb0ec9588a6927330e562beccc19a20b3.jpeg)

Is there a way to avoid this? Did we miss anything?

HTML code:

```lia-code-sample
<script>
function redirectToPreferredLanguage() {
 if (window.location.pathname !== '/') {
 return;
 }
 const lang = navigator.language;

 const languageConfig = {
 de: '/de',
 fr: '/fr',
 en: '/en'
 };

 const defaultLanguage = 'de';
 const userLanguage = Object.keys(languageConfig).find(language => lang.includes(language));
 const redirectLanguage = userLanguage 
 ? languageConfig[userLanguage] || languageConfig[defaultLanguage] 
 : languageConfig[defaultLanguage]

 window.location = languageConfig[userLanguage] || languageConfig[defaultLanguage];
}
redirectToPreferredLanguage();
</script>

```

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [January 25, 2024, 12:39am UTC](https://community.hubspot.com/t/automatic-language-detection/98398/2 "2024-01-25T00:39:54Z")

</div>

Hi @MartinaWi. Thanks for your question and for including your details. I have a question — have you tried adding a few console logs to your code to debug the path resolution?

- The detected browser language (lang)
- The user’s language as determined by the script (userLanguage)
- The final URL to which the user will be redirected (redirectLanguage)
- Example`console.log('Browser Language:', lang);​`

I’d also like to invite some of our SEO experts to the converstaion — hey [@Phil\_Vallender](https://community.hubspot.com/t5/user/viewprofilepage/user-id/74) @louischausse @MatthiasWeber, do you have any additional suggestions for @MartinaWi?

Thank you! — Jaycee
