Path: blob/main/files/en-us/mozilla/add-ons/webextensions/api/tabs/detectlanguage/index.md
6552 views
------{{AddonSidebar()}}
Detects the primary language of the content in a tab, using the Compact Language Detector (CLD).
This is an asynchronous function that returns a Promise.
Syntax
Parameters
tabId{{optional_inline}}:
integer. Defaults to the active tab of the current window.
callback{{optional_inline}}:
function. Currently, if atabIdis specified, this method uses this callback to return the results instead of returning a promise. The callback receives as its only input parameter a string containing the detected language code such asenorfr.
Return value
A Promise that will be fulfilled with a string representing an ISO language code such as en or fr. For a complete list of languages supported by this method, see kLanguageInfoTable. For an unknown language, "und" will be returned (but see bug 1288263). If any error occurs the promise will be rejected with an error message.
Examples
Detect and log the language of the active tab when the user clicks a browser action:
Detect and log the language of every open tab when the user clicks a browser action (note that this example requires the "tabs" permission):
{{WebExtExamples}}
Browser compatibility
{{Compat}}
Note: This API is based on Chromium's
chrome.tabsAPI. This documentation is derived fromtabs.jsonin the Chromium code.