From d059f8baa6c4ae10bc18d45adb1f585af3e4dbef Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Tue, 5 Mar 2019 17:38:54 -0500 Subject: [PATCH] Check and set language, handling many-to-one mapping --- src/extensions/scratch3_text2speech/index.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/extensions/scratch3_text2speech/index.js b/src/extensions/scratch3_text2speech/index.js index b0b6b522b..c9cbdf1a2 100644 --- a/src/extensions/scratch3_text2speech/index.js +++ b/src/extensions/scratch3_text2speech/index.js @@ -476,7 +476,14 @@ class Scratch3Text2SpeechBlocks { if (!stage) return; // Only set the language if it is in the list. if (this.isSupportedLanguage(languageCode)) { - stage.textToSpeechLanguage = languageCode; + // Set the language code used by the extension. There are a few + // languages where we map multiple written languages to one spoken + // language. + for (const lang in this.LANGUAGE_INFO) { + if (this.LANGUAGE_INFO[lang].locales.includes(languageCode)) { + stage.textToSpeechLanguage = lang; + } + } } // If the language is null, set it to the default language. // This can occur e.g. if the extension was loaded with the editor @@ -500,13 +507,18 @@ class Scratch3Text2SpeechBlocks { } /** - * Check if a language code is in the list of supported languages for the + * Check if a Scratch language code is in the list of supported languages for the * speech synthesis service. * @param {string} languageCode the language code to check. * @returns {boolean} true if the language code is supported. */ isSupportedLanguage (languageCode) { - return Object.keys(this.LANGUAGE_INFO).includes(languageCode); + for (const lang in this.LANGUAGE_INFO) { + if (this.LANGUAGE_INFO[lang].locales.includes(languageCode)) { + return true; + } + } + return false; } /**