Translator and Language Detector APIs - Web APIs | MDN (original) (raw)

Concepts and usage

Translating a body of text is a common task on today's web. Typical use cases include:

Detecting the language of a body of test is an important precursor for successful automated translation, but has other uses beyond direct translation. For example, it allows automatic UI configuration based on user text entry, ranging from updating UI and error strings, to automatically loading appropriate dictionaries for spell checking or curse word detection.

AI is well-suited to facilitating language detection and translation. The Translator and Language Detector APIs provide asynchronous (Promise-based) mechanisms for a website to detect languages and translate text via the browser's own internal AI model. This is useful and efficient because the browser handles the service, rather than the developer having to rely on the user downloading AI models, or host or pay for a cloud-based translation service.

You can cancel a pending create(), detect(), or translate() operation using an AbortController.

After a LanguageDetector or Translator instance has been created, you can release its assigned resources and stop any further activity by calling its LanguageDetector.destroy()/Translator.destroy() method. You are encouraged to do this after you've finished with the object as it can consume a lot of resources.

See Using the Translator and Language Detector APIs for a walkthrough of how to use the APIs.

Interfaces

LanguageDetector Experimental

Contains all the language detection functionality, including checking AI model availability, creating a new LanguageDetector instance, using it to detect a language, and more.

Translator Experimental

Contains all the translation functionality, including checking AI model availability, creating a new Translator instance, using it to create a translation, and more.

Permissions-Policy; the language-detector directive

Controls access to the language detection functionality. Where a policy specifically disallows its use, any attempts to call the LanguageDetector methods will fail with a NotAllowedError DOMException.

Permissions-Policy; the translator directive

Controls access to the translation functionality. Where a policy specifically disallows its use, any attempts to call the Translator methods will fail with a NotAllowedError DOMException.

Security considerations

Creation of LanguageDetector and Translator objects requires that the user has recently interacted with the page (transient user activation is required).

Access to the API is also controlled via language-detector and translator Permissions-Policy directives.

Examples

For a full example, see Using the Translator and Language Detector APIs.

Specifications

Specification
Unknown specification

Browser compatibility

See also