mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 14:32:59 -05:00
Add “set language” block (#1610)
This commit is contained in:
parent
e2c6c7667b
commit
622aa0f2b0
1 changed files with 69 additions and 2 deletions
|
@ -62,6 +62,12 @@ class Scratch3Text2SpeechBlocks {
|
|||
*/
|
||||
this.runtime = runtime;
|
||||
|
||||
/**
|
||||
* The current language code to use for speech synthesis.
|
||||
* @type {string}
|
||||
*/
|
||||
this.currentLanguage = 'en-US';
|
||||
|
||||
/**
|
||||
* Map of soundPlayers by sound id.
|
||||
* @type {Map<string, SoundPlayer>}
|
||||
|
@ -132,6 +138,28 @@ class Scratch3Text2SpeechBlocks {
|
|||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* An object with language names mapped to their language codes.
|
||||
*/
|
||||
get LANGUAGE_INFO () {
|
||||
return {
|
||||
'Danish': 'da-DK',
|
||||
'Dutch': 'nl-NL',
|
||||
'English': 'en-US',
|
||||
'French': 'fr-FR',
|
||||
'German': 'de-DE',
|
||||
'Icelandic': 'is-IS',
|
||||
'Italian': 'it-IT',
|
||||
'Japanese': 'ja-JP',
|
||||
'Polish': 'pl-PL',
|
||||
'Portuguese (Brazilian)': 'pt-BR',
|
||||
'Portuguese (European)': 'pt-PT',
|
||||
'Russian': 'ru-RU',
|
||||
'Spanish (European)': 'es-ES',
|
||||
'Spanish (Latin American)': 'es-US'
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The key to load & store a target's text2speech state.
|
||||
* @return {string} The key.
|
||||
|
@ -224,10 +252,27 @@ class Scratch3Text2SpeechBlocks {
|
|||
defaultValue: QUINN_ID
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
opcode: 'setLanguage',
|
||||
text: formatMessage({
|
||||
id: 'text2speech.setLanguageBlock',
|
||||
default: 'set language to [LANGUAGE]',
|
||||
description: 'Set the language for speech synthesis.'
|
||||
}),
|
||||
blockType: BlockType.COMMAND,
|
||||
arguments: {
|
||||
LANGUAGE: {
|
||||
type: ArgumentType.STRING,
|
||||
menu: 'languages',
|
||||
defaultValue: this.currentLanguage
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
menus: {
|
||||
voices: this.getVoiceMenu()
|
||||
voices: this.getVoiceMenu(),
|
||||
languages: this.getLanguageMenu()
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -260,6 +305,17 @@ class Scratch3Text2SpeechBlocks {
|
|||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the menu of languages for the "set language" block.
|
||||
* @return {array} the text and value for each menu item.
|
||||
*/
|
||||
getLanguageMenu () {
|
||||
return Object.keys(this.LANGUAGE_INFO).map(languageName => ({
|
||||
text: languageName,
|
||||
value: this.LANGUAGE_INFO[languageName]
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the voice for speech synthesis for this sprite.
|
||||
* @param {object} args Block arguments
|
||||
|
@ -274,6 +330,17 @@ class Scratch3Text2SpeechBlocks {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the language for speech synthesis.
|
||||
* @param {object} args Block arguments
|
||||
*/
|
||||
setLanguage (args) {
|
||||
// Only set the language if the arg is a valid language code.
|
||||
if (Object.values(this.LANGUAGE_INFO).includes(args.LANGUAGE)) {
|
||||
this.currentLanguage = args.LANGUAGE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop all currently playing speech sounds.
|
||||
*/
|
||||
|
@ -305,7 +372,7 @@ class Scratch3Text2SpeechBlocks {
|
|||
|
||||
// Build up URL
|
||||
let path = `${SERVER_HOST}/synth`;
|
||||
path += `?locale=${this.getViewerLanguageCode()}`;
|
||||
path += `?locale=${this.currentLanguage}`;
|
||||
path += `&gender=${gender}`;
|
||||
path += `&text=${encodeURI(words.substring(0, 128))}`;
|
||||
|
||||
|
|
Loading…
Reference in a new issue