Translator and Language Detector APIs

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The Translator and Language Detector APIs provide functionality to detect the language a body of text is written in and translate text into different languages via a browser's own internal AI model.

Concepts and usage

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

  • An on-the-fly translation of an article that isn't available in your language.
  • Translating a user's support requests into a language the support agent understands.
  • Facilitating chats between users that don't speak each other's languages.

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 translation and language detection. 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.

  • Language detection is done via the LanguageDetector interface. A LanguageDetector object instance is created using the LanguageDetector.create() static method, then the detect() instance method is passed the text string to detect the language for.
  • Translation is done via the Translator interface. A Translator object instance is created using the Translator.create() static method, then the translate() instance method is passed the text string to translate.

After the above instances have been created, you can remove them again using a destroy() instance method (for example, the Translator.destroy()). You can also cancel pending operations using an AbortController.

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

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.

HTTP headers

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.

Examples

Specifications

Specification
Unknown specification

Browser compatibility

See also