ASP.NET MVC4多语言数据注释 [英] ASP.NET MVC4 Multi-lingual Data Annotations

查看:76
本文介绍了ASP.NET MVC4多语言数据注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在标准应用程序中,我具有以下内容:

In a standard application I have the following:

[Required]
[DisplayName("Email Address")]
public string EmailAddress { get; set; }

...这反过来会自动以英语为该表单字段生成一个标签.

...this in turn generates a label for this form field automatically in English.

现在,如果我需要我的应用程序支持5种语言,那么从ASP.NET MVC应用程序中解决该问题的最佳方法是什么?

Now, if I need my app to support 5 languages, what is the best approach from a ASP.NET MVC application to handle this?

应用范围约为400-600个数据字段.

Scope of application is about 400 - 600 data fields.

更新:我还需要支持更新应用程序中的小部分文本,例如页面名称和每种表单的介绍(小段).

UPDATE: I will also require support for updating small sections of text in the application like page names and introductions to each form (small paragraph).

推荐答案

不是为属性属性分配实际值,而是为资源字符串分配键.然后,您可以使用了解本地化上下文并提供适当字符串的自定义ModelMetadataProvider.为了获得更好的解决方案,可以使您的自定义ModelMetadataProvider推断约定(减少了对冗长属性的需要).

Instead of assigning the actual values to the attribute properties, assign keys for resource strings. Then, you can use a custom ModelMetadataProvider that is aware of the localization context and will provide the appropriate string. To get a better solution, you can make your custom ModelMetadataProvider infer conventions, (which cuts down on the need for verbose attributes).

Phil Haack的博客文章名为 ModelMetadataExtensions ,其源代码可在github上的

Phil Haack has a blog article called Model Metadata and Validation Localization using Conventions that explains how this works. There is also a corresponding NuGet package called ModelMetadataExtensions with source code available on github at https://github.com/Haacked/mvc-metadata-conventions .

作为旁注,我建议您回顾一下有关我的一个老问题的一些很棒的答案:

As a side note, I'd recommend reviewing some of the awesome answers I got on an old question of mine: Effective Strategies for Localization in .NET. They don't specifically address your question, but will be very helpful if you are working on a multilingual .NET app.

这篇关于ASP.NET MVC4多语言数据注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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