翻译微风验证消息 [英] Translate breeze validation messages

查看:89
本文介绍了翻译微风验证消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Improving my example on how to use the metadata obtained to create validation rules in knockout (http://stackoverflow.com/questions/13662446/knockout-validation-using-breeze-utility) now I use the validators that breeze inserts into the entities:

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

我现在需要的是将错误消息翻译成我的语言,我想知道是否会可能包括类似于敲除验证中包含的微风函数来翻译消息:

What I need now is to translate the error messages into my language and I was wondering if it would be possible to include a function to breeze similar to the included in knockout-validation to translate messages:

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


推荐答案

好主意。请将其添加到微风用户语音中(并对其投票)。我们非常重视这些建议。

This is a good idea. Please add it to the Breeze User Voice ( and vote for it). We take these suggestions very seriously.

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

There is another approach for the short term. You can replace any of the

Validator.messageTemplates

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

with your own messages. Validator.messageTemplates is a configuration object keyed by the name of the validator where the value is a parameterized version of the error message.

我们确实需要对此进行更好的记录。

We do need to document this better.

这篇关于翻译微风验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