i18n.detectLanguage()

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

js
let 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.

Examples

js
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);

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Firefox for Android
Safari on iOS
detectLanguage
Chrome – Full support
Chrome 47 (Release date: 2015-12-01)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 47 (Release date: 2016-06-07)
footnote Full support
Opera – Full support
Opera 34 (Release date: 2015-12-08)
footnote Full support
Safari – No support
Safari
footnote No support
Firefox for Android – No support
Firefox for Android 48 – 105 (Release date: 2016-08-02)
footnote The method existed but always threw an error.
Firefox for Android – Full support
Firefox for Android 106 (Release date: 2022-10-18)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Has more compatibility info.

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