mirror of
https://github.com/scratchfoundation/scratch-blocks.git
synced 2025-08-28 22:10:31 -04:00
Add function to support translation of dynamic menus
Dynamic menus are constructed in the VM. Add a function to ScratchMsgs that it can use to look up localized strings for jsonInit. Add missing strings for dynamic menus.
This commit is contained in:
parent
7abd74c6f4
commit
eca1eec39c
4 changed files with 58 additions and 1 deletions
|
@ -38,17 +38,47 @@ goog.require('Blockly.Msg');
|
|||
* @type {Object}
|
||||
*/
|
||||
Blockly.ScratchMsgs.locales = {};
|
||||
|
||||
/**
|
||||
* The current locale.
|
||||
* @type {String}
|
||||
* @private
|
||||
*/
|
||||
Blockly.ScratchMsgs.currentLocale_ = 'en';
|
||||
|
||||
/**
|
||||
* Change the Blockly.Msg strings to a new Locale
|
||||
* Does not exist in Blockly, but needed in scratch-blocks
|
||||
* @param {string} locale E.g., 'de', or 'zh-tw'
|
||||
* @package
|
||||
*/
|
||||
Blockly.ScratchMsgs.setLocale = function(locale) {
|
||||
if (Object.keys(Blockly.ScratchMsgs.locales).includes(locale)) {
|
||||
Blockly.ScratchMsgs.currentLocale_ = locale;
|
||||
Blockly.Msg = Blockly.ScratchMsgs.locales[locale];
|
||||
} else {
|
||||
// keep current locale
|
||||
console.warn('Ignoring unrecognized locale: ' + locale);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets a localized message, for use in the Scratch VM with json init.
|
||||
* Does not interpolate placeholders. Provided to allow default values in
|
||||
* dynamic menus, for example, 'next backdrop', or 'random position'
|
||||
* @param {string} msgId id for the message, key in Msg table.
|
||||
* @param {string} defaultMsg string to use if the id isn't found.
|
||||
* @param {string} useLocale optional locale to use in place of currentLocale_.
|
||||
* @return {string} message with placeholders filled.
|
||||
* @suppress {duplicate}
|
||||
*/
|
||||
Blockly.ScratchMsgs.translate = function(msgId, defaultMsg, useLocale) {
|
||||
var locale = useLocale || Blockly.ScratchMsgs.currentLocale_;
|
||||
|
||||
if (Object.keys(Blockly.ScratchMsgs.locales).includes(locale)) {
|
||||
var messages = Blockly.ScratchMsgs.locales[locale];
|
||||
if (Object.keys(messages).includes(msgId)) {
|
||||
return messages[msgId];
|
||||
}
|
||||
}
|
||||
return defaultMsg;
|
||||
};
|
||||
|
|
|
@ -61,8 +61,10 @@ Blockly.Msg["EVENT_WHENKEYPRESSED_UP"] = "up arrow";
|
|||
Blockly.Msg["EVENT_WHENKEYPRESSED_ANY"] = "any";
|
||||
Blockly.Msg["LOOKS_SAYFORSECS"] = "say %1 for %2 seconds";
|
||||
Blockly.Msg["LOOKS_SAY"] = "say %1";
|
||||
Blockly.Msg["LOOKS_HELLO"] = "Hello!";
|
||||
Blockly.Msg["LOOKS_THINKFORSECS"] = "think %1 for %2 seconds";
|
||||
Blockly.Msg["LOOKS_THINK"] = "think %1";
|
||||
Blockly.Msg["LOOKS_HMM"] = "Hmm...";
|
||||
Blockly.Msg["LOOKS_SHOW"] = "show";
|
||||
Blockly.Msg["LOOKS_HIDE"] = "hide";
|
||||
Blockly.Msg["LOOKS_HIDEALLSPRITES"] = "hide all sprites";
|
||||
|
@ -97,6 +99,7 @@ Blockly.Msg["LOOKS_NUMBERNAME_NAME"] = "name";
|
|||
Blockly.Msg["LOOKS_SWITCHBACKDROPTOANDWAIT"] = "switch backdrop to %1 and wait";
|
||||
Blockly.Msg["LOOKS_NEXTBACKDROP"] = "next backdrop";
|
||||
Blockly.Msg["LOOKS_PREVIOUSBACKDROP"] = "previous backdrop";
|
||||
Blockly.Msg["LOOKS_RANDOMBACKDROP"] = "random backdrop";
|
||||
Blockly.Msg["MOTION_MOVESTEPS"] = "move %1 steps";
|
||||
Blockly.Msg["MOTION_TURNLEFT"] = "turn %1 %2 degrees";
|
||||
Blockly.Msg["MOTION_TURNRIGHT"] = "turn %1 %2 degrees";
|
||||
|
@ -133,6 +136,7 @@ Blockly.Msg["MOTION_ALIGNSCENE_TOPLEFT"] = "top-left";
|
|||
Blockly.Msg["MOTION_ALIGNSCENE_TOPRIGHT"] = "top-right";
|
||||
Blockly.Msg["MOTION_XSCROLL"] = "x scroll";
|
||||
Blockly.Msg["MOTION_YSCROLL"] = "y scroll";
|
||||
Blockly.Msg["MOTION_STAGE_SELECTED"] = "Stage selected: no motion blocks";
|
||||
Blockly.Msg["OPERATORS_ADD"] = "%1 + %2";
|
||||
Blockly.Msg["OPERATORS_SUBTRACT"] = "%1 - %2";
|
||||
Blockly.Msg["OPERATORS_MULTIPLY"] = "%1 * %2";
|
||||
|
@ -145,7 +149,10 @@ Blockly.Msg["OPERATORS_AND"] = "%1 and %2";
|
|||
Blockly.Msg["OPERATORS_OR"] = "%1 or %2";
|
||||
Blockly.Msg["OPERATORS_NOT"] = "not %1";
|
||||
Blockly.Msg["OPERATORS_JOIN"] = "join %1 %2";
|
||||
Blockly.Msg["OPERATORS_JOIN_APPLE"] = "apple";
|
||||
Blockly.Msg["OPERATORS_JOIN_BANANA"] = "banana";
|
||||
Blockly.Msg["OPERATORS_LETTEROF"] = "letter %1 of %2";
|
||||
Blockly.Msg["OPERATORS_LETTEROF_APPLE"] = "a";
|
||||
Blockly.Msg["OPERATORS_LENGTH"] = "length of %1";
|
||||
Blockly.Msg["OPERATORS_CONTAINS"] = "%1 contains %2?";
|
||||
Blockly.Msg["OPERATORS_MOD"] = "%1 mod %2";
|
||||
|
@ -174,6 +181,7 @@ Blockly.Msg["SENSING_COLORISTOUCHINGCOLOR"] = "color %1 is touching %2?";
|
|||
Blockly.Msg["SENSING_DISTANCETO"] = "distance to %1";
|
||||
Blockly.Msg["SENSING_DISTANCETO_POINTER"] = "mouse-pointer";
|
||||
Blockly.Msg["SENSING_ASKANDWAIT"] = "ask %1 and wait";
|
||||
Blockly.Msg["SENSING_ASK_TEXT"] = "What's your name?";
|
||||
Blockly.Msg["SENSING_ANSWER"] = "answer";
|
||||
Blockly.Msg["SENSING_KEYPRESSED"] = "key %1 pressed?";
|
||||
Blockly.Msg["SENSING_MOUSEDOWN"] = "mouse down?";
|
||||
|
@ -196,6 +204,7 @@ Blockly.Msg["SENSING_OF_SIZE"] = "size";
|
|||
Blockly.Msg["SENSING_OF_VOLUME"] = "volume";
|
||||
Blockly.Msg["SENSING_OF_BACKDROPNUMBER"] = "backdrop #";
|
||||
Blockly.Msg["SENSING_OF_BACKDROPNAME"] = "backdrop name";
|
||||
Blockly.Msg["SENSING_OF_STAGE"] = "Stage";
|
||||
Blockly.Msg["SENSING_CURRENT"] = "current %1";
|
||||
Blockly.Msg["SENSING_CURRENT_YEAR"] = "year";
|
||||
Blockly.Msg["SENSING_CURRENT_MONTH"] = "month";
|
||||
|
|
|
@ -55,8 +55,10 @@
|
|||
"EVENT_WHENKEYPRESSED_ANY": "any",
|
||||
"LOOKS_SAYFORSECS": "say %1 for %2 seconds",
|
||||
"LOOKS_SAY": "say %1",
|
||||
"LOOKS_HELLO": "Hello!",
|
||||
"LOOKS_THINKFORSECS": "think %1 for %2 seconds",
|
||||
"LOOKS_THINK": "think %1",
|
||||
"LOOKS_HMM": "Hmm...",
|
||||
"LOOKS_SHOW": "show",
|
||||
"LOOKS_HIDE": "hide",
|
||||
"LOOKS_HIDEALLSPRITES": "hide all sprites",
|
||||
|
@ -91,6 +93,7 @@
|
|||
"LOOKS_SWITCHBACKDROPTOANDWAIT": "switch backdrop to %1 and wait",
|
||||
"LOOKS_NEXTBACKDROP": "next backdrop",
|
||||
"LOOKS_PREVIOUSBACKDROP": "previous backdrop",
|
||||
"LOOKS_RANDOMBACKDROP": "random backdrop",
|
||||
"MOTION_MOVESTEPS": "move %1 steps",
|
||||
"MOTION_TURNLEFT": "turn %1 %2 degrees",
|
||||
"MOTION_TURNRIGHT": "turn %1 %2 degrees",
|
||||
|
@ -127,6 +130,7 @@
|
|||
"MOTION_ALIGNSCENE_TOPRIGHT": "top-right",
|
||||
"MOTION_XSCROLL": "x scroll",
|
||||
"MOTION_YSCROLL": "y scroll",
|
||||
"MOTION_STAGE_SELECTED": "Stage selected: no motion blocks",
|
||||
"OPERATORS_ADD": "%1 + %2",
|
||||
"OPERATORS_SUBTRACT": "%1 - %2",
|
||||
"OPERATORS_MULTIPLY": "%1 * %2",
|
||||
|
@ -139,7 +143,10 @@
|
|||
"OPERATORS_OR": "%1 or %2",
|
||||
"OPERATORS_NOT": "not %1",
|
||||
"OPERATORS_JOIN": "join %1 %2",
|
||||
"OPERATORS_JOIN_APPLE": "apple",
|
||||
"OPERATORS_JOIN_BANANA": "banana",
|
||||
"OPERATORS_LETTEROF": "letter %1 of %2",
|
||||
"OPERATORS_LETTEROF_APPLE": "a",
|
||||
"OPERATORS_LENGTH": "length of %1",
|
||||
"OPERATORS_CONTAINS": "%1 contains %2?",
|
||||
"OPERATORS_MOD": "%1 mod %2",
|
||||
|
@ -168,6 +175,7 @@
|
|||
"SENSING_DISTANCETO": "distance to %1",
|
||||
"SENSING_DISTANCETO_POINTER": "mouse-pointer",
|
||||
"SENSING_ASKANDWAIT": "ask %1 and wait",
|
||||
"SENSING_ASK_TEXT": "What's your name?",
|
||||
"SENSING_ANSWER": "answer",
|
||||
"SENSING_KEYPRESSED": "key %1 pressed?",
|
||||
"SENSING_MOUSEDOWN": "mouse down?",
|
||||
|
@ -190,6 +198,7 @@
|
|||
"SENSING_OF_VOLUME": "volume",
|
||||
"SENSING_OF_BACKDROPNUMBER": "backdrop #",
|
||||
"SENSING_OF_BACKDROPNAME": "backdrop name",
|
||||
"SENSING_OF_STAGE": "Stage",
|
||||
"SENSING_CURRENT": "current %1",
|
||||
"SENSING_CURRENT_YEAR": "year",
|
||||
"SENSING_CURRENT_MONTH": "month",
|
||||
|
|
|
@ -96,8 +96,10 @@ Blockly.Msg.EVENT_WHENKEYPRESSED_ANY = 'any';
|
|||
// Looks blocks
|
||||
Blockly.Msg.LOOKS_SAYFORSECS = 'say %1 for %2 seconds';
|
||||
Blockly.Msg.LOOKS_SAY = 'say %1';
|
||||
Blockly.Msg.LOOKS_HELLO = 'Hello!';
|
||||
Blockly.Msg.LOOKS_THINKFORSECS = 'think %1 for %2 seconds';
|
||||
Blockly.Msg.LOOKS_THINK = 'think %1';
|
||||
Blockly.Msg.LOOKS_HMM = 'Hmm...';
|
||||
Blockly.Msg.LOOKS_SHOW = 'show';
|
||||
Blockly.Msg.LOOKS_HIDE = 'hide';
|
||||
Blockly.Msg.LOOKS_HIDEALLSPRITES = 'hide all sprites';
|
||||
|
@ -132,6 +134,7 @@ Blockly.Msg.LOOKS_NUMBERNAME_NAME = 'name';
|
|||
Blockly.Msg.LOOKS_SWITCHBACKDROPTOANDWAIT = 'switch backdrop to %1 and wait';
|
||||
Blockly.Msg.LOOKS_NEXTBACKDROP = 'next backdrop';
|
||||
Blockly.Msg.LOOKS_PREVIOUSBACKDROP = 'previous backdrop';
|
||||
Blockly.Msg.LOOKS_RANDOMBACKDROP = 'random backdrop';
|
||||
|
||||
// Motion blocks
|
||||
Blockly.Msg.MOTION_MOVESTEPS = 'move %1 steps';
|
||||
|
@ -170,6 +173,7 @@ Blockly.Msg.MOTION_ALIGNSCENE_TOPLEFT = 'top-left';
|
|||
Blockly.Msg.MOTION_ALIGNSCENE_TOPRIGHT = 'top-right';
|
||||
Blockly.Msg.MOTION_XSCROLL = 'x scroll';
|
||||
Blockly.Msg.MOTION_YSCROLL = 'y scroll';
|
||||
Blockly.Msg.MOTION_STAGE_SELECTED = 'Stage selected: no motion blocks';
|
||||
|
||||
// Operators blocks
|
||||
Blockly.Msg.OPERATORS_ADD = '%1 + %2';
|
||||
|
@ -184,7 +188,10 @@ Blockly.Msg.OPERATORS_AND = '%1 and %2';
|
|||
Blockly.Msg.OPERATORS_OR = '%1 or %2';
|
||||
Blockly.Msg.OPERATORS_NOT = 'not %1';
|
||||
Blockly.Msg.OPERATORS_JOIN = 'join %1 %2';
|
||||
Blockly.Msg.OPERATORS_JOIN_APPLE = 'apple';
|
||||
Blockly.Msg.OPERATORS_JOIN_BANANA = 'banana';
|
||||
Blockly.Msg.OPERATORS_LETTEROF = 'letter %1 of %2';
|
||||
Blockly.Msg.OPERATORS_LETTEROF_APPLE = 'a';
|
||||
Blockly.Msg.OPERATORS_LENGTH = 'length of %1';
|
||||
Blockly.Msg.OPERATORS_CONTAINS = '%1 contains %2?';
|
||||
Blockly.Msg.OPERATORS_MOD = '%1 mod %2';
|
||||
|
@ -217,6 +224,7 @@ Blockly.Msg.SENSING_COLORISTOUCHINGCOLOR = 'color %1 is touching %2?';
|
|||
Blockly.Msg.SENSING_DISTANCETO = 'distance to %1';
|
||||
Blockly.Msg.SENSING_DISTANCETO_POINTER = 'mouse-pointer';
|
||||
Blockly.Msg.SENSING_ASKANDWAIT = 'ask %1 and wait';
|
||||
Blockly.Msg.SENSING_ASK_TEXT = 'What\'s your name?';
|
||||
Blockly.Msg.SENSING_ANSWER = 'answer';
|
||||
Blockly.Msg.SENSING_KEYPRESSED = 'key %1 pressed?';
|
||||
Blockly.Msg.SENSING_MOUSEDOWN = 'mouse down?';
|
||||
|
@ -239,6 +247,7 @@ Blockly.Msg.SENSING_OF_SIZE = 'size';
|
|||
Blockly.Msg.SENSING_OF_VOLUME = 'volume';
|
||||
Blockly.Msg.SENSING_OF_BACKDROPNUMBER = 'backdrop #';
|
||||
Blockly.Msg.SENSING_OF_BACKDROPNAME = 'backdrop name';
|
||||
Blockly.Msg.SENSING_OF_STAGE = 'Stage';
|
||||
Blockly.Msg.SENSING_CURRENT = 'current %1';
|
||||
Blockly.Msg.SENSING_CURRENT_YEAR = 'year';
|
||||
Blockly.Msg.SENSING_CURRENT_MONTH = 'month';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue