Is there anyway to detect OS language using javascript?

前端 未结 4 1335
深忆病人
深忆病人 2020-12-06 11:05

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

4条回答
  •  爱一瞬间的悲伤
    2020-12-06 11:25

    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.

提交回复
热议问题