SpeechSynthesisUtterance: lang property

Baseline Widely available

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

Thuộc tính lang của giao diện SpeechSynthesisUtterance lấy và đặt ngôn ngữ của yêu cầu phát âm.

Nếu không được đặt, ngôn ngữ của ứng dụng (tức là giá trị lang của <html>) sẽ được dùng, hoặc mặc định của tác nhân người dùng nếu giá trị đó cũng không được đặt.

Giá trị

Một chuỗi đại diện cho BCP 47 language tag.

Ví dụ

js
const synth = window.speechSynthesis;

const inputForm = document.querySelector("form");
const inputTxt = document.querySelector("input");
const voiceSelect = document.querySelector("select");

const voices = synth.getVoices();

// …

inputForm.onsubmit = (event) => {
  event.preventDefault();

  const utterThis = new SpeechSynthesisUtterance(inputTxt.value);
  const selectedOption =
    voiceSelect.selectedOptions[0].getAttribute("data-name");
  for (const voice of voices) {
    if (voice.name === selectedOption) {
      utterThis.voice = voice;
    }
  }
  utterThis.lang = "en-US";
  synth.speak(utterThis);
  inputTxt.blur();
};

Thông số kỹ thuật

Thông số kỹ thuật
Web Speech API
# dom-speechsynthesisutterance-lang

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

Xem thêm