asp.net-mvc 带有 DisplayAttribute 和自定义资源提供程序的 ASP.NET MVC 3 本地化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4872309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:49:47  来源:igfitidea点击:

ASP.NET MVC 3 localization with DisplayAttribute and custom resource provider

asp.net-mvcasp.net-mvc-3localizationinternationalizationdisplayattribute

提问by slfan

I use a custom resource provider to get resource strings from a database. This works fine with ASP.NET where I can define the resource type as a string. The metadata attributes for model properties in MVC 3 (like [Range], [Display], [Required] require the type of a Resource as a parameter, where the ResourceType is the type of the generated code-behind class of a .resx file.

我使用自定义资源提供程序从数据库中获取资源字符串。这适用于 ASP.NET,我可以将资源类型定义为字符串。MVC 3 中模型属性的元数据属性(如 [Range]、[Display]、[Required] 需要 Resource 的类型作为参数,其中 ResourceType 是生成的 .resx 文件的代码隐藏类的类型.

    [Display(Name = "Phone", ResourceType = typeof(MyResources))]
    public string Phone { get; set; }

Because I don't have .resx files, such a class does not exist. How can I use the model attributes with a custom resource provider?

因为我没有 .resx 文件,所以不存在这样的类。如何将模型属性与自定义资源提供程序一起使用?

I would like to have something like this:

我想要这样的东西:

    [Display(Name = "Telefon", ResourceTypeName = "MyResources")]
    public string Phone { get; set; }

The DisplayNameAttribute from System.ComponentModel had a overridable DisplayName Property for this purpose, but the DisplayAttribute class is sealed. For the validation attributes no corresponding classes exist.

为此,System.ComponentModel 中的 DisplayNameAttribute 具有可覆盖的 DisplayName 属性,但 DisplayAttribute 类是密封的。对于验证属性,不存在相应的类。

采纳答案by pollirrata

You can extend the DisplayNameAttribute and override the DisplayName string property. I have something like this

您可以扩展 DisplayNameAttribute 并覆盖 DisplayName 字符串属性。我有这样的事情

    public class LocalizedDisplayName : DisplayNameAttribute
    {
        private string DisplayNameKey { get; set; }   
        private string ResourceSetName { get; set; }   

        public LocalizedDisplayName(string displayNameKey)
            : base(displayNameKey)
        {
            this.DisplayNameKey = displayNameKey;
        }


        public LocalizedDisplayName(string displayNameKey, string resourceSetName)
            : base(displayNameKey)
        {
            this.DisplayNameKey = displayNameKey;
            this.ResourceSetName = resourceSetName;
        }

        public override string DisplayName
        {
            get
            {
                if (string.IsNullOrEmpty(this.GlobalResourceSetName))
                {
                    return MyHelper.GetLocalLocalizedString(this.DisplayNameKey);
                }
                else
                {
                    return MyHelper.GetGlobalLocalizedString(this.DisplayNameKey, this.ResourceSetName);
                }
            }
        }
    }
}

For MyHelper, the methods can be something like this:

对于MyHelper,方法可以是这样的:

public string GetLocalLocalizedString(string key){
    return _resourceSet.GetString(key);
}

Obviously you will need to add error handling and have the resourceReaderset up. More info here

显然,您需要添加错误处理并进行resourceReader设置。更多信息在这里

With this, you then decorate your model with the new attribute, passing the key of the resource you want to get the value from, like this

有了这个,然后你用新属性装饰你的模型,传递你想要从中获取值的资源的键,就像这样

[LocalizedDisplayName("Title")]

Then the Html.LabelForwill display the localized text automatically.

然后Html.LabelFor将自动显示本地化的文本。

回答by Marcin Rybacki

The most cleanest way I came up with is to override DataAnnotationsModelMetadataProvider. Here's a very neat article on how to do this.

我想出的最干净的方法是覆盖DataAnnotationsModelMetadataProvider. 这是一篇关于如何做到这一点的非常简洁的文章。

http://buildstarted.com/2010/09/14/creating-your-own-modelmetadataprovider-to-handle-custom-attributes/

http://buildstarted.com/2010/09/14/creating-your-own-modelmetadataprovider-to-handle-custom-attributes/

回答by danludwig

I think you would have to override the DataAnnotations properties to localize them with a DB resource provider. You could inherit from the current ones, and then specify further properties such as DB connection string to use when getting the resources from your custom provider.

我认为您必须覆盖 DataAnnotations 属性才能使用数据库资源提供程序对它们进行本地化。您可以继承当前的属性,然后指定更多属性,例如从自定义提供程序获取资源时要使用的数据库连接字符串。