SAPUI5 Walkthrough Step 8: Translatable Texts
阿新 • • 發佈:2021-07-13
https://sapui5.hana.ondemand.com/#/topic/df86bfbeab0645e5b764ffa488ed57dc
在這一步中,我們將文字移動到一個指定的資原始檔中。這樣將便於我們管理和翻譯。
增加i18n檔案 webapp/i18n/i18n.properties,內容如下
showHelloButtonText=說點啥
helloMsg=說 {0}
修改控制器controll檔案 webapp/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){ return Controller.extend("zdemo_step1.controller.App",{ onInit: function(){ var oJsonData = { recipient : { name :"World" } }; var oJSONModel = new JSONModel(oJsonData); this.getView().setModel(oJSONModel, "jsonModel"); //設定模型i18n var i18nModel = new ResourceModel({ bundleName: "zdemo_step1.i18n.i18n" });this.getView().setModel(i18nModel, "i18n"); }, onShowHello: function(){ var oBundle = this.getView().getModel("i18n").getResourceBundle(); var sRecipient = this.getView().getModel("jsonModel").getProperty("/recipient/name"); var sMsg = oBundle.getText("helloMsg", [sRecipient]); MessageToast.show(sMsg); } }); });
增加 ResourceModel 模組的引用,
在onInit中,增加模型i18n(型別為ResourceModel)。
onShowHello中,讀取模型i18n中的內容,並替換成我們想要的內容
修改檢視檔案 webapp/view/App.view.xml,
<mvc:View controllerName="zdemo_step1.controller.App" xmlns="sap.m" xmlns:mvc="sap.ui.core.mvc"> <Button text="{i18n>showHelloButtonText}" press=".onShowHello" /> <Input value="{jsonModel>/recipient/name}" description="Hello {jsonModel>/recipient/name}" valueLiveUpdate="true" width="50%" /> </mvc:View>
按鈕的text屬性讀取模型i18n。
執行