20

I need to detect OS language using javascript so I can view my page depending on the language.

I know that we can detect the browser language but that is not enough for me.

I need Operation System language

Thanks in advance

Saleh
  • 2,657
  • 12
  • 46
  • 73
  • 2
    You can't do it. You can have your user explicitly select a language, or you can obey the language(s) listed in the "Accept-Language" header. – Pointy Oct 08 '10 at 21:45

6 Answers6

23

There is no cross-browser way to do this. Internet Explorer supports the following:

  • navigator.browserLanguage: browser language
  • navigator.systemLanguage: Windows system language
  • navigator.userLanguage: Windows user-specific language

But there is no way to access these settings from any other browsers (that I can tell) so don't use them: stick to the standard navigator.language (corresponding to the browser language) if you want to maintain cross-browser functionality. If you do use them you will tie your web site to a specific family of operating systems (i.e. Windows) and a specific browser (i.e. Internet Explorer). Do you really want to do this?

Why is the browser language insufficient for your application?

Richard Cook
  • 32,523
  • 5
  • 46
  • 71
  • I am looking for navigator.systemLanguage , can I use it with firefox or just IE?? – Saleh Oct 08 '10 at 21:45
  • 1
    No, IE only I'm afraid. However, this stuff is complicated. However, I think you should think long and hard about why the browser's language is not sufficient. – Richard Cook Oct 08 '10 at 21:46
  • @SzamDev: Firefox provides `navigator.language`, which is much the same thing, see Matthew's answer. – T.J. Crowder Oct 08 '10 at 21:47
  • 1
    @T.J. `navigator.language` is equivalent to `navigator.browserLanguage`. – Matthew Flaschen Oct 08 '10 at 21:50
  • 1
    about your question, Why is the browser language insufficient for your application? I have pages for each language, I want after detect the OS language to redirect to this page or display it because sometime the browser language maybe not the same of OS – Saleh Oct 08 '10 at 21:56
  • 1
    @SzamDev: That's your user's problem, not yours. If they're lying to you, well, they should fix that. You're dealing with the browser, so that's the language you should be dealing with. – T.J. Crowder Oct 08 '10 at 22:02
18

This returns the system language:

Intl.DateTimeFormat().resolvedOptions().locale
Clay Sissing
  • 224
  • 2
  • 4
5

You may just guess OS language considering few factors:

Windows OS

Internet Explorer

  • navigator.browserLanguage: IE language (menu, help and etc.), the same as OS display language (if user hasn't change it). As in Control Panel -> Region and Language -> Keyboards and Lanugages -> Display language
  • navigator.systemLanguage: as in Control Panel -> Region and Language -> Location
  • navigator.userLanguage: as in Control Panel -> Region and Language -> Formats

ECMAScript Internationalization API

var d=new Date(Date.UTC(2014,1,26,3,0,0));
var dateFormat={weekday:"long",year:"numeric",month:"long",day:"numeric"};
var result = d.toLocaleDateString("i-default",dateFormat);
alert(result);
//e.g. for Russian format ( Control Panel -> Region and Language -> Formats ) 
//result == 'среда, 26 февраля 2014 г.'

Then search result on your server over preliminary generated set of formatted dates in different languages.

NB! Chrome returns date formatted in its UI language.

Adobe Flash

If you desperately need to know OS language — embed flash in your page and exploit flash.system.Capabilities.language:

NB! Chrome doesn't allow the trick — Chrome's Flash always shows browser.language, I think because it has own Flash.

Firefox and Chrome

navigator.language tells you a browser's UI language (menu, help and etc.) and you may assume that in overwhelming majority of cases it matches OS language (especially for home computers): while downloading FF or Chrome a download page is displayed according user's then browser — on Windows it is IE in the same language as OS.

It is very strange indeed that Chrome is thing in itself when dealing with browser's environment parameters, alas.

Martin Andersson
  • 18,072
  • 9
  • 87
  • 115
Denis Kalinin
  • 337
  • 2
  • 8
2

You can use the user agent. However, it can be spoofed easily, it is not guaranteed to contain language information, and navigator.language and navigator.browserLanguage will probably be more reliable.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • He's talking about the locale, I think. – Pointy Oct 08 '10 at 21:42
  • 4
    `navigator.language` or `navigator.browserLanguage`, it's never been standardized: https://developer.mozilla.org/en/Navigator.language, http://msdn.microsoft.com/en-us/library/ms533542(v=VS.85).aspx IE also provides `systemLanguage` and `userLanguage` – T.J. Crowder Oct 08 '10 at 21:46
2

You can get from the js with default languages, and supported languages through this:

navigator.language

Return 'en-GB'

navigator.languages

Returns ['en-GB', 'en-US', 'en', 'zh-CN']

PS: I tested on Chrome on Windows (Not sure if Mac also works the same)

Xin
  • 33,823
  • 14
  • 84
  • 85
  • 1
    Yes, it should work fine on most platforms. https://developer.mozilla.org/en-US/docs/Web/API/Navigator/language – Hg0428 Jan 21 '23 at 23:09
0

One thing for everyone here to consider, not all users know how to set their browser language preferences. So if you're making a site you WANT people to see, it's your problem if they land on it in a different language than one they're comfortable with, as it increases the change they will leave. A lot of it depends on the demographic you're targeting, but for a web page I think it's good to detect the system language IF you can at least, then then fall back on the browser language if you can't. It's more likely to be accurate.

Denny
  • 186
  • 1
  • 10
  • (until looking for this setting, as an english speaker never having the need, i had no idea how to change it. which could really be a pain for me on vacation at an itallian coffee shop, even if i had used the language bar to change the system language.) – Denny Dec 10 '12 at 15:31