|
处于多语言考虑,控件名称是需要根据语言不同进行翻译的,因此在UI5中一般会把这些文本统一放在一个文件中,i18n.properties.
今天尝试下绑定这个文件,以下所有代码全部在Display.view.js中
1 createContent : function(oController) {
2 var oModel = new sap.ui.model.resource.ResourceModel(
3 {
4 bundleName: "i18n",
5 bundleLocale: "zh",
6 }
7 );
8 var myBundle = oModel.getResourceBundle();
9
10 var oButton = new sap.ui.commons.Button({
11 id : "B1", // sap.ui.core.ID
12 //text : "TTTTTT",
13 text : myBundle.getText("BUTTON_NAME") // string
14 });
15 return oButton;
16 }
这里声明的oModel是一个ResourceModel,bundleName对应文件名,bundleLocale对应语言,
UI5核心库会自动根据这两个参数去寻找路径为/resourece/i18n_zn.properties的文件
在这个文件中 新建一个文本:
BUTTON_NAME=MY BUTTON
当使用oModel.getResourceBundle().getText("BUTTON_NAME")的时候,会自动去这个文件中获取对应的字符串,
这边为MY BUTTON.
效果如下:
这边的文件路径默认为/resource, 有点不太灵活,查看了UI5的API,发现有另外一个参数可以设定
bundleUrl: 这边填写的路径是相对于整个项目的路径
在WebContent下建立一个i18n文件夹,并且放入刚才的i18n_zh.properties文件
ResourceModel如下定义:
1 var oModel = new sap.ui.model.resource.ResourceModel(
2 {
3 bundleUrl: "./i18n/i18n.properties",
4 bundleLocale: "zh",
5 }
6 );
测试,成功。
|
|
|