ASP.NET MVC 3: - 使用数据库,而不是资源文件作为本地化店 [英] ASP.NET MVC 3 : - Using database instead of resource files as the localization store

查看:432
本文介绍了ASP.NET MVC 3: - 使用数据库,而不是资源文件作为本地化店的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经定位于数据库的字符串,并想知道,如果延长ASP.NET资源提供者模型 将与ASP.NET MVC 3的Razor视图引擎工作。

We have localised strings in the database and would like to know if extending the ASP.NET Resource Provider Model would work with the ASP.NET MVC 3 Razor view engine.

请让我知道,如果ASP.NET MVC 3的Razor视图引擎支持从数据库中检索本地化字符串一旦我们扩展了ASP.NET资源提供者模型。还是只能用经典ASP.NET而不是ASP.NET MVC工作。

Kindly let me know if ASP.NET MVC 3 Razor view engine supports retrieving localized strings from the database once we have extended the ASP.NET Resource Provider model. Or does it work only with Classic ASP.NET and not with ASP.NET MVC.

感谢您

SatyaprakashĴ

Satyaprakash J

推荐答案

我发现到目前为止是最干净的解决方案:<一href=\"http://www.$c$cproject.com/Tips/514321/A-Simple-and-Effective-Way-to-Localize-ASP-Net-MVC\">http://www.$c$cproject.com/Tips/514321/A-Simple-and-Effective-Way-to-Localize-ASP-Net-MVC.

The cleanest solution I've found so far is: http://www.codeproject.com/Tips/514321/A-Simple-and-Effective-Way-to-Localize-ASP-Net-MVC.

评论/反馈是欢迎的。

编辑1:根据意见,我添加了code的例子和使用的链接作为参考

Edit 1: Based on comments, I added code examples and used the link as reference.

我创建了一个customDataAnnotationsProvider类:

I created a customDataAnnotationsProvider class:

public class CustomDataAnnotationsProvider: DataAnnotationsModelMetadataProvider
{
    private ResourceManager resourceManager = new ResourceManager();
    protected override ModelMetadata CreateMetadata(
                         IEnumerable<Attribute> attributes,
                         Type containerType,
                         Func<object> modelAccessor,
                         Type modelType,
                         string propertyName)
    {
        string key = string.Empty;
        string localizedValue = string.Empty;


        foreach (var attr in attributes)
        {
            if (attr != null)
            {
                if (attr is DisplayAttribute)
                {
                    key = ((DisplayAttribute)attr).Name;
                    if (!string.IsNullOrEmpty(key))
                    {
                        localizedValue = resourceManager.GetLocalizedText(key);
                        ((DisplayAttribute)attr).Name = localizedValue;
                    }
                }
                else if (attr is ValidationAttribute)
                {
                    key = ((ValidationAttribute)attr).ErrorMessage;
                    if (!string.IsNullOrEmpty(key))
                    {
                        localizedValue = resourceManager.GetLocalizedText(key);
                        ((ValidationAttribute)attr).ErrorMessage = localizedValue;
                    }
                }
            }
        }
        return base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
    }
}

然后我在Global.asax中引用的自定义提供程序上ApplicationStart

Then I referenced the custom provider on ApplicationStart in Global.asax

ModelMetadataProviders.Current = new Project.Web.Helpers.CustomDataAnnotationsProvider();

您不必更改模型,并可以使用显示注释:

You do not have to change your model and can use the Display annotation:

[Display(Name = "CustomerAccountNumber")]
public string CustomerAccountNumber { get; set; }

这篇关于ASP.NET MVC 3: - 使用数据库,而不是资源文件作为本地化店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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