【问题标题】:Translate breeze validation messages翻译微风验证消息
【发布时间】:2013-01-15 02:12:56
【问题描述】:

改进我的示例,了解如何使用获得的元数据在淘汰赛中创建验证规则 (http://stackoverflow.com/questions/13662446/knockout-validation-using-breeze-utility) 现在我使用微风插入的验证器进入实体:

function addValidationRules(entity) {
    var entityType = entity.entityType;
    console.log(entityType);
    if (entityType) {
        for (var i = 0; i < entityType.dataProperties.length; i++) {
            var property = entityType.dataProperties[i];
            var propertyName = property.name;
            var propertyObject = entity[propertyName];

            var validators = [];
            for (var u = 0; u < property.validators.length; u++) {
                var validator = property.validators[u];
                var nValidator = {
                    propertyName: propertyName,
                    validator: function (val, other) {
                        var error = this.innerValidator.validate(val, { displayName: this.propertyName });
                        this.message = error ? error.errorMessage : "";
                        return error === null;
                    },
                    message: "",
                    innerValidator: validator
                }
                validators.push(nValidator);
            }
            propertyObject.extend({
                validation: validators
            });
        }

        for (var i = 0; i < entityType.foreignKeyProperties.length; i++) {
            var property = entityType.foreignKeyProperties[i];
            var propertyName = property.name;
            var propertyObject = entity[propertyName];

            var validators = [];
            for (var u = 0; u < property.validators.length; u++) {
                var validator = property.validators[u];
                var nValidator = {
                    propertyName: propertyName,
                    validator: function (val, other) {
                        var error = this.innerValidator.validate(val, { displayName: this.propertyName });
                        this.message = error ? error.errorMessage : "";
                        return error === null;
                    },
                    message: "",
                    innerValidator: validator
                }
                validators.push(nValidator);
            }
            propertyObject.extend({
                validation: validators
            });
            if (!property.isNullable) {
                //Bussiness Rule: 0 is not allowed for required foreign keys
                propertyObject.extend({ notEqual: foreignKeyInvalidValue });
            }
        }
    }
};

我现在需要将错误消息翻译成我的语言,我想知道是否可以包含一个类似于敲除验证中包含的功能来翻译消息:

//quick function to override rule messages
ko.validation.localize = function (msgTranslations) {

    var msg, rule;

    //loop the properties in the object and assign the msg to the rule
    for (rule in msgTranslations) {
        if (ko.validation.rules.hasOwnProperty(rule)) {
            ko.validation.rules[rule].message = msgTranslations[rule];
        }
    }
};
//#endregion

【问题讨论】:

    标签: breeze


    【解决方案1】:

    这是个好主意。请将其添加到Breeze User Voice(并投票)。我们非常重视这些建议。

    短期内还有另一种方法。您可以替换任何

    Validator.messageTemplates
    

    您自己的消息。 Validator.messageTemplates 是一个配置对象,以验证器的名称为键,其中值是错误消息的参数化版本。

    我们确实需要更好地记录这一点。

    【讨论】:

    • 我见过这个配置对象,但是以微风更新的速度每次都必须去替换那些行似乎很乏味。
    • 这个不用接触源码,微风加载后直接更新即可。
    • 朱利安,请重新阅读API documentation of messageTemplates 并帮助我们了解我们缺少什么。我们认为,如果您在应用程序启动时调用自己的自定义消息模板配置代码,您的需求就会得到满足。这样的配置会有Validator.messageTemplates["required", "'%displayName%' is required")这样的行当然你会用你的语言说出来。
    • 对不起,这是我的错。我没有阅读 api(或breezejs.com/documentation/validation 的“自定义消息模板”部分),我认为“Validator.messageTemplates”是一个无法更改的内部属性。现在我清楚地看到,没有其他必要了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    相关资源
    最近更新 更多