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
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
2018-09-17 14:04:32 -04:00
|
|
|
|
2018-05-23 17:00:59 -04:00
|
|
|
/**
|
|
|
|
* List of supported language name and language code pairs, for use in the block menu.
|
2018-09-17 14:04:32 -04:00
|
|
|
* Filled in by getInfo so it is updated when the interface language changes.
|
2018-02-23 16:53:38 -08:00
|
|
|
* @type {Array.<object.<string, string>>}
|
|
|
|
* @private
|
|
|
|
*/
|
2018-09-17 14:04:32 -04:00
|
|
|
this._supportedLanguages = [];
|
2018-02-23 16:53:38 -08:00
|
|
|
|
2018-05-31 11:48:37 -04:00
|
|
|
/**
|
|
|
|
* A randomly selected language code, for use as the default value in the language menu.
|
2018-09-17 14:04:32 -04:00
|
|
|
* Properly filled in getInfo so it is updated when the interface languages changes.
|
2018-05-31 11:48:37 -04:00
|
|
|
* @type {string}
|
|
|
|
* @private
|
|
|
|
*/
|
2018-09-17 14:04:32 -04:00
|
|
|
this._randomLanguageCode = 'en';
|
|
|
|
|
2018-05-31 11:48:37 -04:00
|
|
|
|
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 () {
|
2018-09-17 14:04:32 -04:00
|
|
|
this._supportedLanguages = this._getSupportedLanguages(this.getViewerLanguageCode());
|
|
|
|
this._randomLanguageCode = this._supportedLanguages[
|
|
|
|
Math.floor(Math.random() * this._supportedLanguages.length)].value;
|
|
|
|
|
2018-02-23 16:53:38 -08:00
|
|
|
return {
|
|
|
|
id: 'translate',
|
2018-06-18 11:37:10 -04:00
|
|
|
name: formatMessage({
|
|
|
|
id: 'translate.categoryName',
|
2018-09-11 15:19:19 -04:00
|
|
|
default: 'Translate',
|
|
|
|
description: 'Name of extension that adds translate blocks'
|
2018-06-18 11:37:10 -04:00
|
|
|
}),
|
2018-02-23 16:53:38 -08:00
|
|
|
blocks: [
|
|
|
|
{
|
|
|
|
opcode: 'getTranslate',
|
2018-05-29 15:06:38 -07:00
|
|
|
text: formatMessage({
|
|
|
|
id: 'translate.translateBlock',
|
|
|
|
default: 'translate [WORDS] to [LANGUAGE]',
|
|
|
|
description: 'translate some text to a different language'
|
|
|
|
}),
|
2018-02-23 16:53:38 -08:00
|
|
|
blockType: BlockType.REPORTER,
|
|
|
|
arguments: {
|
|
|
|
WORDS: {
|
|
|
|
type: ArgumentType.STRING,
|
2018-05-29 15:06:38 -07:00
|
|
|
defaultValue: formatMessage({
|
|
|
|
id: 'translate.defaultTextToTranslate',
|
|
|
|
default: 'hello',
|
|
|
|
description: 'hello: the default text to translate'
|
|
|
|
})
|
2018-02-23 16:53:38 -08:00
|
|
|
},
|
|
|
|
LANGUAGE: {
|
|
|
|
type: ArgumentType.STRING,
|
2018-05-23 17:00:59 -04:00
|
|
|
menu: 'languages',
|
2018-05-31 11:48:37 -04:00
|
|
|
defaultValue: this._randomLanguageCode
|
2018-09-17 14:04:32 -04:00
|
|
|
|
|
|
|
|
2018-02-23 16:53:38 -08:00
|
|
|
}
|
|
|
|
}
|
2018-05-28 11:58:17 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'getViewerLanguage',
|
2018-05-29 15:06:38 -07:00
|
|
|
text: formatMessage({
|
|
|
|
id: 'translate.viewerLanguage',
|
2018-07-10 15:10:27 -04:00
|
|
|
default: 'language',
|
2018-05-29 15:06:38 -07:00
|
|
|
description: 'the languge of the project viewer'
|
|
|
|
}),
|
2018-05-28 11:58:17 -07:00
|
|
|
blockType: BlockType.REPORTER,
|
|
|
|
arguments: {}
|
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
|
|
|
|
2018-09-17 14:04:32 -04:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
});
|
|
|
|
}
|
2018-05-28 11:58:17 -07:00
|
|
|
/**
|
2018-05-29 15:06:38 -07:00
|
|
|
* Get the human readable language value for the reporter block.
|
|
|
|
* @return {string} the language name of the project viewer.
|
2018-05-28 11:58:17 -07:00
|
|
|
*/
|
|
|
|
getViewerLanguage () {
|
2018-05-29 15:06:38 -07:00
|
|
|
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;
|
2018-05-28 11:58:17 -07: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;
|