From 653ae1193ce5837b89381caf19590f4f2aab2588 Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 1 Oct 2021 16:24:50 +0100 Subject: [PATCH] moved tts into its own file, added support for bcp47 codes to enable multilanguage. closes #397 --- gulpfile.js | 1 + src/js/config.js | 12 +++++++++++- src/js/test/funbox.js | 9 ++------- src/js/test/tts.js | 27 +++++++++++++++++++++++++++ static/languages/polish.json | 1 + 5 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 src/js/test/tts.js diff --git a/gulpfile.js b/gulpfile.js index 2adbbcb33..7224040ac 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -179,6 +179,7 @@ const refactoredSrc = [ "./src/js/test/today-tracker.js", "./src/js/test/weak-spot.js", "./src/js/test/wordset.js", + "./src/js/test/tts.js", "./src/js/replay.js", ]; diff --git a/src/js/config.js b/src/js/config.js index f2e6fef3c..00de4d702 100644 --- a/src/js/config.js +++ b/src/js/config.js @@ -19,6 +19,7 @@ import * as CommandlineLists from "./commandline-lists"; import * as BackgroundFilter from "./custom-background-filter"; import LayoutList from "./layouts"; import * as ChallengeContoller from "./challenge-controller"; +import * as TTS from "./tts"; export let localStorageConfig = null; export let dbConfigLoaded = false; @@ -339,8 +340,14 @@ export function setFavThemes(themes, nosave) { } export function setFunbox(funbox, nosave) { - config.funbox = funbox ? funbox : "none"; + let val = funbox ? funbox : "none"; + config.funbox = val; ChallengeContoller.clearActive(); + if (val === "none") { + TTS.clear(); + } else if (val === "tts") { + TTS.init(); + } if (!nosave) { saveToLocalStorage(); } @@ -1332,6 +1339,9 @@ export function setLanguage(language, nosave) { language = "english"; } config.language = language; + if (config.funbox === "tts") { + TTS.setLanguage(); + } try { firebase.analytics().logEvent("changedLanguage", { language: language, diff --git a/src/js/test/funbox.js b/src/js/test/funbox.js index 6455d10cb..491f731be 100644 --- a/src/js/test/funbox.js +++ b/src/js/test/funbox.js @@ -5,6 +5,7 @@ import * as Misc from "./misc"; import * as ManualRestart from "./manual-restart-tracker"; import Config, * as UpdateConfig from "./config"; import * as Settings from "./settings"; +import * as TTS from "./tts"; let modeSaved = null; let memoryTimer = null; @@ -79,13 +80,7 @@ export function reset() { export function toggleScript(...params) { if (Config.funbox === "tts") { - var msg = new SpeechSynthesisUtterance(); - console.log("Speaking"); - msg.text = params[0]; - if (!msg.text) return; - msg.lang = "en-US"; - window.speechSynthesis.cancel(); - window.speechSynthesis.speak(msg); + TTS.speak(params[0]); } } diff --git a/src/js/test/tts.js b/src/js/test/tts.js new file mode 100644 index 000000000..7226fab1d --- /dev/null +++ b/src/js/test/tts.js @@ -0,0 +1,27 @@ +import Config from "./config"; +import * as Misc from "./misc"; + +let voice; + +export async function init() { + voice = new SpeechSynthesisUtterance(); + setLanguage(); +} + +export function clear() { + voice = undefined; +} + +export async function setLanguage(lang = Config.language) { + if (!voice) return; + let language = await Misc.getLanguage(lang); + let bcp = language.bcp47 ? language.bcp47 : "en-US"; + voice.lang = bcp; +} + +export function speak(text) { + if (!voice) init(); + voice.text = text; + window.speechSynthesis.cancel(); + window.speechSynthesis.speak(voice); +} diff --git a/static/languages/polish.json b/static/languages/polish.json index d98686628..539c531df 100644 --- a/static/languages/polish.json +++ b/static/languages/polish.json @@ -1,6 +1,7 @@ { "name": "polish", "leftToRight": true, + "bcp47": "pl-PL", "words": [ "jak", "jego",