Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. Translator and Language Detector APIs

Translator and Language Detector APIs

Experimental:This is anexperimental technology
Check theBrowser compatibility table carefully before using this in production.

Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.

TheTranslator and Language Detector APIs provide functionality to detect the language that text is written in, and to translate text into different languages, via a browser's own internal AI model (which may differ between browsers).

Concepts and usage

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

  • 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 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.

  • Language detection is done via theLanguageDetector interface. ALanguageDetector object instance is created using theLanguageDetector.create() static method, then thedetect() instance method is passed the text string to detect the language for.
  • Translation is done via theTranslator interface. ATranslator object instance is created using theTranslator.create() static method, then thetranslate() instance method is passed the text string to translate.

You can cancel a pendingcreate(),detect(), ortranslate() operation using anAbortController.

After aLanguageDetector orTranslator instance has been created, you can release its assigned resources and stop any further activity by calling itsLanguageDetector.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.

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

Interfaces

LanguageDetectorExperimental

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

TranslatorExperimental

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

HTTP headers

Permissions-Policy; thelanguage-detector directive

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

Permissions-Policy; thetranslator directive

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

Security considerations

Creation ofLanguageDetector andTranslator objects requires that the user has recently interacted with the page (transient user activation is required).

Access to the API is also controlled vialanguage-detector andtranslatorPermissions-Policy directives.

Examples

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

Specifications

Specification
Unknown specification

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp