没有参数。
<Label text="{i18n>myKey}"/>
这个代码可以工作,但我们如何在表达式中传递参数呢?
到目前为止,我找到的唯一信息是http://scn.sap.com/thread/3586754。我真的希望这不是正确的方法,因为这看起来更像是一个(丑陋的)hack。
<Label text="{i18n>myKey}"/>
这个代码可以工作,但我们如何在表达式中传递参数呢?
到目前为止,我找到的唯一信息是http://scn.sap.com/thread/3586754。我真的希望这不是正确的方法,因为这看起来更像是一个(丑陋的)hack。
关键是要像这样使用格式化程序jQuery.sap.formatMessage
<Label text="{parts:['i18n>myKey', 'someModel>/someProperty'],
formatter: 'jQuery.sap.formatMessage'}"/>
这将从模型someModel
中取出值为/someProperty
的内容,并将其放入i18n资源包的myKey
中。
编辑 2020-05-19:
jQuery.sap.formatMessage
已于UI5版本1.58中弃用,请使用sap/base/strings/formatMessage
。有关用法说明,请参见此答案。
目前这是不可能的。但是你可以使用这个简单的解决方法,对我有效。
首先,在我们的 Component.js 中创建一个通用的 i18n 处理程序。我们还创建了一个 JSONModel,进行简单修改,以便立即返回请求的路径。
sap.ui.define([
"sap/ui/core/UIComponent",
"sap/ui/model/json/JSONModel"
], function(UIComponent, JSONModel) {
"use strict";
return UIComponent.extend("your namespace", {
/**
* Add a simple "StringReturnModel" to the components' models
*/
init: function() {
// [...] your other code in the init method
// String Return Model
var stringModel = new JSONModel({});
stringModel.getProperty = function(sPath) {
return sPath;
};
this.setModel(stringModel, "string");
},
/**
* Reads out a string from our text domain.
* The model i18n is defined in your manifest.json
*
* @param param text parameter
* @param arr array for parameters
* @return string
*/
i18n: function(param, arr) {
var oBundle = this.getModel("i18n").getResourceBundle();
return oBundle.getText(param, arr);
},
});
});
现在,已经存在一个带有上下文{string>}的模型。要在XML视图中使用i18n函数,我们需要创建一个格式化函数。该函数解析绑定的部分并返回本地化字符串。
sap.ui.define([
], function() {
"use strict";
var formatter = {
/**
* First argument must be the property key. The other
* one are the parameters. If there are no parameters, the
* function returns an empty string.
*
* @return string The localized text
*/
i18n: function() {
var args = [].slice.call(arguments);
if (args.length > 1) {
var key = args.shift();
// Get the component and execute the i18n function
return this.getOwnerComponent().i18n(key, args);
}
return "";
}
};
return formatter;
});
与字符串模型一起,您可以使用格式化程序将参数传递给您的i18n:
<Text text="{ parts: ['string>yourProperty', 'string/yourFirstParamter', 'anotherModel/yourSecondParamter'], formatter: '.model.formatter.i18n' }" />
你可以传递任意数量的参数,但是请确保第一个“部分”是属性键。
<Label text="{i18n>myKey} Whatever"/>
or
<Label text="{i18n>myKey1} {i18n>myKey2}"/>
<Text text="5 {i18n>hours}"/>
渲染为"5 {i18n>hours}"。 - z00bscreate file formatter.js
sap.ui.define([
"sap/base/strings/formatMessage"
], function (formatMessage) {
"use strict";
return {
formatMessage: formatMessage
};
});
View
<headerContent>
<m:MessageStrip
text="{
parts: [
'i18n>systemSettingsLastLoginTitle',
'view>/currentUser',
'view>/lastLogin'
],
formatter: '.formatter.formatMessage'
}"
type="Information"
showIcon="true">
</m:MessageStrip>
</headerContent>
控制器
var oBundle = this.getModel("i18n").getResourceBundle();
MessageToast.show(this.formatter.formatMessage(oBundle.getText("systemSettingsLastLoginTitle"), "sInfo1", "sInfo2"));
i18n
systemSettingsLastLoginTitle=You are logged in as: {0}\nLast Login: {1}
myOwnFormatter : function(someParameter)
{
/* 在此函数中接收'someParameter' */
var i18n = this.i18nModel; /* 你可以在这里访问i18n模型 */
var sCompleteText = someParameter + " " + i18n.getText("myKey")
/* 根据需要拼接字符串 */
}
对于传递多个参数,请使用:
<Label text="{parts:[{path : 'parameter1'}, {path :'parameter2'}], formatter : '.myOwnFormatter'}" />
在你的控制器中,接收这些参数:
myOwnFormatter : function(parameter1, parameter2) { } /* 以此类推.. */
完成后,标签的文本将显示带有参数和i18n文本。
<Text text="{parts: ['i18n>PEC_to',
'promoprocsteps>RetailPromotionSalesFromDate_E',
'promoprocsteps>RetailPromotionSalesToDate_E'}],
formatter: 'retail.promn.promotioncockpit.utils.Formatter.formatDatesString'}"/>
假设您正在使用{0}、{1}等作为占位符,格式化函数可能如下所示(没有任何错误处理和日期的特殊处理,这在SCN示例中可能是必要的):
formatTextWithParams : function(textWithPlaceholders, any_placeholders /*Just as marker*/) {
var finalText = textWithPlaceholders;
for (var i = 1; i < arguments.length; i++) {
var argument = arguments[i];
var placeholder = '{' + (i - 1) + '}';
finalText = finalText.replace(placeholder, arguments[i]);
}
return finalText;
},