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

查看:13
本文介绍了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 资源提供程序模型,请告诉我 ASP.NET MVC 3 Razor 视图引擎是否支持从数据库中检索本地化字符串.还是仅适用于 Classic 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 J

Satyaprakash J

推荐答案

目前我找到的最干净的解决方案是:http://www.codeproject.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.

欢迎评论/反馈.

根据评论,我添加了代码示例并使用链接作为参考.

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

您不必更改模型,可以使用 Display 注释:

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天全站免登陆