ASP.NET MVC 3 : - Using database instead of resource files as the localization store

本小妞迷上赌 提交于 2019-11-29 14:02:53

问题


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.

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.

Thank you

Satyaprakash J


回答1:


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.

Comments/Feedback are welcomed.

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

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

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



回答2:


You are in luck because Rick have already done it for you!

Westwind.Globalization Data Driven Resource Provider for ASP.NET



来源:https://stackoverflow.com/questions/8562916/asp-net-mvc-3-using-database-instead-of-resource-files-as-the-localization-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!