NavigatorLanguage.language 只读属性返回一个表示用户偏好语言的字符串,通常指浏览器 UI 的语言。

语法

let lang = navigator.language;

一个 DOMStringlang 存储一个表示语言版本(在 BCP 47 中定义)的字符串。合法的语言版本有 "zh-CN"、"en"、"en-US"、"fr"、"es-ES" 等。

注意 macOS 和 iOS 平台上的 Safari(10.2 之前版本),国家代码为小写:"zh-cn"、"en-us"、"fr-fr" 等。

示例

if ( window.navigator.language != 'zh-CN' ) {       
  doLangSelect(window.navigator.language); 
}

标准

标准 状态 备注
HTML Living Standard
NavigatorLanguage: language
Living Standard 初次定义

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support Yes
Notes
Full support Yes
Notes
Notes Returns the browser UI language, not the value of the Accept-Language HTTP header.
Edge Full support YesFirefox Full support 1
Notes
Full support 1
Notes
Notes Prior to Firefox 4 this property's value was also part of the user agent string, as reported by navigator.userAgent.
Full support 5
Notes
Notes Starting in Firefox 5.0 this property's value is based on the value of the Accept-Language HTTP header.
IE Full support 11
Notes
Full support 11
Notes
Notes Closest available (non-standard) properties are userLanguage and browserLanguage.
Opera Full support YesSafari Full support YesWebView Android Full support Yes
Notes
Full support Yes
Notes
Notes Returns the browser UI language, not the value of the Accept-Language HTTP header.
Chrome Android Full support Yes
Notes
Full support Yes
Notes
Notes Returns the browser UI language, not the value of the Accept-Language HTTP header.
Edge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.

另请参阅

文档标签和贡献者

此页面的贡献者: Roy-Tian, Freed, teoli, khalid32, ziyunfei, gqqnbig
最后编辑者: Roy-Tian,