4、SAP UI5 之 Step8:转移文本(高耦合)

    xiaoxiao2022-07-06  189

    Table of Contents

     

    Step8: 转移文本

    1、webapp/i18n/i18n.properties (New)

    2、controller/App.controller.js

    3、webapp/view/App.view.xml

    总结


    Step8: 转移文本

    1、webapp/i18n/i18n.properties (New)

    showHelloButtonText=Say Hello helloMsg=Hello {0}

    2、controller/App.controller.js

    sap.ui.define([ "sap/ui/core/mvc/Controller", "sap/m/MessageToast", "sap/ui/model/json/JSONModel", "sap/ui/model/resource/ResourceModel" ], function (Controller, MessageToast, JSONModel, ResourceModel) { "use strict"; return Controller.extend("sap.ui.demo.walkthrough.controller.App", { onInit : function () { // set data model on view var oData = { recipient : { name : "World" } }; var oModel = new JSONModel(oData); this.getView().setModel(oModel); // set i18n model on view var i18nModel = new ResourceModel({ bundleName: "sap.ui.demo.walkthrough.i18n.i18n" }); this.getView().setModel(i18nModel, "i18n"); }, onShowHello : function () { // read msg from i18n model var oBundle = this.getView().getModel("i18n").getResourceBundle(); var sRecipient = this.getView().getModel().getProperty("/recipient/name"); var sMsg = oBundle.getText("helloMsg", [sRecipient]); // show message MessageToast.show(sMsg); } }); });

    注意:

    (1)引用i18n.properties

    sap.ui.demo.walkthrough.i18n.i18n包含命名空间 sap.ui.demo.walkthrough (the application root as defined in the index.html), the folder name i18n and finally the file name i18n without extension.

     

    (2)onShowHello 是点击事件

    (3)在App.controller.js中的recipient/name的值----world

    (4) helloMsg的值是Hello

    因此最后toast的值为hello world。

    3、webapp/view/App.view.xml

    <mvc:View controllerName="sap.ui.demo.walkthrough.controller.App" xmlns="sap.m" xmlns:mvc="sap.ui.core.mvc"> <Button text="{i18n>showHelloButtonText}" press=".onShowHello"/> <Input value="{/recipient/name}" description="Hello {/recipient/name}" valueLiveUpdate="true" width="60%"/> </mvc:View>

    标黄的是取i8n的showHelloButtonText

    总结

    The resource model for internationalization is called the i18n model.

    The default filename is i18n.properties.

    Resource bundle keys are written in (lower) camelCase.

    Resource bundle values can contain parameters like {0}, {1}, {2}, …

    Never concatenate strings that are translated, always use placeholders.

    Use Unicode escape sequences for special characters.

    最新回复(0)