ASP.NET MVC 3本土化与DisplayAttribute和自定义资源提供者 [英] ASP.NET MVC 3 localization with DisplayAttribute and custom resource provider

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

问题描述

我使用自定义的资源提供者从数据库中获取资源字符串。这能与ASP.NET,我可以定义资源类型为字符串。在MVC 3模特属性的元数据属性(如[范围],[显示],[必需]需要作为参数,其中的ResourceType是生成的$ C $的C-隐藏类的类型资源的类型.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并覆盖显示名称字符串属性。我有这样的事情

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 成立。更多信息这里

有了这个,你再与新的属性修饰你的模型,通过资源的关键,你想从,值类似这样

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.

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

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