Translator: translate() method

Limited availability

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 translate() của giao diện Translator trả về bản dịch của một chuỗi đầu vào.

Cú pháp

js
translate(input)
translate(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 translate(). Các giá trị có thể bao gồm:

signal

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

Giá trị trả về

Một Promise thực hiện với một chuỗi 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 translation = await translator.translate(myTextString);
console.log(translation);

Thông số kỹ thuật

Specification
Translator and Language Detector APIs
# dom-translator-translate

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

Xem thêm