如何在 XML 视图中使用带有 i18n 模型和参数的 "formatMessage" 模块
How to use "formatMessage" module with i18n model and parameters in XML views
在 UI5 演示工具包中,Walkthrough step 8(仍然)提到以下内容:
To be on the safe side, we would have to use a similar mechanism as in the controller to use a string from the resource bundle and replace parts of it. This can be done with the jQuery.sap.formatMessage
formatter.
它可用于 XML 视图中带有占位符的可翻译文本,例如:
<Title text="{
parts: [
'i18n>overflowToolbarTitle',
'appView>/listItemCount'
],
formatter: 'jQuery.sap.formatMessage'
}" />
但是,jQuery.sap.formatMessage
自 1.58 以来已贬值,如果我改用建议的替代方案 sap.base.strings.formatMessage
,则会抛出以下错误:
formatter function sap.base.strings.formatMessage not found!
如何在XML中使用新的formatMessage
模块?
UI5 1.69+
与提交:<a href="https://github.com/SAP/openui5/commit/2dab48e8c3604099f7d2bd56741f5635d3357639" rel="noreferrer">2dab48e</a>
, it is now possible to require modules declaratively in XML views:
<Title xmlns="sap.m" xmlns:core="sap.ui.core"
core:require="{ formatMessage: 'sap/base/strings/formatMessage' }"
text="{
parts: [
'i18n>overflowToolbarTitle',
'appView>/listItemCount'
],
formatter: 'formatMessage'
}"
/>
这样,我们就可以避免对中间控制器的依赖,并独立使用新模块。
UI5 1.68 及以下(上一个答案)
UI5 当前不允许获取模块并将其分配给 formatter
。1 此外,新功能模块永远不会以名称 [=14] 导出=].src
一种替代方法是指向控制器中分配的方法,而该方法指向所需的 formatMessage
模块。
<Title text="{
parts: [
'i18n>overflowToolbarTitle',
'appView>/listItemCount'
],
formatter: '.formatMessage'
}"/>
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/base/strings/formatMessage",
// ...
], function(Controller, formatMessage/*, ...*/) {
"use strict";
return Controller.extend("...", {
formatMessage: formatMessage,
// ...
});
})
我可以想象 UI5 在以后的版本中支持在绑定定义中获取和分配模块,但在撰写本文时这是不可能的。
在 UI5 演示工具包中,Walkthrough step 8(仍然)提到以下内容:
To be on the safe side, we would have to use a similar mechanism as in the controller to use a string from the resource bundle and replace parts of it. This can be done with the
jQuery.sap.formatMessage
formatter.
它可用于 XML 视图中带有占位符的可翻译文本,例如:
<Title text="{
parts: [
'i18n>overflowToolbarTitle',
'appView>/listItemCount'
],
formatter: 'jQuery.sap.formatMessage'
}" />
但是,jQuery.sap.formatMessage
自 1.58 以来已贬值,如果我改用建议的替代方案 sap.base.strings.formatMessage
,则会抛出以下错误:
formatter function sap.base.strings.formatMessage not found!
如何在XML中使用新的formatMessage
模块?
UI5 1.69+
与提交:<a href="https://github.com/SAP/openui5/commit/2dab48e8c3604099f7d2bd56741f5635d3357639" rel="noreferrer">2dab48e</a>
, it is now possible to require modules declaratively in XML views:
<Title xmlns="sap.m" xmlns:core="sap.ui.core"
core:require="{ formatMessage: 'sap/base/strings/formatMessage' }"
text="{
parts: [
'i18n>overflowToolbarTitle',
'appView>/listItemCount'
],
formatter: 'formatMessage'
}"
/>
这样,我们就可以避免对中间控制器的依赖,并独立使用新模块。
UI5 1.68 及以下(上一个答案)
UI5 当前不允许获取模块并将其分配给 formatter
。1 此外,新功能模块永远不会以名称 [=14] 导出=].src
一种替代方法是指向控制器中分配的方法,而该方法指向所需的 formatMessage
模块。
<Title text="{
parts: [
'i18n>overflowToolbarTitle',
'appView>/listItemCount'
],
formatter: '.formatMessage'
}"/>
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/base/strings/formatMessage",
// ...
], function(Controller, formatMessage/*, ...*/) {
"use strict";
return Controller.extend("...", {
formatMessage: formatMessage,
// ...
});
})
我可以想象 UI5 在以后的版本中支持在绑定定义中获取和分配模块,但在撰写本文时这是不可能的。