How to detect the browser language preference using JavaScript ?

Detecting the language preference of users can be very important for Websites or Web Apps for increasing user interaction. By using JavaScript, this task can be easily done by using:

Languages property is available for the navigator interface, which returns the most preferred / user preferred language set in the web browser. This property is read-only.

Syntax:

navigator.languages 

// Or

navigator.language

Return Value: 

  • The navigator.languages property will return an array which stores the languages in an order in which language most preferred by the user will be the first element.
  • The navigator.language property will return the first element of the array which is returned by the navigator.languages property i.e. the most preferred user language.

Note: Language property is a read-only property, thus it is only possible for us to get the value, we cannot make changes to the user preferred language.



Example 1: Getting the most preferred language.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html
  
<body
    <h1 align="left">GeekforGeeks</h1
    <script
        var usrlang = navigator.language 
                || navigator.userLanguage;
        document.write(
            "User's preferred language is: "
            + usrlang);
    </script
</body
</html>

chevron_right


Output:

Example 2: Getting the preferred language array.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html
    
<body
    <h1 align="left">GeekforGeeks</h1
     
    <script
        var usrlang = navigator.languages;
        console.log(usrlang);
    </script
</body
</html>

chevron_right


Output:

Supported Browsers:

  • Google Chrome
  • Mozilla Firefox
  • Edge
  • Opera
  • Internet Explorer

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.