Blockly supports block definitions that can be localized to the user's language.Through the use of string tables, message strings in the JSON block definitioncan adapt the inputs, fields, and labels to reflect the language's vocabulary,word ordering, and direction.
All of these cases share the same JSON block definition:
// Block for creating a list with one element repeated.
{
"type": "lists_repeat",
"message0": "%{BKY_LISTS_REPEAT_TITLE}",
"args0": [
{
"type": "input_value",
"name": "ITEM"
},
{
"type": "input_value",
"name": "NUM",
"check": "Number"
}
],
"output": "Array",
"colour": "%{BKY_LISTS_HUE}",
"tooltip": "%{BKY_LISTS_REPEAT_TOOLTIP}",
"helpUrl": "%{BKY_LISTS_REPEAT_HELPURL}"
}
The lists_repeat
example includes several "%{BKY_...}"
strings. Each ofthese is a reference to a string in the Blockly.Msg
string table. When theblock is instantiated, Blockly attempts to replace the string with the value.
For example, %{BKY_LISTS_REPEAT_TITLE}
is replaced with the value ofBlockly.Msg['LISTS_REPEAT_TITLE']
, if it exists. If the value does not exist,the %{BKY_...}
notation is left in place, and Blockly emits a warningfor the missing translation.
As shown in the example, the notation works in several locations. The messagesand tooltips allow user visible strings to be replaced. Similarly, dropdownfields can also use the notation for item text. Help URL can be localized toensure the user is directed to a similarly localized page. And finally, thecolour
value can use the notation to help centralize the palette of blocks.
If you're using the JavaScript implementation and do not expect to change theuser language at runtime, you may find it easier to use a direct reference. Forexample, either Blockly.Msg['LISTS_REPEAT_TITLE']
orBlockly.Msg.LISTS_REPEAT_TITLE'
. Since this is not valid JSON, this syntax(referencing a variable declared elsewhere) does not work for Android or iOS
The string tables are loaded via any one of the language specific .js
files inmsg/js
. Load theappropriate file into your web page to load the correct block translation.Blockly for iOS operates slightly differently, automatically selecting thecorrect JSON file from thelocalized messages.
The message0
attribute (and message1
, message2
, etc.) dictate the inputs,fields, and surrounding label text. For LISTS_REPEAT_TITLE
in thelists_repeat
block, the English value is:
Blockly.Msg.LISTS_REPEAT_TITLE = 'create list with item %1 repeated %2 times';
Two interpolation markers, %1
and %2
, mark the locations of the two inputs.Further details are provided in the args0
array. args1
would corespond tothe message1
string. See theCreate custom blocksguide for more details about specifying block inputs and fields.
The text between the interpolation markers is converted into unnamed/blockly/reference/js/Blockly.FieldLabel. This creates the wordordering of the block and how it reads:
// In Spanish
Blockly.Msg.LISTS_REPEAT_TITLE = "crear lista con el elemento %1 repetido %2 veces";
// In Korean
Blockly.Msg.LISTS_REPEAT_TITLE = "%1을 %2번 넣어, 리스트 생성";
When translating to right-to-left languages, the message string is written inthe visual order, and should not include Unicode direction commands:
// In Arabic. Note how %2 is left of %1, since it read right to left.
Blockly.Msg.LISTS_REPEAT_TITLE = "إنشئ قائمة مع العنصر %1 %2 مرات";
Building the string table .js
files is part of the build.py
script. Thescript takes the identifier keys and English translations frommsg/messages.js
to create a new en.json
.Then, combined with and any translations found inthe other JSON files,it will recreate updatedJavaScript string tablesfor all languages, including a newen.js
.
The non-English JSON files come from partners atTranslateWikie. See thenotes ontranslatingfor details about how you can help.
联系客服