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

Translator and Language Detector APIs

· 52CE 我爱测

The **Translator 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).

Translator and Language Detector APIs

The Translator 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 the LanguageDetector interface. A LanguageDetector object instance is created using the LanguageDetector.create_static static method, then the LanguageDetector.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 static method, then the Translator.translate instance method is passed the text string to translate.

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

  • CreateMonitor
    • : Provides information on the progress of an AI model download, for example a language pack or some fine-tuning data.
  • LanguageDetector
    • : 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
    • : 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 Permissions-Policy/language-detector directive
    • : Controls access to the language detection functionality. Where a policy specifically disallows its use, the LanguageDetector.availability_static static method will return unavailable, and any attempts to call other LanguageDetector methods will fail with a NotAllowedError DOMException.
  • Permissions-Policy; the Permissions-Policy/translator directive
    • : Controls access to the translation functionality. Where a policy specifically disallows its use, the Translator.availability_static static method will return unavailable, and any attempts to call other 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 Permissions-Policy/language-detector and Permissions-Policy/translator Permissions-Policy directives.

Examples

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

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

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

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