带有 DisplayAttribute 和自定义资源提供程序的 ASP.NET MVC 3 本地化 [英] ASP.NET MVC 3 localization with DisplayAttribute and custom resource provider

查看:18
本文介绍了带有 DisplayAttribute 和自定义资源提供程序的 ASP.NET MVC 3 本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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?

我想要这样的东西:

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

来自 System.ComponentModel 的 DisplayNameAttribute 有一个可覆盖的 DisplayName 属性用于此目的,但 DisplayAttribute 类是密封的.对于验证属性,不存在相应的类.

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.

推荐答案

您可以扩展 DisplayNameAttribute 并覆盖 DisplayName 字符串属性.我有这样的东西

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

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

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

For MyHelper, the methods can be something like this:

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

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

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

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

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")]

然后Html.LabelFor会自动显示本地化文本.

Then the Html.LabelFor will display the localized text automatically.

这篇关于带有 DisplayAttribute 和自定义资源提供程序的 ASP.NET MVC 3 本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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