在XML视图中将参数传递给i18n模型

时间:2014-12-04 13:34:27

标签: internationalization sapui5

我们如何从XML视图中将参数传递给i18n模型?

没有参数

<Label text="{i18n>myKey}"/>

有效,但我们如何在该表达式中传递参数?

到目前为止,我发现的唯一信息是http://scn.sap.com/thread/3586754。我真的希望这不是正确的方法,因为这看起来更像是一个(丑陋的)黑客攻击。

6 个答案:

答案 0 :(得分:9)

诀窍是使用像这样的格式化程序jQuery.sap.formatMessage

<Label text="{parts:['i18n>myKey', 'someModel>/someProperty'], 
                    formatter: 'jQuery.sap.formatMessage'}"/>

这将取模型/someProperty中的值someModel,并将其粘贴到您的i18n资源包的myKey中。

答案 1 :(得分:2)

链接上写的内容对于复杂的格式化案例是正确的。 但如果你想组合两个字符串,你可以写

<Label text="{i18n>myKey} Whatever"/> 
or
<Label text="{i18n>myKey1} {i18n>myKey2}"/>

答案 2 :(得分:2)

目前这是不可能的。但是你可以使用这个简单的解决方法,这对我有用。

制剂

首先,我们在 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&gt;} 的模型。要在XML视图中使用i18n函数,我们创建一个formatter function。此函数解析绑定的各个部分并返回本地化的字符串。

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' }" />

您可以根据需要传递多少参数,但请确保第一个“部分”是属性键。

答案 3 :(得分:1)

尽管看起来很难看,但你提到的link给出的答案是要走的路。然而,它可能看起来很复杂(阅读丑陋),所以让我们分解它。

因此,您可以使用以下内容传递单个参数

<Label text="{path: 'someParameter', formatter: '.myOwnFormatter'}"/>

这里,someParameter是绑定到整个页面/控件的OData模型属性的绑定,因为很明显你不会绑定&#34;硬编码&#34 ;生产方案中的价值。但是它确实以此结束,因为您看到i18n文本没有地方。这在 controller.js

中得到了解决

在控制器中,添加具有相同格式化名称的控制器方法

myOwnFormatter : function(someParameter) { /* the 'someParameter' will be received in this function */ var i18n = this.i18nModel; /* However you can access the i18n model here*/ var sCompleteText = someParameter + " " + i18n.getText("myKey") /* Concatenate the way you need */ }

传递多个参数

使用,

<Label text="{parts:[{path : 'parameter1'}, {path :'parameter2'}], formatter : '.myOwnFormatter'}" />

在您的控制器中,接收这些参数,

myOwnFormatter : function(parameter1, parameter2) { } /* and so on.. */

完成所有这些操作后,标签的文字将显示参数和你的i18n文字。

答案 4 :(得分:0)

原则上它完全如上面提到的SCN-Link中所描述的那样。您需要绑定到资源束的键,以及对应该进入相应文本的参数的值的其他绑定。最后,这些绑定找到的所有值必须以某种方式组合,您需要为其指定格式化程序。

通过省略绑定数组中的路径前缀,可以稍微缩短一下。使用SCN中的示例,它的工作原理如下:

<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;
},

答案 5 :(得分:0)

创建文件formatter.js

   sap.ui.define([
        "sap/base/strings/formatMessage"
    ], function (formatMessage) {
        "use strict";

        return {
            formatMessage: formatMessage
        };
    });

查看

<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}