Translator: translateStreaming() method

Khả dụng hạn chế

This feature is not Baseline because it does not work in some of the most widely-used browsers.

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

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Phương thức translateStreaming() của giao diện Translator tạo bản dịch dưới dạng ReadableStream.

Cú pháp

js
translateStreaming(input)
translateStreaming(input, options)

Tham số

input

Một chuỗi đại diện cho văn bản cần dịch.

options Optional

Một đối tượng chỉ định các tùy chọn cấu hình cho thao tác translateStreaming(). Các giá trị có thể bao gồm:

signal

Một đối tượng AbortSignal, cho phép hủy thao tác translateStreaming() qua AbortController liên kết.

Giá trị trả về

Một ReadableStream chứa bản dịch đã tạo.

Ngoại lệ

AbortError DOMException

Được ném ra nếu Translator đã bị phá hủy trước đó (đã có Translator.destroy() được gọi trên nó, hoặc bị hủy qua signal hủy bỏ sau khi tạo).

InvalidStateError DOMException

Được ném ra nếu Document hiện tại không hoạt động.

QuotaExceededError

Được ném ra nếu thao tác dịch vượt quá inputQuota có sẵn.

Ví dụ

Sử dụng cơ bản translate()

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

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

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

console.log("Complete translation:", translation);

Thông số kỹ thuật

Thông số kỹ thuật
Translator and Language Detector APIs
# dom-translator-translatestreaming

Tương thích trình duyệt

Xem thêm