docs.rodeo

MDN Web Docs mirror

i18n.detectLanguage()

{{AddonSidebar}} 

Detects the language of the provided text using the Compact Language Detector (CLD).

This is an asynchronous function that returns a Promise.

See the Internationalization page for a guide on using this function.

Syntax

let detectingLanguages = browser.i18n.detectLanguage(
  text                  // string
)

Parameters

Return value

A Promise that will be fulfilled with a result object. The result object has two properties:

Browser compatibility

{{Compat}} 

Examples

function onLanguageDetected(langInfo) {
  for (const lang of langInfo.languages) {
    console.log(`Language is: ${lang.language}`);
    console.log(`Percentage is: ${lang.percentage}`);
  }
}

let text = "L'homme est né libre, et partout il est dans les fers.";

let detecting = browser.i18n.detectLanguage(text);
detecting.then(onLanguageDetected);

{{WebExtExamples}} 

[!NOTE] This API is based on Chromium’s chrome.i18n API. This documentation is derived from i18n.json in the Chromium code.

In this article

View on MDN