LanguageDetector: create() static method

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

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

Syntax

js
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 to have its language detected. These should be valid BCP 47 language tags (as specified in RFC 5646). Defaults to ["en"].

monitor Optional

A callback function with a CreateMonitor argument that enables monitoring download progress of the AI model.

signal Optional

An AbortSignal object instance, which allows the create() operation to be aborted via the associated AbortController.

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 in any of the following circumstances:

  • The network was not available to download the AI model.
  • The user has cancelled the AI model download.
NotAllowedError DOMException

Thrown in any of the following circumstances:

NotSupportedError DOMException

Thrown in any of the following circumstances:

  • 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

js
const detector = await LanguageDetector.create({
  expectedInputLanguages: ["en-US", "zh"],
});

Specifications

No specification found

No specification data found for api.LanguageDetector.create_static.
Check for problems with this page or contribute a missing spec_url to mdn/browser-compat-data. Also make sure the specification is included in w3c/browser-specs.

Browser compatibility

See also