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

Translator

· 52CE 我爱测

The **`Translator`** interface of the Translator and Language Detector APIs contains all the associated translation functionality, including checking AI model availability, creating a new `Translator` instance, using it to create a translation, and more.

Translator and Language Detector APIs

The Translator interface of the Translator and Language Detector APIs contains all the associated translation functionality, including checking AI model availability, creating a new Translator instance, using it to create a translation, and more.

Instance properties

  • Translator.inputQuota
    • : The input quota available to the browser for generating translations.
  • Translator.sourceLanguage
    • : The expected language of the input text to be translated.
  • Translator.targetLanguage
    • : The language that the input text will be translated into.

Static methods

  • Translator.availability_static
    • : Returns an enumerated value that indicates the availability of the AI model for the given Translator configuration.
  • Translator.create_static
    • : Creates a new Translator instance from which to generate translations.

Instance methods

  • Translator.destroy
    • : Releases the resources assigned to the Translator instance it is called on and stops any further activity on it.
  • Translator.measureInputUsage
    • : Reports how much input quota would be used by a translation operation for a given text input.
  • Translator.translate
    • : Returns a string containing a translation of the input string.
  • Translator.translateStreaming
    • : Generates a translation of the input string as a ReadableStream.

Examples

See Using the Translator and Language Detector APIs for a complete example.

Creating a Translator instance

const translator = await Translator.create({
  sourceLanguage: "en",
  targetLanguage: "ja",
});

Generating a translation

const translation = await translator.translate(myTextString);
console.log(translation);

Generating a translation stream

const stream = translator.translateStreaming(myTextString);
let translation = "";

for await (const chunk of stream) {
  translation += chunk;
}

console.log("Stream complete");
console.log(translation);

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

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

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