将 ResourceBundle 属性 绑定到列表项

Binding ResourceBundle property to list item

在 SAPUI5 中,您通常可以通过多种方式将资源包属性绑定到项目。我正在尝试使用 Odata 服务提供的数据在 JavaScript 中执行此操作,但到目前为止我在此处找到的方法还没有奏效。我尝试了两种不同的方式:

  1. How to Use Internalization i18n in a Controller in SAPUI5?

而且这些都没有用。我觉得这是因为我在项目列表中,在导致我的问题的对话框中:

我的代码是:

var resourceBundle = this.getView().getModel("i18n");

if (!this.resizableDialog) {
    this.resizableDialog = new Dialog({
        title: 'Title',
        contentWidth: "550px",
        contentHeight: "300px",
        resizable: true,
        content: new List({
            items: {
                path: path,
                template: new StandardListItem({
                    title: resourceBundle.getProperty("{Label}"),//"{ path : 'Label', fomatter : '.getI18nValue' }",
                    description: "{Value}"
                })
            }
        }),
        beginButton: new Button({
            text: 'Close',
            press: function () {
                this.resizableDialog.close();
            }.bind(this)
        })
    });

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

使用上面的第二种方法,从不调用JavaScript方法。我不认为它会起作用,因为它更基于 JSON。第一种方法可以很好地加载数据,但它不显示资源包文本,而是只显示在 {Label} 值中找到的文本。

{Label} 中找到的值与在资源包中找到的键匹配,即前面没有 i18n>,就像在视图中一样。

有人有什么建议吗?

使用 formatter 可以解决您的问题,但是您的操作方式不对。试试这个,它会解决你的问题。

var resourceBundle = this.getView().getModel("i18n");
if (!this.resizableDialog) {
    this.resizableDialog = new Dialog({
        title: 'Title',
        contentWidth: "550px",
        contentHeight: "300px",
        resizable: true,
        content: new List({
            items: {
                path: path,
                template: new StandardListItem({
                   title: {
                       parts: [{ path: "Label" }],
                       formatter: this.getI18nValue.bind(this)
                   },
                   description: "{Value}"
                })
            }
        }),
        beginButton: new Button({
            text: 'Close',
            press: function () {
                this.resizableDialog.close();
            }.bind(this)
       })
    });
}

格式化程序函数getI18nValue会像这样,

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