Chrome android text to speech not changing languag

2019-08-17 03:39发布

The code below works fine in Chrome desktop, but in Chrome Android, it's not using the msg.lang specified. The French text is being read out as if it was English, in an American accent. My phone's default language is English, does that matter? I want the page to read out in the selected language regardless of what settings the user has on their phone.

    const msg = new SpeechSynthesisUtterance();
    msg.volume = 1; 
    msg.text = text; // these words are in French
    msg.lang = 'fr-FR';
    speechSynthesis.speak(msg);

1条回答
倾城 Initia
2楼-- · 2019-08-17 04:01

Changing the dash to an underscore fixed it, i.e., from 'fr-FR' to 'fr_FR'.

The documentation https://w3c.github.io/speech-api/#tts-section states that the code should be "BCP 47", which specifies a dash.

However, this article indicates that in some mobile implementations an underscore is used: https://manu.ninja/using-the-speech-synthesis-interface-of-the-web-speech-api

So making it an underscore fixed it. And the underscore works on desktop Chrome, too.

I won't mark this answer as accepted, in case there is a better answer.

查看更多
登录 后发表回答