Asp.net mvc 2 使用多种语言进行验证

Asp.net mvc 2 使用多种语言进行验证,asp.net-mvc-2,data-modeling,multilingual,Asp.net Mvc 2,Data Modeling,Multilingual,我正在编写带有模型验证的对象。 我的应用程序应该使用3种语言(英语、德语和捷克语) 我应该如何为验证模型分配和获取适当的语言字符串 捷克选择: [DisplayName("Nazev")] [StringLength(200,ErrorMessage="Nazev musi byt 10 az 200 znaku dlouhy",MinimumLength=10)] [Column] public string Name { get; set; } 英文选项: [DisplayName("Na

我正在编写带有模型验证的对象。 我的应用程序应该使用3种语言(英语、德语和捷克语)

我应该如何为验证模型分配和获取适当的语言字符串

捷克选择:

[DisplayName("Nazev")]
[StringLength(200,ErrorMessage="Nazev musi byt 10 az 200 znaku dlouhy",MinimumLength=10)]
[Column]
public string Name { get; set; }
英文选项:

[DisplayName("Name")]
[StringLength(200,ErrorMessage="Name has to be between 10 and 200",MinimumLength=10)]
[Column]
public string Name { get; set; }

您必须使用LocalizedDisplayName属性,请参见以下问题:


如果您的解决方案允许您使用资源字符串,那么LocalizedDisplayName可以很好地工作。不幸的是,在我的项目中,我们有几种语言,而且越来越多。。。翻译都保存在数据库中

因此,我们采取了解决问题的办法

  • 从我们自己的dll中的属性继承,并且
  • 然后获取默认格式字符串,并将其用作消息的基值
  • 让我们的翻译工厂获取值或在数据库中注册默认值
  • 然后我们导入名称空间并给它一个别名,实现的版本如下所示:

    using tf = MyDating.Translation;
    
    public class CompareAttribute : System.ComponentModel.DataAnnotations.CompareAttribute
    {
        public CompareAttribute(string otherProperty)
            :base(otherProperty)
        {
            var tf = TranslatetionFactory.Current.GetSection("CompareAttribute");
            var msg = tf.Get(this.ErrorMessageString);
            ErrorMessage = msg;
        }
    }
    
    在ViewModel中,我们执行以下操作:

    [tf.DisplayName("Verify Password")]
    [DataType(DataType.Password)]
    [tf.Compare("Password")]
    public string VerifyPassword
    {
        get;
        set;
    }
    
    上面的CompareAttribute如下所示:

    using tf = MyDating.Translation;
    
    public class CompareAttribute : System.ComponentModel.DataAnnotations.CompareAttribute
    {
        public CompareAttribute(string otherProperty)
            :base(otherProperty)
        {
            var tf = TranslatetionFactory.Current.GetSection("CompareAttribute");
            var msg = tf.Get(this.ErrorMessageString);
            ErrorMessage = msg;
        }
    }
    

    我可以选择我想要实现它的任何方式。我从来没有这样做过,这就是为什么,你可以把这个类你想要的,为我自己,我有一个助手文件夹在我的项目。然后,你需要在资源文件中添加你的资源(你在App_GlobalResources中添加的),这样在你的助手中,你只需在资源文件中提供变量名,然后在html助手中找到资源文件中正确的标记并替换它?