From 722a8b265c407b4d5cd3ab48cd428e58a2f2a8fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Fri, 26 Jan 2024 13:46:36 -0300 Subject: [PATCH] stores: alert: Use platform language by default over default speech MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Usually the speech language should be the same as the platform language, but it appears that some browsers have a weird default speech language. To be safer, we use by default the current platform language, and if that does not exist, the default speech language. Signed-off-by: Patrick José Pereira --- src/stores/alert.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/stores/alert.ts b/src/stores/alert.ts index 55615527d..ad42a4c94 100644 --- a/src/stores/alert.ts +++ b/src/stores/alert.ts @@ -54,13 +54,31 @@ export const useAlertStore = defineStore('alert', () => { // We need to cache these otherwise they get garbage collected... const utterance_cache: SpeechSynthesisUtterance[] = [] + // By default we use the platform language over the default speech text, + // it appears that browsers like chrome fail to have it correctly based on the system. + // The default speech langauge _should_ be the same as platform language. synth.onvoiceschanged = () => { + let default_speech = undefined + let default_speech_by_language = undefined synth.getVoices().forEach((voice) => { availableAlertSpeechVoices.push(voice) - if (selectedAlertSpeechVoiceName.value === undefined && voice.default) { - selectedAlertSpeechVoiceName.value = voice.name + + if (voice.default) { + default_speech = voice + } + + if (voice.lang === navigator.language) { + default_speech_by_language = voice } }) + + if (selectedAlertSpeechVoiceName.value === undefined) { + if (default_speech_by_language !== undefined) { + selectedAlertSpeechVoiceName.value = default_speech_by_language.name + return + } + selectedAlertSpeechVoiceName.value = default_speech.name + } } const availableAlertSpeechVoiceNames = computed(() =>