Table of Contents
Step 1: A Simple Foundation
Simple Text-to-Speech
Step 2: International Voice Selection
Multilingual Text-to-Speech
Step 3: The Complete Polyglot Application
Home Web Front-end CSS Tutorial Using the Web Speech API for Multilingual Translations

Using the Web Speech API for Multilingual Translations

Apr 22, 2025 am 11:23 AM

Using the Web Speech API for Multilingual Translations

Ever since science fiction's early days, we've dreamed of machines that converse with us. Today, this is commonplace. However, the technology for enabling websites to "speak" is still relatively new.

The Web Speech API's SpeechSynthesis component allows us to create talking web pages. While still considered experimental, it boasts excellent support in the latest Chrome, Safari, and Firefox versions.

A particularly exciting aspect is its use with multiple languages. Mac OSX and most Windows systems offer robust cross-browser support. Chrome dynamically loads voices, so even if your OS lacks international voices, Chrome will provide them. We'll build a three-step page that speaks the same text in various languages. The core code is adapted from existing documentation, but our final version adds enhanced features and is viewable on my Polyglot CodePen.

Step 1: A Simple Foundation

Let's begin with a basic page containing a text input for the speech content and a button to trigger the speech.

<div>
  <h1 id="Simple-Text-to-Speech">Simple Text-to-Speech</h1>
  <p id="warning">Sorry, your browser doesn't support the Web Speech API.</p>  
  <textarea id="txtFld" placeholder="Type text here..."></textarea><br>
  <button id="speakBtn">Speak</button><br>
  <p>Note: For optimal Mac performance, use the latest Chrome, Safari, or Firefox. On Windows, use Chrome.</p>
</div>
Copy after login

The paragraph with the ID "warning" only appears if JavaScript detects Web Speech API incompatibility. Note the IDs for the textarea and button; we'll use them in our JavaScript.

Feel free to customize the HTML styling. You can also use my demo as a starting point.

It's advisable to style the disabled button state to avoid confusion for users with incompatible browsers (like the outdated Internet Explorer). We'll also hide the warning initially using CSS:

button:disabled {
  cursor: not-allowed;
  opacity: 0.3;
}

#warning {
  color: red;
  display: none;
  font-size: 1.4rem;
}
Copy after login

Now for the JavaScript! We'll define variables referencing the "Speak" button and the textarea. An event listener ensures the init() function executes after the DOM loads. I use a helper function, "qs," (defined below) as a shortcut for document.querySelector. An event listener on speakBtn calls the talk() function.

The talk() function creates a SpeechSynthesisUtterance object (part of the Web Speech API), assigns the textarea's text to its text property, and then uses speechSynthesis.speak() to play the audio. The voice varies depending on the browser and OS. On my Mac, the default is Alex (American English). In Step 2, we'll add a voice selection menu.

let speakBtn, txtFld;

function init() {
  speakBtn = qs("#speakBtn");
  txtFld = qs("#txtFld");
  speakBtn.addEventListener("click", talk, false);
  if (!window.speechSynthesis) {
    speakBtn.disabled = true;
    qs("#warning").style.display = "block";
  }
}

function talk() {
  let u = new SpeechSynthesisUtterance();
  u.text = txtFld.value;
  speechSynthesis.speak(u);
}

// Reusable utility function
function qs(selectorText) {
  return document.querySelector(selectorText);
}

document.addEventListener('DOMContentLoaded', init);
Copy after login

Step 2: International Voice Selection

To use languages beyond the default, we need additional code. Let's add a select element for voice options:

<h1 id="Multilingual-Text-to-Speech">Multilingual Text-to-Speech</h1>
<div>
  <label for="speakerMenu">Voice: </label>
  <select id="speakerMenu"></select>
</div>
Copy after login

Before populating the menu, we'll map language codes to names. Each language has a two-letter code (e.g., "en" for English, "es" for Spanish). We'll create an array of objects like {"code": "pt", "name": "Portuguese"}. A helper function will search this array for a specific property value. We'll use it to find the language name matching the selected voice's code. Add the following functions:

function getLanguageTags() {
  // ... (same as before) ...
}

function searchObjects(array, prop, term, caseSensitive = false) {
  // ... (same as before) ...
}
Copy after login

Now, let's populate the select element's options using JavaScript. We'll declare variables for the #speakerMenu select element, a placeholder for language display (removed later), the array of voices (allVoices), an array of language codes (langtags), and a variable to track the selected voice (voiceIndex).

let speakBtn, txtFld, speakerMenu, allVoices, langtags, voiceIndex = 0;
Copy after login

