Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get visitors language & country code with javascript (client-side) [duplicate]

Question: Is there a javascript (client-side) code to get visitors country/language code, that is accurate and is cross-"modern"-browser ? I am looking for results like 'en-US', 'sv-SE', 'nl-NL', etc.

Related questions to this have been asked before (some SO links: 1,2,3,4, among others) but I didn't find answer and some of the answers are some yearls old and in some cases referring to even more old articles, which makes me think there are new solutions for this.

I tried :

var language = window.navigator.userLanguage || window.navigator.language; console.log(language); 

and got "sv" in Chrome and "en-GB" in Firefox, in the same machine, same place.

like image 227
Sergio Avatar asked Jul 16 '13 15:07

Sergio


People also ask

How do I get user browser language?

To get the user's locale in the browser, access the first element of the languages property on the navigator object, e.g. navigator. languages[0] . The property returns an array of strings that represent the user's preferred languages.

What is navigator language?

The Navigator language property is used for returning the browser's language version. It is a read-only property and it returns a string representing the language version of the browser. Some of the possible language codes are : en-US.

How is locale determined?

On Mac and Linux, the locale is decided by the system regardless of the user language preferences. On Windows is can be elected among the languages in the preferred list on Chrome.


1 Answers

Using jQuery, this line will display your user's country code.

  $.getJSON('https://freegeoip.net/json/', function(result) {     alert(result.country_code);   }); 
like image 150
Sean McClory Avatar answered Oct 05 '22 23:10

Sean McClory