Navigator.language

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

io error: No such file or directory (os error 2) (/home/runner/work/yari/yari/mdn/translated-content/files/zh-tw/web/api/html_dom_api/index.md)

Navigator.language 是一個唯讀的屬性,回傳使用者偏好的語言字串,通常是瀏覽器 UI 的文字

語法

js
const lang = navigator.language

一個 DOMString. lang 儲存一個代表此語言的字串。定義在BCP 47。範例: 合法的語言代碼 "en", "en-US", "fr", "fr-FR", "es-ES", etc.

在 iOS 小於 10.2 的 Safari 國碼是回傳小寫的喲! "en-us", "fr-fr" etc.

範例

js
if (/^en\b/.test(navigator.language)) {
  doLangSelect(window.navigator.language);
}

規格

Specification
HTML Standard
# dom-navigator-language-dev

瀏覽器相容性

BCD tables only load in the browser

參見