From 5966a54500d17f5ccd065b338d5a8935c1ad1b0c Mon Sep 17 00:00:00 2001 From: picklesrus Date: Mon, 17 Sep 2018 14:04:32 -0400 Subject: [PATCH] Reset the supported language list in getInfo. This works because refreshBlocks is called when the language gets changed. Fixes #1365. --- src/extensions/scratch3_translate/index.js | 32 +++++++++++++++++----- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/extensions/scratch3_translate/index.js b/src/extensions/scratch3_translate/index.js index bb6f3d81f..6a6e2b0d6 100644 --- a/src/extensions/scratch3_translate/index.js +++ b/src/extensions/scratch3_translate/index.js @@ -31,24 +31,23 @@ class Scratch3TranslateBlocks { * @private */ this._viewerLanguageCode = this.getViewerLanguageCode(); - + /** * List of supported language name and language code pairs, for use in the block menu. + * Filled in by getInfo so it is updated when the interface language changes. * @type {Array.>} * @private */ - this._supportedLanguages = languageNames.menuMap[this._viewerLanguageCode].map(entry => { - const obj = {text: entry.name, value: entry.code}; - return obj; - }); + this._supportedLanguages = []; /** * A randomly selected language code, for use as the default value in the language menu. + * Properly filled in getInfo so it is updated when the interface languages changes. * @type {string} * @private */ - this._randomLanguageCode = this._supportedLanguages[ - Math.floor(Math.random() * this._supportedLanguages.length)].value; + this._randomLanguageCode = 'en'; + /** * The result from the most recent translation. @@ -84,6 +83,10 @@ class Scratch3TranslateBlocks { * @returns {object} metadata for this extension and its blocks. */ getInfo () { + this._supportedLanguages = this._getSupportedLanguages(this.getViewerLanguageCode()); + this._randomLanguageCode = this._supportedLanguages[ + Math.floor(Math.random() * this._supportedLanguages.length)].value; + return { id: 'translate', name: formatMessage({ @@ -113,6 +116,8 @@ class Scratch3TranslateBlocks { type: ArgumentType.STRING, menu: 'languages', defaultValue: this._randomLanguageCode + + } } }, @@ -133,6 +138,19 @@ class Scratch3TranslateBlocks { }; } + /** + * Computes a list of language code and name pairs for the given language. + * @param {string} code The language code to get the list of language pairs + * @return {Array.>} An array of languge name and + * language code pairs. + * @private + */ + _getSupportedLanguages (code) { + return languageNames.menuMap[code].map(entry => { + const obj = {text: entry.name, value: entry.code}; + return obj; + }); + } /** * Get the human readable language value for the reporter block. * @return {string} the language name of the project viewer.