0

I'm using navigator.languages to detect the browser's language & country code in the form of en-US.

If I set my browser language to English then I get en-US but if I set it to Japanese it only returns ja, without the country code JP. Obviously Japanese is only spoken in Japan in the whole world so they must have omitted it. The same goes for Korean as I get ko instead of ko_KR.

But I need both information because the Facebook SDK script I'm loading requires it in the format of ll_cc as below:

https://connect.facebook.net/en_US/sdk.js
https://connect.facebook.net/ja_JP/sdk.js

According to the Facebook Documentation, Putting ja_JP in the script address is the only way to achieve the change in display language.

I would like to change the display language based on users' browser language settings. (e.g., Japanese, Korean etc)

So how should I go about getting the country code JP from browser?

I was thinking about creating a table to match the language to country code in the case of one-to-one, but I was curious to see if there is better way to approach this.

Thanks for your help in advance.

bubbleChaser
  • 755
  • 1
  • 8
  • 21
  • how is this `broswer.languages` defined? I certainly don't have such a property in my broswer – Jaromanda X Jun 30 '21 at 02:57
  • oh, I didn't notice the typo (I just copy pasted without thinking) ... but I don't have browser.languages in my browser either - is this some sort of browser web extension you're talking about? – Jaromanda X Jun 30 '21 at 03:00
  • My bad. It is `navigator.languages`. I updated the title as well. Thanks – bubbleChaser Jun 30 '21 at 03:05
  • You can't without user permission. You can try to derive it from IP with geocoding but it's not super reliable. If the user will give you permission you can use html5 geolocation API https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API – Matt Jun 30 '21 at 03:09
  • @Matt For the most part that may work. But in some cases the geolocation is not necessarily the same as the users' browser language country code. – bubbleChaser Jun 30 '21 at 03:24
  • Agreed. Consequently, the language country code is not necessarily the country they are in. – Matt Jun 30 '21 at 03:28

1 Answers1

0

What navigator.language, as specified in BCP 47, gets you is the International Standards Organization's ISO 639-1 language code, which is primarily a 2-letter code. (like en for English and jp for Japanese)

There is a exception for languages with dialects, versions or variations of a language which require another specified language code. (Like en-us for the United States's version of English, which differs from for example the United Kingdom's version of English (en-gb).

What you had was a confusing overlap between the combination of a language code and a combination of a language and country code.

In order for you to meet the language-country code format, you will need to reference ISO-639-1 and ISO-3166 or a website the conveniently combines the two, here for example.

Here is some code I wrote that should work. fingers-crossed This is my first time writing a answer so please let me know if I did anything wrong.

/// Note the l2.io and jQuery script tag


/// From Xposedbones (https://stackoverflow.com/questions/35999265/get-country-code-using-javascript)
var country_code = null;
$.getJSON('http://ipinfo.io/' + userip, function(data){
  country_code = data.country;
  console.log(country_code)
    
  /// Modified From MDN Web Docs (https://developer.mozilla.org/en-US/docs/Web/API/NavigatorLanguage/language)
  var language_code = navigator.language.split("-")[0] /// There is probably a better way for this

  /// Combine country and language
  const combined = `${language_code}-${country_code}`
  alert(combined)
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="https://l2.io/ip.js?var=userip"></script>
Kodzvn
  • 45
  • 1
  • 6
  • Using the IP address to deduct the country code may work for the most part. However, it does not account for the browser's language settings, which is the key information that I'm interested in. For instance, my browser thinks my one & only language is `en_US` and I want it to stay that way, even though I'm not geographically located in the United States. – bubbleChaser Jun 30 '21 at 09:02