ASP.NET MVC 3:- 使用数据库而不是资源文件作为本地化存储

发布于 2024-12-22 01:31:33 字数 310 浏览 2 评论 0原文

我们已在数据库中本地化了字符串,并想知道是否扩展 ASP.NET 资源提供程序模型将与 ASP.NET MVC 3 Razor 视图引擎一起使用。

请告诉我,一旦我们扩展了 ASP.NET 资源提供程序模型,ASP.NET MVC 3 Razor 视图引擎是否支持从数据库检索本地化字符串。或者它仅适用于经典 ASP.NET 而不适用于 ASP.NET MVC。

谢谢萨蒂亚普拉

卡什 J

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岛徒 2024-12-29 01:31:33

到目前为止我发现的最干净的解决方案是: http://www.codeproject.com/Tips/514321/A-Simple-and-Effective-Way-to-Localize-ASP-Net-MVC

欢迎评论/反馈。

编辑1:根据评论,我添加了代码示例并使用链接作为参考。

我创建了一个 customDataAnnotationsProvider 类:

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 上引用了自定义提供程序

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

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

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

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; }
沉睡月亮 2024-12-29 01:31:33

你很幸运,因为 Rick 已经为你做到了!

西风.ASP.NET 全球化数据驱动资源提供程序

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

Westwind.Globalization Data Driven Resource Provider for ASP.NET

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文