Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

tabs.detectLanguage()

Detects the primary language of the content in a tab, using the Compact Language Detector (CLD).

Syntax

chrome.tabs.detectLanguage(
  tabId,                   // optional integer
  function(language) {...} // function
)

This API is also available as browser.tabs.detectLanguage() in a version that returns a promise.

Parameters

tabIdOptional
integer. Defaults to the active tab of the current window.
callback
function. The function is passed the following arguments:
language
string. An ISO language code such as en or fr. For a complete list of languages supported by this method, see the CLD2 README. For an unknown language, "und" will be returned (but see bug 1288263).

Browser compatibility

Chrome Edge Firefox Firefox for Android Opera
Basic support Yes Yes 45.0 No 33

Examples

Detect and log the language of the active tab when the user clicks a browser action:

function onLanguageDetected(lang) {
  console.log("Language is: " + lang);
}

chrome.browserAction.onClicked.addListener(function() {
  chrome.tabs.detectLanguage(onLanguageDetected);
});

Detect and log the language of every open tab when the user clicks a browser action (note that this example requires the "tabs" permission):

function onLanguageDetected(url, lang) {
  console.log("Language in " + url + " is: " + lang);
}

function detectLanguages(tabs) {
  for (tab of tabs) {
    var callback = onLanguageDetected.bind(null, tab.url);
    chrome.tabs.detectLanguage(tab.id, callback);
  }
}

chrome.browserAction.onClicked.addListener(function() {
  chrome.tabs.query({}, detectLanguages);
});

Acknowledgements

This API is based on Chromium's chrome.tabs API. This documentation is derived from tabs.json in the Chromium code.

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

Document Tags and Contributors

 Contributors to this page: Makyen, wbamberg
 Last updated by: Makyen,