Javascript 如何使用JSON文件和XML视图设置本地化数据绑定?

Javascript 如何使用JSON文件和XML视图设置本地化数据绑定?,javascript,sapui5,Javascript,Sapui5,我有一个包含一些互动程序的XMLView主页。这些分幅是从JSON文件填充的。磁贴具有需要i18n数据绑定的“title”属性 XML视图的一部分: <TileContainer id="container" tiles="{/TileCollection}"> <StandardTile icon="{icon}" title="{title}" press="onPress" /> </TileContainer> 我完成数据绑定的

我有一个包含一些互动程序的XMLView主页。这些分幅是从JSON文件填充的。磁贴具有需要i18n数据绑定的“title”属性

XML视图的一部分:

<TileContainer id="container" tiles="{/TileCollection}">
  <StandardTile
   icon="{icon}"
   title="{title}"
   press="onPress" />
</TileContainer>
我完成数据绑定的旧方法是直接在视图中使用
title=“{i18n>foo}”
。当然,现在我基本上有两层数据绑定,一层在JSON中用于i18n,另一层在视图中用于获取JSON(获取i18n)

这也是我设置i18n模型的Component.js

sap.ui.core.UIComponent.extend("MYAPP.Component", {
  metadata: {
    rootView : "MYAPP.view.Home", //points to the default view

    config: {
      resourceBundle: "i18n/messageBundle.properties"
    },
    ... etc


  init: function(){
    sap.ui.core.UIComponent.prototype.init.apply(this, arguments);
    var mConfig = this.getMetadata().getConfig();

    var oRouter = this.getRouter();
    this.RouteHandler = new sap.m.routing.RouteMatchedHandler(oRouter);
    oRouter.register("router");
    oRouter.initialize();

    var sRootPath = jQuery.sap.getModulePath("MYAPP");
    var i18nModel = new sap.ui.model.resource.ResourceModel({
        bundleUrl : [sRootPath, mConfig.resourceBundle].join("/")
    });
    this.setModel(i18nModel, "i18n");
}

这个问题源于对另一个问题的讨论,因此可能会有更多信息供感兴趣的人参考

我通常采用的方法是使用格式化程序函数,其唯一目的是为某个键(在资源模型中维护,并由数据模型驱动)获取正确的本地化值

例如,互动程序UI的外观如下所示:

<TileContainer id="container" tiles="{/tiles}">
    <StandardTile
      icon="{icon}"
      type="{type}"
      title="{ path : 'title', formatter : '.getI18nValue' }"
      info="{ path : 'info', formatter : '.getI18nValue' }"
      infoState="{infoState}" 
      press="handlePress"/>
</TileContainer>
tiles : [
    {
        icon      : "sap-icon://inbox",
        number    : "12",
        title     : "inbox",   // i18n property 'inbox'
        info      : "overdue", // i18n property 'overdue'
        infoState : "Error"
    },
    {
        icon      : "sap-icon://calendar",
        number    : "3",
        title     : "calendar", // i18n property 'calendar'
        info      : "planned",  // i18n property 'planned'
        infoState : "Success"
    }
]
JSONModel的
标题
信息
属性值(例如,“收件箱”和“过期”)与resourcebundle文件(以及ResourceModel)中的一个键相对应

控制器中的格式化程序函数(或者更好,在独立的JS文件中,用于在多个视图中重用)非常简单:

getI18nValue : function(sKey) {
    return this.getView().getModel("i18n").getProperty(sKey);
}
它只是从模型(例如,“收件箱”)提供值,然后从资源模型返回该键的本地化值

getI18nValue : function(sKey) {
    return this.getView().getModel("i18n").getProperty(sKey);
}