How to read a text has many languages using chrome.tts.speak API?

104 Views Asked by At

I want to use tts chrome extensions API to read a text with multiple languages.

I tried this

const english = (await chrome.tts.getVoices()).find(x => x.lang === "en-US").voiceName

const danish = (await chrome.tts.getVoices()).find(x => x.lang === "da-DK").voiceName

await chrome.tts.speak(`
<?xml version="1.0"?>
<speak version="1.0">
  <voice name="${danish}">
        Jeg er glad for at se dig
  </voice>
  <voice name="${english}">
        I'm good to see you
  </voice>
</speak>
`);

But it reads both texts as English texts.

1

There are 1 best solutions below

4
Rahul Sharma On

It might have to do with document fragmentation. I also tried this using the back-tick same way as you're doing and it uttered all the words even the tags, attributes, question marks of xml. Then I changed to this:

chrome.tts.speak(
'<?xml version="1.0"?>' +
'<speak>' +
  `<voice name=${danish}>` +
      'Jeg er glad for at se dig' +
  '</voice>' +
  `<voice name=${english}>` +
      "I am good to see you" +
  '</voice>' +
'</speak>'
);

and it worked.