The updated init() function adds references to #speakerMenu and calls setUpVoices() if the Web Speech API is supported. For Chrome, we listen for voice changes and re-run the setup. Chrome handles voices asynchronously, requiring this extra step.

function init() {
  // ... (modified init function as described above) ...
}
Copy after login
Copy after login

The setUpVoices() function retrieves SpeechSynthesisVoice objects using speechSynthesis.getVoices(). We use getAllVoices() to handle potential duplicate voices. A unique ID is added to each voice object for later filtering. allVoices will contain objects like:

{id:48, voiceURI:"Paulina", name:"Paulina", lang: "es-MX", localService:true},
{id:52, voiceURI:"Samantha", name:"Samantha", lang: "en-US", localService:true},
{id:72, voiceURI:"Google Deutsch", name:"Google Deutsch", lang: "de-DE", localService:false}
Copy after login

The last line of setUpVoices() calls a function to create the speaker menu options. The voice ID is used as the option's value, and the name and language are displayed.

function setUpVoices() {
  allVoices = getAllVoices();
  createSpeakerMenu(allVoices);
}

function getAllVoices() {
  // ... (same as before) ...
}

function createSpeakerMenu(voices) {
  // ... (same as before) ...
}
Copy after login

The selectSpeaker() function (called when speakerMenu changes) stores the selected index, retrieves the selected voice, extracts the language code, searches langtags for the language name, and updates the display.

function selectSpeaker() {
  // ... (same as before) ...
}
Copy after login

Finally, update talk() to use the selected voice and language, and to allow setting the speech rate:

function talk() {
  // ... (modified talk function as described above) ...
}
Copy after login
Copy after login

This completes Step 2. Experiment with different voices and languages!

Step 3: The Complete Polyglot Application

The final step refines the UI and adds functionality:

  • A language selection menu
  • User-adjustable speech speed
  • A default phrase that translates based on language selection

Here's the updated HTML:

<div>
  <label for="languageMenu">Language: </label>
  <select id="languageMenu"></select>
</div>

<div>
  <label for="rateFld">Speed: </label>
  <input type="number" id="rateFld" min="0.5" max="2" step="0.1" value="0.8">
</div>
Copy after login

We'll modify the JavaScript variable declarations to include: allLanguages, primaryLanguages, langhash, langcodehash, rateFld, languageMenu, and blurbs. A flag, initialSetup, will control the languages menu setup.

let speakBtn, txtFld, speakerMenu, allVoices, langtags, voiceIndex = 0;
let allLanguages, primaryLanguages, langhash, langcodehash;
let rateFld, languageMenu, blurbs;
let initialSetup = true;
let defaultBlurb = "I enjoy the traditional music of my native country.";
Copy after login

The init() function now creates the blurbs array, references rateFld and languageMenu, and creates hash tables for language lookups.

function init() {
  // ... (modified init function as described above) ...
}
Copy after login
Copy after login

setUpVoices() now calls getAllLanguages(), getPrimaryLanguages(), filterVoices(), and createLanguageMenu(). getAllLanguages() extracts unique languages from allVoices, and getPrimaryLanguages() extracts the main language codes.

function setUpVoices() {
  // ... (modified setUpVoices function as described above) ...
}

function getAllLanguages(voices) {
  // ... (same as before) ...
}

function getPrimaryLanguages(langlist) {
  // ... (same as before) ...
}
Copy after login

filterVoices() filters allVoices based on the selected language, populates speakerMenu, and updates the textarea with the appropriate blurb. createLanguageMenu() creates the language menu options. selectLanguage() is called when the language is changed, triggering filterVoices() and resetting the voice selection.

function filterVoices() {
  // ... (same as before) ...
}

function createLanguageMenu() {
  // ... (same as before) ...
}

function selectLanguage() {
  // ... (same as before) ...
}
Copy after login

Add the getLookupTable() utility function:

function getLookupTable(objectsArray, propname) {
  // ... (same as before) ...
}
Copy after login

Add the blurbs array:

function createBlurbs() {
  // ... (same as before) ...
}
Copy after login

Finally, update talk() to use the speech rate from rateFld:

function talk() {
  // ... (modified talk function as described above) ...
}
Copy after login
Copy after login

This completes the polyglot application. The user can now select a language, choose a voice, adjust the speech speed, and hear the selected text spoken in the chosen language. This demonstrates the power and flexibility of the Web Speech API for creating multilingual web applications.

The above is the detailed content of Using the Web Speech API for Multilingual Translations. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles