files/en-us/web/api/languagedetector/availability_static/index.md
{{APIRef("Translator and Language Detector APIs")}}{{SeeCompatTable}}{{securecontext_header}}
The availability() static method of the {{domxref("LanguageDetector")}} interface returns an enumerated value that indicates whether the browser AI model supports a given LanguageDetector configuration.
LanguageDetector.availability(options)
options
LanguageDetector. Possible values include:
expectedInputLanguages
["en"]A {{jsxref("Promise")}} that fulfills with an enumerated value indicating whether support is available (or will be available) for a given LanguageDetector configuration, or null if support could not be determined.
Possible values include:
available
downloadable
downloading
unavailable
InvalidStateError {{domxref("DOMException")}}
OperationError {{domxref("DOMException")}}
UnknownError {{domxref("DOMException")}}
availability() call failed for any other reason, or a reason the user agent did not wish to disclose.If usage of the method is blocked by a {{httpheader('Permissions-Policy/language-detector','language-detector')}} {{httpheader("Permissions-Policy")}}, the promise rejects with a value of unavailable.
availability() usageIn the following snippet, we start by checking the availability of the model for detecting a couple of languages using the availability() method:
unavailable, we print an appropriate error message to the console.available, we create a language detector using the {{domxref("LanguageDetector.create_static", "create()")}} method, passing it the expectedInputLanguages. The required AI model is available, so we can use it immediately.downloadable or downloading), we run the same create() method call, but this time we include a monitor that logs the percentage of the model downloaded each time the {{domxref("CreateMonitor/downloadprogress_event", "downloadprogress")}} event fires.async function getDetector(languages) {
const availability = await LanguageDetector.availability({
expectedInputLanguages: languages,
});
if (availability === "unavailable") {
console.log(`Detection not supported; try a different set of languages.`);
return undefined;
} else if (availability === "available") {
return await LanguageDetector.create({
expectedInputLanguages: languages,
});
}
return await LanguageDetector.create({
expectedInputLanguages: languages,
monitor(monitor) {
monitor.addEventListener("downloadprogress", (e) => {
console.log(`Downloaded ${Math.floor(e.loaded * 100)}%`);
});
},
});
}
const detector = await getDetector(["en-US", "zh"]);
async function langSupport(language) {
const availability = await LanguageDetector.availability({
expectedInputLanguages: [language],
});
return availability;
}
await langSupport("en");
await langSupport("pt");
await langSupport("zh");
{{Specifications}}
{{Compat}}