HTMLElement: thuộc tính translate

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2023.

Thuộc tính translate của giao diện HTMLElement cho biết liệu các giá trị thuộc tính và giá trị của các nút Text con của phần tử có được dịch khi trang được bản địa hóa hay không, hay nên giữ nguyên.

Nó phản ánh giá trị của thuộc tính HTML toàn cục translate.

Giá trị

Một giá trị boolean là true nếu các giá trị thuộc tính và giá trị của các nút Text con của phần tử sẽ được dịch khi trang được bản địa hóa, false trong trường hợp còn lại.

Ví dụ

Ví dụ sau đây cho thấy cách bật hoặc tắt dịch thuật qua tập lệnh:

html
<div>
  <span>The content may always be translated: </span>
  <span translate="yes">El contenido será traducido</span>
</div>
<div>
  <span id="translate-label">The content may be translated:</span>
  <span id="translate-element" translate="no">
    El contenido puede ser traducido.
  </span>
</div>
<input id="translate-controller" type="checkbox" /> Enable translation
js
const label = document.getElementById("translate-label");
const element = document.getElementById("translate-element");
const controller = document.getElementById("translate-controller");

controller.addEventListener("change", (e) => {
  if (controller.checked) {
    element.translate = true;
    label.innerText = "The content may be translated:";
  } else {
    element.translate = false;
    label.innerText = "The content may not be translated:";
  }
});

Thông số kỹ thuật

Thông số kỹ thuật
HTML
# dom-translate

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

Xem thêm