Internationalize variable name strings (#930 from AnmAtAnm/variable_i18n)

Also, .utils.replaceMessageReferences(..) now gracefully returns non-string arguments.
This commit is contained in:
Andrew n marshall
2017-02-14 11:58:02 -08:00
committed by GitHub
3 changed files with 73 additions and 27 deletions

View File

@@ -1150,13 +1150,10 @@ Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign) {
input = this.appendDummyInput(element['name']);
break;
case 'field_label':
field = new Blockly.FieldLabel(element['text'], element['class']);
field = Blockly.Block.newFieldLabelFromJson_(element);
break;
case 'field_input':
field = new Blockly.FieldTextInput(element['text']);
if (typeof element['spellcheck'] == 'boolean') {
field.setSpellcheck(element['spellcheck']);
}
field = Blockly.Block.newFieldTextInputFromJson_(element);
break;
case 'field_angle':
field = new Blockly.FieldAngle(element['angle']);
@@ -1169,14 +1166,13 @@ Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign) {
field = new Blockly.FieldColour(element['colour']);
break;
case 'field_variable':
field = new Blockly.FieldVariable(element['variable']);
field = Blockly.Block.newFieldVariableFromJson_(element);
break;
case 'field_dropdown':
field = new Blockly.FieldDropdown(element['options']);
break;
case 'field_image':
field = new Blockly.FieldImage(element['src'],
element['width'], element['height'], element['alt']);
field = Blockly.Block.newFieldImageFromJson_(element);
break;
case 'field_number':
field = new Blockly.FieldNumber(element['value'],
@@ -1215,6 +1211,64 @@ Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign) {
}
};
/**
* Helper function to construct a FieldImage from a JSON arg object,
* dereferencing any string table references.
* @param {!Object} options A JSON object with options (src, width, height, and alt).
* @returns {!Blockly.FieldImage} The new image.
* @private
*/
Blockly.Block.newFieldImageFromJson_ = function(options) {
var src = Blockly.utils.replaceMessageReferences(options['src']);
var width = Number(Blockly.utils.replaceMessageReferences(options['width']));
var height =
Number(Blockly.utils.replaceMessageReferences(options['height']));
var alt = Blockly.utils.replaceMessageReferences(options['alt']);
return new Blockly.FieldImage(src, width, height, alt);
};
/**
* Helper function to construct a FieldLabel from a JSON arg object,
* dereferencing any string table references.
* @param {!Object} options A JSON object with options (text, and class).
* @returns {!Blockly.FieldImage} The new image.
* @private
*/
Blockly.Block.newFieldLabelFromJson_ = function(options) {
var text = Blockly.utils.replaceMessageReferences(options['text']);
return new Blockly.FieldLabel(text, options['class']);
};
/**
* Helper function to construct a FieldTextInput from a JSON arg object,
* dereferencing any string table references.
* @param {!Object} options A JSON object with options (text, class, and
* spellcheck).
* @returns {!Blockly.FieldImage} The new image.
* @private
*/
Blockly.Block.newFieldTextInputFromJson_ = function(options) {
var text = Blockly.utils.replaceMessageReferences(options['text']);
var field = new Blockly.FieldTextInput(text, options['class']);
if (typeof options['spellcheck'] == 'boolean') {
field.setSpellcheck(options['spellcheck']);
}
return field;
};
/**
* Helper function to construct a FieldVariable from a JSON arg object,
* dereferencing any string table references.
* @param {!Object} options A JSON object with options (variable).
* @returns {!Blockly.FieldImage} The new image.
* @private
*/
Blockly.Block.newFieldVariableFromJson_ = function(options) {
var varname = Blockly.utils.replaceMessageReferences(options['variable']);
return new Blockly.FieldVariable(varname);
};
/**
* Add a value input, statement input or local variable to this block.
* @param {number} type Either Blockly.INPUT_VALUE or Blockly.NEXT_STATEMENT or

View File

@@ -35,8 +35,8 @@ goog.require('goog.userAgent');
/**
* Class for an image on a block.
* @param {string} src The URL of the image.
* @param {number|string} width Width of the image, possibly via reference.
* @param {number|string} height Height of the image, possibly via reference.
* @param {number} width Width of the image.
* @param {number} height Height of the image.
* @param {string=} opt_alt Optional alt text for when block is collapsed.
* @extends {Blockly.Field}
* @constructor
@@ -44,18 +44,6 @@ goog.require('goog.userAgent');
Blockly.FieldImage = function(src, width, height, opt_alt) {
this.sourceBlock_ = null;
// Replace any message references in the arguments.
src = Blockly.utils.replaceMessageReferences(src);
if (goog.isString(height)) {
height = Blockly.utils.replaceMessageReferences(height);
}
if (goog.isString(width)) {
width = Blockly.utils.replaceMessageReferences(width);
}
if (goog.isString(opt_alt)) {
opt_alt = Blockly.utils.replaceMessageReferences(opt_alt);
}
// Ensure height and width are numbers. Strings are bad at math.
this.height_ = Number(height);
this.width_ = Number(width);

View File

@@ -259,7 +259,7 @@ Blockly.utils.getRelativeXY.XY_2D_REGEX_ =
* context (scale...).
* @return {!SVGElement} Newly created SVG element.
*/
Blockly.utils.createSvgElement = function(name, attrs, parent, opt_workspace) {
Blockly.utils.createSvgElement = function(name, attrs, parent /*, opt_workspace */) {
var e = /** @type {!SVGElement} */ (
document.createElementNS(Blockly.SVG_NS, name));
for (var key in attrs) {
@@ -409,13 +409,17 @@ Blockly.utils.tokenizeInterpolation = function(message) {
};
/**
* Replaces string table references in a message string. For example,
* %{bky_my_msg} and %{BKY_MY_MSG} will both be replaced with the value in
* Blockly.Msg['MY_MSG'].
* @param {string} message Text which might contain string table references.
* Replaces string table references in a message, if the message is a string.
* For example, "%{bky_my_msg}" and "%{BKY_MY_MSG}" will both be replaced with
* the value in Blockly.Msg['MY_MSG'].
* @param {string|?} message Message, which may be a string that contains
* string table references.
* @return {!string} String with message references replaced.
*/
Blockly.utils.replaceMessageReferences = function(message) {
if (!goog.isString(message)) {
return message;
}
var interpolatedResult = Blockly.utils.tokenizeInterpolation_(message, false);
// When parseInterpolationTokens == false, interpolatedResult should be at
// most length 1.