Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to detect a browser user's locale in 2012?

Tags:

I want to detect a user's locale (not their location). The problem is that none of the methods seem reliable.

Accept-Language Header

Pros

  1. You're meant to get a list of values which can reveal true locale if the browser has been altered to default to something else.
  2. Seems accurate in IE by default?
  3. Accurate in IE by default means accurate in FF on Windows by default? (i.e. you download the correct version from mozilla.org)
  4. Same as #3 for Opera on Windows?

Cons

  1. Server side overhead and interference with caching (or an extra XHR).
  2. Defaults to "en-us" in Chrome on all systems for English unless you install your local dictionary and drag it to the top.
  3. Not sent by Safari?
  4. Incorrect by default in Safari and Chrome mean that http://mozilla.org and http://opera.com direct to the user to the wrong downloads and will also install "en-us" versions of themselves.

Conclusion

  • Completely unreliable on OSX since every browser will be wrong by default. However should be accurate in IE and FF on Windows.

window.navigator.language/userLanguage

Pros

  1. The best part of this is that it's nice lightweight client side solution.
  2. Seems accurate in IE by default?
  3. Seems accurate in FF on Windows by default?

Cons

  1. Always "en-us" by default in Safari on all systems.
  2. Defaults to "en-us" in Chrome on all systems for English unless you install your local dictionary and drag it to the top.
  3. Always just "en" in Opera.

Conclusion

  • Even more unreliable than Accept-Language.

IP Geolocation

Pros

  1. Works independently of browser and OS language settings.

Cons

  1. Works independently of browser and OS language settings.
  2. All the usual about performance, pricing, keeping it up to date, etc.

Conclusion

  • Whether this works really depends on why you're trying to detect a user's locale. This method gives you a user's locality which isn't the same as locale in an i18n sense.

HTML5 Geolocation

Pros

  1. Generally as accurate as IP geolocation without the server overhead.

Cons

  1. Requires the user to allow you access to their location.
  2. Browser support (actually support looks pretty good).

Conclusion

  • Having to ask for permission is generally a deal breaker. Also same conclusion as IP geolocation.

Summary

It now seems like detecting a user's locale is harder than ever while detecting their physical location has become easier. The big offender is Safari which should at least reflect the OS settings by default on OSX but Chrome isn't setting any examples either. Even if people install their local dictionary to Chrome I really doubt they will also drag it to the top of the list.

I can't blame Mozilla and Opera for getting dragged down by other browsers misreporting to their download pages. However it would mitigate the problem if they would switch their download pages to use geolocation instead of presumably looking at Accept-Language.

But really, what options are left to detect a user's locale now that IE is no longer dominant? Is there anything left?

like image 519
Kyle MacFarlane Avatar asked Sep 19 '12 00:09

Kyle MacFarlane


People also ask

How do I find the locale of my browser?

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.

How is locale determined?

A locale is a location-based language setting that determines which conversational settings and strings to display. The user specifies their locale using settings on their device. They may change this setting whenever they wish, including during a conversation with a Business Messages agent.

What is user's locale?

A user locale specifies the default settings that a user wants to use for formatting dates, times, currency, and numbers. IBM Cognos software uses this information to present data to the user. IBM Cognos software obtains a value for user locale by checking these sources, in the order listed: user preference settings.

What is my current locale?

Press the Win + R hotkeys together on the keyboard and type the following command in your Run box: msinfo32 . Click the System Summary section on the left. On the right, see the Locale value.


1 Answers

This depends on what you mean by “user’s locale”. The possibilities listed in the question reflect different meanings for “user’s locale”. None of them reflects the adequate meaning in modern localization, namely the set of cultural conventions preferred by the user (including a list of human languages in order of preference). The way to find that is to ask the user about them.

Naturally, you should only ask about conventions that really matter in the context, and you may consider storing the preferences, once expressed by the user, in a cookie, in HTML5 storage, in a user database, or somewhere else.

Browsers generally allow the user to specify a list of preferred languages, to be sent in Accept-Language headers. This however relates to a single aspect of locale concept only, it is unknown to most users, and it is known to have incorrect defaults very often.

like image 147
Jukka K. Korpela Avatar answered Sep 19 '22 20:09

Jukka K. Korpela