i18n.detectLanguage()

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

This is an asynchronous function that returns a Promise.

Syntax

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

Parameters

text
string. User input string to be translated.

Return value

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

isReliable
boolean. Whether the language was detected reliably.
languages
array of objects, each of which has two properties:
language
i18n.LanguageCode. The detected language.
percentage
integer. The percentage of the input string that was in the detected language.

Browser compatibility

ChromeEdgeFirefoxFirefox for AndroidOpera
Basic support47No474834

Examples

function onLanguageDetected(langInfo) {
  for (lang of  langInfo.languages) {
    console.log("Language is: " + lang.language);
    console.log("Percentage is: " + lang.percentage);
  }
}
var text = "L'homme est né libre, et partout il est dans les fers."
var detecting = browser.i18n.detectLanguage(text);
detecting.then(onLanguageDetected);

Acknowledgements

This API is based on Chromium's chrome.i18n API. This documentation is derived from i18n.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: michael-zapata, wbamberg, Makyen
 Last updated by: michael-zapata,