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

LanguageDetector: create() static method

· 52CE 我爱测

The **`create()`** static method of the LanguageDetector interface creates a new `LanguageDetector` instance to detect languages.

Translator and Language Detector APIs

The create() static method of the LanguageDetector interface creates a new LanguageDetector instance to detect languages.

[!NOTE] The create() method requires transient activation, that is, it must be invoked in response to a user action such as a mouse click or button press.

Syntax

LanguageDetector.create(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, which helps improve the accuracy of the language detection. These should be valid BCP 47 language tag. Defaults to ["en"].
      • monitor
        • : A callback function with a CreateMonitor argument that enables monitoring download progress of the AI model.
      • signal
        • : An AbortSignal object instance, which allows a create() operation to be aborted via the associated AbortController. The exact effect is dependent on when AbortController.abort() is called:
          • If abort() is called before the create() promise resolves, the create() operation is canceled.
          • If abort() is called after the create() promise fulfills, it has the same effect as calling LanguageDetector.destroy(): The resources assigned to the resulting LanguageDetector instance are released, and any ongoing and subsequent LanguageDetector method calls will reject with an AbortError.

Return value

A Promise that fulfills with a LanguageDetector object instance.

Exceptions

  • InvalidStateError DOMException
    • : Thrown if the page's Document is not yet active.
  • NetworkError DOMException
    • : Thrown if:
      • The network was not available to download the AI model.
      • The user has canceled the AI model download.
  • NotAllowedError DOMException
    • : Thrown if:
      • Usage of the method is blocked by a Permissions-Policy/language-detector Permissions-Policy.
      • The user has blocked the AI model download in some way.
      • The create() method wasn't called via transient activation.
  • NotSupportedError DOMException
    • : Thrown if:
      • The language tags specified in expectedInputLanguages are invalid, or not supported.
      • An AI model to support the specified expectedInputLanguages is not available.
  • OperationError DOMException
    • : General-purpose exception thrown if LanguageDetector creation failed for any other reason.

Examples

Basic LanguageDetector creation

const detector = await LanguageDetector.create({
  expectedInputLanguages: ["en-US", "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

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

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