Get the human readable language value for the reporter block.
@return {string} the language name of the project viewer.
*/
getViewerLanguage () {
this._viewerLanguageCode = this.getViewerLanguageCode();
const names = languageNames.menuMap[this._viewerLanguageCode];
const langNameObj = names.find(obj => obj.code === this._viewerLanguageCode);
let langName = this._viewerLanguageCode;
if (langNameObj) {
langName = langNameObj.name;
}
return langName;
}
/**
Get the viewer's language code.
@return {string} the language code.
*/
getViewerLanguageCode () {
const locale = formatMessage.setup().locale;
const viewerLanguages = [locale].concat(navigator.languages);
const languageKeys = Object.keys(languageNames.menuMap);
// Return the first entry in viewerLanguages that matches
// one of the available language keys.
const languageCode = viewerLanguages.reduce((acc, lang) => {
if (acc) {
return acc;
}
if (languageKeys.indexOf(lang) > -1) {
return lang;
}
return acc;
}, '') || 'en';
return languageCode;
}
/**
Get a language code from a block argument. The arg can be a language code
or a language name, written in any language.
@param {object} arg A block argument.
@return {string} A language code.
*/
getLanguageCodeFromArg (arg) {
const languageArg = Cast.toString(arg).toLowerCase();
// Check if the arg matches a language code in the menu.
if (languageNames.menuMap.hasOwnProperty(languageArg)) {
return languageArg;
}
// Check for a dropped-in language name, and convert to a language code.
if (languageNames.nameMap.hasOwnProperty(languageArg)) {
return languageNames.nameMap[languageArg];
}
// Default to English.
return 'en';
}
/**
Translates the text in the translate block to the language specified in the menu.
@param {object} args - the block arguments.
@return {Promise} - a promise that resolves after the response from the translate server.
*/
getTranslate (args) {
// Don't remake the request if we already have the value.
if (this._lastTextTranslated === args.WORDS &&
this._lastLangTranslated === args.LANGUAGE) {
return this._translateResult;
}
const lang = this.getLanguageCodeFromArg(args.LANGUAGE);
Scratch 资源 Ereignisse 事件
Wenn flag angeklickt 当开始的旗被点击了
Wenn Taste [] gedrückt 当某个按键给按下了
Wenn diese Figur angeklickt
Wenn das Bühnenbild zu [] wechselt
Wenn Lautstärke > 10
Wenn ich Nachricht1 emfange
sende Nachricht1 an alle
sende Nachricht1 an alle und warte
壹 通用的计算概念
贰 扩展开发
/**
/**
/**
/**
/**
@constructor */ class Scratch3TranslateBlocks { constructor () { /**
@private */ this._viewerLanguageCode = this.getViewerLanguageCode();
/**
@private */ this._supportedLanguages = [];
/**
@private */ this._randomLanguageCode = 'en';
/**
@private */ this._translateResult = '';
/**
@private */ this._lastLangTranslated = '';
/**
/**
/**
@returns {object} 扩展的元数据 和 扩展的积木. / getInfo () { this._supportedLanguages = this._getSupportedLanguages(this.getViewerLanguageCode()); this._randomLanguageCode = this._supportedLanguages[ Math.floor(Math.random() this._supportedLanguages.length)].value;
return { id: 'translate', name: formatMessage({ id: 'translate.categoryName', default: 'Translate', description: 'Name of extension that adds translate blocks' }), blockIconURI: blockIconURI, menuIconURI: menuIconURI, blocks: [ { opcode: 'getTranslate', text: formatMessage({ id: 'translate.translateBlock', default: 'translate [WORDS] to [LANGUAGE]', description: 'translate some text to a different language' }), blockType: BlockType.REPORTER, arguments: { WORDS: { type: ArgumentType.STRING, defaultValue: formatMessage({ id: 'translate.defaultTextToTranslate', default: 'hello', description: 'hello: the default text to translate' }) }, LANGUAGE: { type: ArgumentType.STRING, menu: 'languages', defaultValue: this._randomLanguageCode } } }, { opcode: 'getViewerLanguage', text: formatMessage({ id: 'translate.viewerLanguage', default: 'language', description: 'the languge of the project viewer' }), blockType: BlockType.REPORTER, arguments: {} } ], menus: { languages: this._supportedLanguages } }; }
/**
/**
/**
/**
@return {Promise} - a promise that resolves after the response from the translate server. */ getTranslate (args) { // Don't remake the request if we already have the value. if (this._lastTextTranslated === args.WORDS && this._lastLangTranslated === args.LANGUAGE) { return this._translateResult; }
const lang = this.getLanguageCodeFromArg(args.LANGUAGE);
let urlBase =
${serverURL}translate?language=
; urlBase += lang; urlBase += '&text='; urlBase += encodeURIComponent(args.WORDS);const tempThis = this; const translatePromise = new Promise(resolve => { nets({ url: urlBase, timeout: serverTimeoutMs }, (err, res, body) => { if (err) { log.warn(
error fetching translate result! ${res}
); resolve(''); return ''; } const translated = JSON.parse(body).result; tempThis._translateResult = translated; // Cache what we just translated so we don't keep making the // same call over and over. tempThis._lastTextTranslated = args.WORDS; tempThis._lastLangTranslated = args.LANGUAGE; resolve(translated); return translated; });}); translatePromise.then(translatedText => translatedText); return translatePromise; } } module.exports = Scratch3TranslateBlocks;
http://localhost:8601/ 要应该可以看到scratch 3.0启动了。
利用 SCRATCH-VM 開發 SCRATCH 3.0 EXTENSION
再探 SCRATCH-VM 架構下的 SCRATCH 3.0 EXTENSION
Scratch 资源 Ereignisse 事件 Wenn flag angeklickt 当开始的旗被点击了 Wenn Taste [] gedrückt 当某个按键给按下了 Wenn diese Figur angeklickt Wenn das Bühnenbild zu [] wechselt Wenn Lautstärke > 10 Wenn ich Nachricht1 emfange sende Nachricht1 an alle sende Nachricht1 an alle und warte
Steuerung 控制
microbit扩展的写法 https://github.com/LLK/scratch-vm/blob/develop/src/extensions/scratch3_microbit/index.js
叁 一些tips
发布scratch3-gui到github上面去 https://github.com/LLK/scratch-gui/wiki/Publishing-to-GitHub-Pages
连接到利用Websocket 连接esp8266控制gpio https://github.com/MrYsLab/s3onegpio/blob/master/scratch-vm/src/extensions/scratch3_onegpioEsp/index.js
scratch3里面可以开发的积木类型。 https://medium.com/@hiroyuki.osaki/scratch-3-block-types-you-can-develop-and-samples-191b0d769b91