Learn how to detect your browser language

Video thumbnail

Detecting the user's browser language is one of those small but essential tasks for offering a more personalized experience.
In my case, on my academy's website, I use this method to automatically display the Spanish or English version according to the browser settings. It's a lightweight solution, without frameworks, and fully compatible with modern browsers.

1. What detecting the browser language is and what it's used for

When a visitor enters your site, their browser sends information about the system's preferred language.
Utilizing that data allows you to adapt the interface, the content, or even redirect to a localized version of your website.

Benefits of personalizing the user experience

Increases retention: the user feels comfortable if they see content in their language.

  • Improves international SEO: facilitates multilingual strategies.
  • Reduces friction: doesn't force the user to manually select their language.
  • When it's convenient to do it and when it's not

It's convenient when you have site versions in several languages or content that is sensitive to cultural context.
It's not necessary if your website is monolingual or if the language doesn't affect the interaction.

2. Main method: using navigator.language

JavaScript offers a direct and modern way to get the browser language with the navigator.language property.

What value does this property return?

It returns a string that identifies the language and region.
Example: es-ES, en-US, fr-FR, etc.

const idiomaNavegador = navigator.language;
console.log(idiomaNavegador); // 'es-ES' o 'en-US'
const idioma = navigator.language || navigator.userLanguage;
alert(`El idioma del navegador es: ${idioma}`);
const idiomaCorto = navigator.language.split('-')[0];
console.log(idiomaCorto); // 'es'

3. Alternatives and cross-browser compatibility

Also, we can use another option instead of:

console.log(navigator.languages); // ['es-ES', 'en-US', 'fr']

Which is the one that supports old browsers like Internet Explorer

navigator.userLanguage (old IE version)

Frequently Asked Questions about Language Detection

  • What's the difference between navigator.language and navigator.languages?
    The first returns the primary language; the second, a list of the user's preferences.
  • How can I redirect users based on the detected language?
    Use a conditional structure with window.location.href or a routing library.
  • Is navigator.language compatible with all modern browsers?
    Yes, except for old versions of IE (use userLanguage as a fallback).
  • What format does navigator.language return?
    It usually comes in ISO format with country: es-ES, en-US, etc.
  • Can the language be detected without JavaScript?
    Yes, using the HTTP Accept-Language header, although it requires server configuration.

Conclusion

Detecting the browser language with JavaScript is a simple, efficient, and modern solution for adapting your site to each user.
In my experience, implementing navigator.language has simplified the management of multilingual versions and improved the visitor experience from the very first second.

I agree to receive announcements of interest about this Blog.

We see how to detect the language using navitagor.language and a possible use for filters.

| 👤 Andrés Cruz

🇪🇸 En español