Reset the supported language list in getInfo. This works because refreshBlocks is called

when the language gets changed. Fixes #1365.
This commit is contained in:
picklesrus 2018-09-17 14:04:32 -04:00
parent fa0af58c41
commit 5966a54500

View file

@ -31,24 +31,23 @@ class Scratch3TranslateBlocks {
* @private * @private
*/ */
this._viewerLanguageCode = this.getViewerLanguageCode(); this._viewerLanguageCode = this.getViewerLanguageCode();
/** /**
* List of supported language name and language code pairs, for use in the block menu. * 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.<object.<string, string>>} * @type {Array.<object.<string, string>>}
* @private * @private
*/ */
this._supportedLanguages = languageNames.menuMap[this._viewerLanguageCode].map(entry => { this._supportedLanguages = [];
const obj = {text: entry.name, value: entry.code};
return obj;
});
/** /**
* A randomly selected language code, for use as the default value in the language menu. * 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} * @type {string}
* @private * @private
*/ */
this._randomLanguageCode = this._supportedLanguages[ this._randomLanguageCode = 'en';
Math.floor(Math.random() * this._supportedLanguages.length)].value;
/** /**
* The result from the most recent translation. * The result from the most recent translation.
@ -84,6 +83,10 @@ class Scratch3TranslateBlocks {
* @returns {object} metadata for this extension and its blocks. * @returns {object} metadata for this extension and its blocks.
*/ */
getInfo () { getInfo () {
this._supportedLanguages = this._getSupportedLanguages(this.getViewerLanguageCode());
this._randomLanguageCode = this._supportedLanguages[
Math.floor(Math.random() * this._supportedLanguages.length)].value;
return { return {
id: 'translate', id: 'translate',
name: formatMessage({ name: formatMessage({
@ -113,6 +116,8 @@ class Scratch3TranslateBlocks {
type: ArgumentType.STRING, type: ArgumentType.STRING,
menu: 'languages', menu: 'languages',
defaultValue: this._randomLanguageCode 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.<object.<string, string>>} 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. * Get the human readable language value for the reporter block.
* @return {string} the language name of the project viewer. * @return {string} the language name of the project viewer.