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>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>
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; }
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);
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>Multilingual Text-to-Speech</h1> <div> <label for="speakerMenu">Voice: </label> <select id="speakerMenu"></select> </div>
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) ... }
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;
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) ... }
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}
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) ... }
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) ... }
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) ... }
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>
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.";
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) ... }
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) ... }
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) ... }
Add the getLookupTable()
utility function:
function getLookupTable(objectsArray, propname) { // ... (same as before) ... }
Add the blurbs
array:
function createBlurbs() { // ... (same as before) ... }
Finally, update talk()
to use the speech rate from rateFld
:
function talk() { // ... (modified talk function as described above) ... }
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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

There are three ways to create a CSS loading rotator: 1. Use the basic rotator of borders to achieve simple animation through HTML and CSS; 2. Use a custom rotator of multiple points to achieve the jump effect through different delay times; 3. Add a rotator in the button and switch classes through JavaScript to display the loading status. Each approach emphasizes the importance of design details such as color, size, accessibility and performance optimization to enhance the user experience.

To deal with CSS browser compatibility and prefix issues, you need to understand the differences in browser support and use vendor prefixes reasonably. 1. Understand common problems such as Flexbox and Grid support, position:sticky invalid, and animation performance is different; 2. Check CanIuse confirmation feature support status; 3. Correctly use -webkit-, -moz-, -ms-, -o- and other manufacturer prefixes; 4. It is recommended to use Autoprefixer to automatically add prefixes; 5. Install PostCSS and configure browserslist to specify the target browser; 6. Automatically handle compatibility during construction; 7. Modernizr detection features can be used for old projects; 8. No need to pursue consistency of all browsers,

Use the clip-path attribute of CSS to crop elements into custom shapes, such as triangles, circular notches, polygons, etc., without relying on pictures or SVGs. Its advantages include: 1. Supports a variety of basic shapes such as circle, ellipse, polygon, etc.; 2. Responsive adjustment and adaptable to mobile terminals; 3. Easy to animation, and can be combined with hover or JavaScript to achieve dynamic effects; 4. It does not affect the layout flow, and only crops the display area. Common usages are such as circular clip-path:circle (50pxatcenter) and triangle clip-path:polygon (50%0%, 100 0%, 0 0%). Notice

Themaindifferencesbetweendisplay:inline,block,andinline-blockinHTML/CSSarelayoutbehavior,spaceusage,andstylingcontrol.1.Inlineelementsflowwithtext,don’tstartonnewlines,ignorewidth/height,andonlyapplyhorizontalpadding/margins—idealforinlinetextstyling

Setting the style of links you have visited can improve the user experience, especially in content-intensive websites to help users navigate better. 1. Use CSS's: visited pseudo-class to define the style of the visited link, such as color changes; 2. Note that the browser only allows modification of some attributes due to privacy restrictions; 3. The color selection should be coordinated with the overall style to avoid abruptness; 4. The mobile terminal may not display this effect, and it is recommended to combine it with other visual prompts such as icon auxiliary logos.

To create responsive images using CSS, it can be mainly achieved through the following methods: 1. Use max-width:100% and height:auto to allow the image to adapt to the container width while maintaining the proportion; 2. Use HTML's srcset and sizes attributes to intelligently load the image sources adapted to different screens; 3. Use object-fit and object-position to control image cropping and focus display. Together, these methods ensure that the images are presented clearly and beautifully on different devices.

The choice of CSS units depends on design requirements and responsive requirements. 1.px is used for fixed size, suitable for precise control but lack of elasticity; 2.em is a relative unit, which is easily caused by the influence of the parent element, while rem is more stable based on the root element and is suitable for global scaling; 3.vw/vh is based on the viewport size, suitable for responsive design, but attention should be paid to the performance under extreme screens; 4. When choosing, it should be determined based on whether responsive adjustments, element hierarchy relationships and viewport dependence. Reasonable use can improve layout flexibility and maintenance.

Different browsers have differences in CSS parsing, resulting in inconsistent display effects, mainly including the default style difference, box model calculation method, Flexbox and Grid layout support level, and inconsistent behavior of certain CSS attributes. 1. The default style processing is inconsistent. The solution is to use CSSReset or Normalize.css to unify the initial style; 2. The box model calculation method of the old version of IE is different. It is recommended to use box-sizing:border-box in a unified manner; 3. Flexbox and Grid perform differently in edge cases or in old versions. More tests and use Autoprefixer; 4. Some CSS attribute behaviors are inconsistent. CanIuse must be consulted and downgraded.
