2018-02-23 16:53:38 -08:00
|
|
|
const ArgumentType = require('../../extension-support/argument-type');
|
|
|
|
const BlockType = require('../../extension-support/block-type');
|
2018-05-23 17:00:59 -04:00
|
|
|
const Cast = require('../../util/cast');
|
2018-02-23 16:53:38 -08:00
|
|
|
const log = require('../../util/log');
|
|
|
|
const nets = require('nets');
|
2018-05-23 17:00:59 -04:00
|
|
|
const languageNames = require('scratch-translate-extension-languages');
|
|
|
|
const formatMessage = require('format-message');
|
2018-02-23 16:53:38 -08:00
|
|
|
|
|
|
|
// TODO: Change these to the correct icons.
|
|
|
|
const blockIconURI = 'https://www.gstatic.com/images/icons/material/system/1x/translate_white_24dp.png';
|
|
|
|
const menuIconURI = 'https://www.gstatic.com/images/icons/material/system/1x/translate_grey600_24dp.png';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The url of the translate server.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
const serverURL = 'https://translate-service.scratch.mit.edu/';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* How long to wait in ms before timing out requests to translate server.
|
|
|
|
* @type {int}
|
|
|
|
*/
|
|
|
|
const serverTimeoutMs = 10000; // 10 seconds (chosen arbitrarily).
|
|
|
|
|
|
|
|
/**
|
2018-05-23 17:00:59 -04:00
|
|
|
* Class for the translate block in Scratch 3.0.
|
2018-02-23 16:53:38 -08:00
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
class Scratch3TranslateBlocks {
|
|
|
|
constructor () {
|
|
|
|
/**
|
2018-05-23 17:00:59 -04:00
|
|
|
* Language code of the viewer, based on their locale.
|
|
|
|
* @type {string}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._viewerLanguageCode = this.getViewerLanguageCode();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of supported language name and language code pairs, for use in the block menu.
|
2018-02-23 16:53:38 -08:00
|
|
|
* @type {Array.<object.<string, string>>}
|
|
|
|
* @private
|
|
|
|
*/
|
2018-05-23 17:00:59 -04:00
|
|
|
this._supportedLanguages = languageNames.menuMap[this._viewerLanguageCode].map(entry => {
|
|
|
|
const obj = {text: entry.name, value: entry.code};
|
|
|
|
return obj;
|
|
|
|
});
|
2018-02-23 16:53:38 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The result from the most recent translation.
|
|
|
|
* @type {string}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._translateResult = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The language of the text most recently translated.
|
|
|
|
* @type {string}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._lastLangTranslated = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The text most recently translated.
|
|
|
|
* @type {string}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._lastTextTranslated = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The key to load & store a target's translate state.
|
|
|
|
* @return {string} The key.
|
|
|
|
*/
|
|
|
|
static get STATE_KEY () {
|
|
|
|
return 'Scratch.translate';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {object} metadata for this extension and its blocks.
|
|
|
|
*/
|
|
|
|
getInfo () {
|
|
|
|
return {
|
|
|
|
id: 'translate',
|
|
|
|
name: 'Translate',
|
|
|
|
menuIconURI: menuIconURI,
|
|
|
|
blockIconURI: blockIconURI,
|
|
|
|
blocks: [
|
|
|
|
{
|
|
|
|
opcode: 'getTranslate',
|
|
|
|
text: 'translate [WORDS] to [LANGUAGE]',
|
|
|
|
blockType: BlockType.REPORTER,
|
|
|
|
arguments: {
|
|
|
|
WORDS: {
|
|
|
|
type: ArgumentType.STRING,
|
|
|
|
defaultValue: 'hello'
|
|
|
|
},
|
|
|
|
LANGUAGE: {
|
|
|
|
type: ArgumentType.STRING,
|
2018-05-23 17:00:59 -04:00
|
|
|
menu: 'languages',
|
|
|
|
defaultValue: this._viewerLanguageCode
|
2018-02-23 16:53:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
menus: {
|
2018-05-23 17:00:59 -04:00
|
|
|
languages: this._supportedLanguages
|
2018-02-23 16:53:38 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2018-05-23 17:00:59 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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';
|
|
|
|
}
|
|
|
|
|
2018-02-23 16:53:38 -08:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2018-05-23 17:00:59 -04:00
|
|
|
const lang = this.getLanguageCodeFromArg(args.LANGUAGE);
|
2018-02-23 16:53:38 -08:00
|
|
|
|
2018-05-23 17:00:59 -04:00
|
|
|
let urlBase = `${serverURL}translate?language=`;
|
2018-02-23 16:53:38 -08:00
|
|
|
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;
|