打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Localize Blocks  |  Blockly  |  Google Developers

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}"
}

String Table

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.

JSON Message Interpolation

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 مرات";

Rebuilding string tables

Building the string table .js files is part of the build.py script. Thescript takes the identifier keys and English translations frommsg/messages.jsto 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.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
蓝  颜  知  己
一天又一天 (动画好美呀) Qzone精彩由你而来
Groovy入门教程
获取json格式的参数数据
php推送例子(第三方极光推送)
我心中的玫瑰
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服