大网数据广告活动
Web 标准与前端开发

LanguageDetector: availability() static method

· 52CE 我爱测

The **`availability()`** static method of the LanguageDetector interface returns an enumerated value that indicates whether the browser AI model supports a given `LanguageDetector` configuration.

Translator and Language Detector APIs

The availability() static method of the LanguageDetector interface returns an enumerated value that indicates whether the browser AI model supports a given LanguageDetector configuration.

Syntax

LanguageDetector.availability(options)

Parameters

  • options
    • : An object specifying configuration options for the LanguageDetector. Possible values include:
      • expectedInputLanguages
        • : An array of strings specifying the expected languages of the input text to have its language detected. These should be valid BCP 47 language tag. Defaults to ["en"]

Return value

A 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
    • : The browser supports the given configuration and it can be used immediately.
  • downloadable
    • : The browser supports the given configuration, but it first needs to download an AI model, or some fine-tuning data for the model.
  • downloading
    • : The browser supports the given configuration, but it has to finish an ongoing download before it can proceed.
  • unavailable
    • : The browser does not support the given configuration, or the Language Detector API is blocked by a Permissions-Policy/language-detector Permissions-Policy.

Exceptions

  • InvalidStateError DOMException
    • : Thrown if the page's Document is not yet active.
  • OperationError DOMException
    • : Thrown if initialization of the AI model failed for any reason.
  • UnknownError DOMException
    • : Thrown if the availability() call failed for any other reason, or a reason the user agent did not wish to disclose.

Examples

Basic availability() usage

In the following snippet, we start by checking the availability of the model for detecting a couple of languages using the availability() method:

  • If it returns unavailable, we print an appropriate error message to the console.
  • If it returns available, we create a language detector using the LanguageDetector.create_static method, passing it the expectedInputLanguages. The required AI model is available, so we can use it immediately.
  • If it returns a different value (that is, 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 CreateMonitor/downloadprogress_event 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"]);

Detecting language support

async function langSupport(language) {
  const availability = await LanguageDetector.availability({
    expectedInputLanguages: [language],
  });
  return availability;
}

await langSupport("en");
await langSupport("pt");
await langSupport("zh");

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。