本地化属性比较 [英] Localize Compare attribute

查看:168
本文介绍了本地化属性比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前这个问题的底部,是包含三个属性的模型:OldPassword更改,新密码和ConfirmPassword。这些显示在更改密码的形式。我已经本地化的所有错误消息,除了一个:当属性比较失败,我不能完全本地化的错误消息。正如你所看到的,我读从我的资源文件名为FieldMismatch字符串错误消息。下面是在西班牙资源文件的字符串:

 萨尔瓦多坎普{0}Ÿ{1}坎波没有coinciden。

{0}部分得到正确替换为翻译OldPassword更改,但我不知道如何定位指针NEWPASSWORD。

因此​​,要回顾一下,我正在寻找一种方式来代替
    [比较(新密码,...

[对比(Resources.Culture.Account.Account.NewPassword,...

任何人有最好的方式有什么想法做到这一点?

 公共类LocalPasswordModel
{
    [必填(ErrorMessageResourceName =FieldIsRequired,ErrorMessageResourceType = typeof运算(Resources.Culture.Home.Global))]
    [数据类型(DataType.Password)
    [显示(NAME =CurrentPasswordLabel的ResourceType = typeof运算(Resources.Culture.Account.Account))]
    公共字符串OldPassword更改{搞定;组; }    [必填(ErrorMessageResourceName =FieldIsRequired,ErrorMessageResourceType = typeof运算(Resources.Culture.Home.Global))]
    [StringLength(100,ErrorMessageResourceName =NewPasswordLength,ErrorMessageResourceType = typeof运算(Resources.Culture.Account.Account),MinimumLength = 6)]
    [数据类型(DataType.Password)
    [显示(NAME =NewPasswordLabel的ResourceType = typeof运算(Resources.Culture.Account.Account))]
    公共字符串NEWPASSWORD {搞定;组; }    [数据类型(DataType.Password)
    [显示(NAME =ConfirmPasswordLabel的ResourceType = typeof运算(Resources.Culture.Account.Account))]
    [比较(新密码,ErrorMessageResourceName =FieldMismatch,ErrorMessageResourceType = typeof运算(Resources.Culture.Home.Global))]
    公共字符串ConfirmPassword {搞定;组; }
}


解决方案

这看起来像MVC4一个已知的bug,并MVC5(这我使用)。它看起来像它已被固定在5.1:

的http://aspnetwebstack.$c$cplex.com/workitem/1401

At the bottom of this question is a model containing three properties: OldPassword, NewPassword and ConfirmPassword. These display in a Change Password form. I've localized all of the error messages, except one: I cannot fully localize the error message when the Compare attribute fails. As you'll see, I'm reading the error message from a string named FieldMismatch in my resource file. Here's that string in the Spanish resource file:

El campo {0} y {1} campo no coinciden.

The {0} part is correctly getting replaced with the translation for OldPassword, but i don't know how to localize the pointer to NewPassword.

So to recap, I'm looking a way to replace [Compare("NewPassword", ... with [Compare(Resources.Culture.Account.Account.NewPassword, ...

Anyone have any thoughts on the best way to accomplish this?

public class LocalPasswordModel
{
    [Required(ErrorMessageResourceName = "FieldIsRequired", ErrorMessageResourceType = typeof(Resources.Culture.Home.Global))]
    [DataType(DataType.Password)]
    [Display(Name = "CurrentPasswordLabel", ResourceType = typeof(Resources.Culture.Account.Account))]
    public string OldPassword { get; set; }

    [Required(ErrorMessageResourceName = "FieldIsRequired", ErrorMessageResourceType = typeof(Resources.Culture.Home.Global))]
    [StringLength(100, ErrorMessageResourceName = "NewPasswordLength", ErrorMessageResourceType = typeof(Resources.Culture.Account.Account), MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "NewPasswordLabel", ResourceType = typeof(Resources.Culture.Account.Account))]
    public string NewPassword { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "ConfirmPasswordLabel", ResourceType = typeof(Resources.Culture.Account.Account))]
    [Compare("NewPassword", ErrorMessageResourceName = "FieldMismatch", ErrorMessageResourceType = typeof(Resources.Culture.Home.Global))]
    public string ConfirmPassword { get; set; }
}

解决方案

This looks like a known bug in MVC4, and MVC5 (which I am using). It looks like it has been fixed in 5.1:

http://aspnetwebstack.codeplex.com/workitem/1401

这篇关于本地化属性比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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