Is it possible to determine the user locale's **country** as set in the OS from the browser in JavaScript?
Asked Answered
L

3

5

Do web browsers have an interface to access any of the locale settings as set in the operating system by the user?

I can detect what country the user is currently in from their IP address or using the new geolocation API, but I'm writing a browser app for travellers so I expect their current location to often not be their home country.

Is there any way to determine what country/locale the browser/OS is set to?

If not is there any proposal to add proper locale support to JavaScript?

Also are there any clever workarounds? (For instance I know I can get the user's preferred browsing language but there are plenty of countries which use the same language and plenty of users who keep this set to the default rather than changing it to their usual language.)

Luteolin answered 6/8, 2012 at 10:3 Comment(4)
Apart from navigator.language and geolocation I don't think so.Etamine
Maybe if user didn't change his preferred language, then he actually really would prefer this default (for example for more concise tech terms) instead of his native?Nephelometer
@OlegV.Volkov: Yes there are pretty valid reasons for changing your "accept language" to something other than your native language, but I'm trying to think of any heuristic to get the user's home country.Luteolin
I've posted a followup question: Is there any current proposal I can follow about adding proper locale support to JavaScript?Luteolin
G
2

The best you'll get is languages:

  • navigator.language (Netscape - Browser Localization)
  • navigator.browserLanguage (IE-Specific - Browser Localized Language)
  • navigator.systemLanguage (IE-Specific - Windows OS - Localized Language)
  • navigator.userLanguage

Due to either security concerns or just not implemented, what you have requested is not possible, natively. You can however use an ActiveX object for your Windows users

You could have a user select their perfered language, country, etc then use javascript to set a cookie, which will be sent to your server anytime a page is requested

Grimbald answered 6/8, 2012 at 10:14 Comment(5)
This only gets me the language - as I specifically state I want the locale or the country since one language can be used in many countries.Luteolin
I gave you the best you can get. Due to security concerns(for what ever reasons) browser based js does not have access to locale settings.Grimbald
I've undone my downvote due to your improvements to the question - thanks. But can you provide some link or reference to support the claim that JS lacks locale support due to security concerns?Luteolin
It would appear that it is not a security concern so much as not implemented. You can access the information you want if the users are using Windows(through activeX, and if you would like an example I can code one up) but browsers do not natively expose such members to the engineGrimbald
Well as much as I hate it at least activeX is a possible workaround. This makes me think other possible workarounds might be possible via Java applets or Flash objects, all of which I have no experience in but some vague knowledge of.Luteolin
F
7

I'd have to defend or re-state the answers of SReject and CodeJack because the browser language does in general contain the country locale of the OS as in "en-US" or "de-CH".

We've used this for years to pre-select the country in forms and it works in enough cases to be useful.

So with travelling Swiss users you would use navigator.language.slice(-2) and get "CH" which is a valid answer to your question.

Fendig answered 10/6, 2013 at 8:27 Comment(1)
-2 is not smart. There are valid ISO codes like ES-419 and 419 is latin america :-/Disgrace
G
2

The best you'll get is languages:

  • navigator.language (Netscape - Browser Localization)
  • navigator.browserLanguage (IE-Specific - Browser Localized Language)
  • navigator.systemLanguage (IE-Specific - Windows OS - Localized Language)
  • navigator.userLanguage

Due to either security concerns or just not implemented, what you have requested is not possible, natively. You can however use an ActiveX object for your Windows users

You could have a user select their perfered language, country, etc then use javascript to set a cookie, which will be sent to your server anytime a page is requested

Grimbald answered 6/8, 2012 at 10:14 Comment(5)
This only gets me the language - as I specifically state I want the locale or the country since one language can be used in many countries.Luteolin
I gave you the best you can get. Due to security concerns(for what ever reasons) browser based js does not have access to locale settings.Grimbald
I've undone my downvote due to your improvements to the question - thanks. But can you provide some link or reference to support the claim that JS lacks locale support due to security concerns?Luteolin
It would appear that it is not a security concern so much as not implemented. You can access the information you want if the users are using Windows(through activeX, and if you would like an example I can code one up) but browsers do not natively expose such members to the engineGrimbald
Well as much as I hate it at least activeX is a possible workaround. This makes me think other possible workarounds might be possible via Java applets or Flash objects, all of which I have no experience in but some vague knowledge of.Luteolin
C
1
var userLocale = navigator.language or

navigator.userLanguage;

EDIT only other way is geoLocation API of HTML5

http://html5demos.com/geo http://dev.w3.org/geo/api/spec-source.html

Caliph answered 6/8, 2012 at 10:15 Comment(2)
This only gets me the language - as I specifically state I want the locale or the country since one language can be used in many countries.Luteolin
As I also mentioned in my question I'm trying to distinguish between the user's current location and the user's home location as set in their operating system. I'm writing an app for travellers who will often be far from their home country and one aim is to auto-detect as many defaults as possible. (They will be override-able too.)Luteolin

© 2022 - 2024 — McMap. All rights reserved.