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.
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.
let detectingLanguages = browser.i18n.detectLanguage(
text // string
)
text
string
. User input string to be translated.
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.
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
detectLanguage |
47 | 79 | 47 | ? | 34 | No | ? | ? | 106
48–106The method existed but always threw an error. |
? | No | ? |
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);
Note: This API is based on Chromium's chrome.i18n
API. This documentation is derived from i18n.json
in the Chromium code.
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/i18n/detectLanguage