简体   繁体   English

ASP.NET MVC4多语言数据注释

[英]ASP.NET MVC4 Multi-lingual Data Annotations

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. ...这反过来会自动以英语为该表单字段生成标签。

Now, if I need my app to support 5 languages, what is the best approach from a ASP.NET MVC application to handle this? 现在,如果我需要我的应用程序支持5种语言,那么ASP.NET MVC应用程序处理此问题的最佳方法是什么?

Scope of application is about 400 - 600 data fields. 应用范围约为400-600个数据字段。

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

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. 然后,您可以使用自定义ModelMetadataProvider ,该模型知道本地化上下文并提供适当的字符串。 To get a better solution, you can make your custom ModelMetadataProvider infer conventions, (which cuts down on the need for verbose attributes). 为了获得更好的解决方案,可以使自定义ModelMetadataProvider推断约定(从而减少了对冗长属性的需求)。

Phil Haack has a blog article called Model Metadata and Validation Localization using Conventions that explains how this works. Phil Haack的博客文章名为Model Metadata and Validation Localization using Conventions ,它解释了它是如何工作的。 There is also a corresponding NuGet package called ModelMetadataExtensions with source code available on github at https://github.com/Haacked/mvc-metadata-conventions . 还有一个名为ModelMetadataExtensions的相应NuGet软件包,其源代码可从github上的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 . 附带说明一下,我建议您回顾一下我的一个老问题: .NET中有效的本地化策略 They don't specifically address your question, but will be very helpful if you are working on a multilingual .NET app. 他们没有专门解决您的问题,但是如果您使用的是多语言.NET应用程序,它们将非常有帮助。

I would make custom attribute, like [MyDisplayName("Section", "Key")] And this would provide translation based on selected language. 我将创建自定义属性,例如[MyDisplayName(“ Section”,“ Key”)] ,这将提供基于所选语言的翻译。 Also check out database driven resources manager, like http://www.west-wind.com/presentations/wwDbResourceProvider/ 还要检查数据库驱动的资源管理器,例如http://www.west-wind.com/presentations/wwDbResourceProvider/

The best approach for localization is to store your strings in the database and not resource files unless, 最好的本地化方法是将字符串存储在数据库中而不是资源文件中,除非,

  1. Your app is very static 您的应用程序非常静态
  2. Your language set is very static 您的语言集非常静态

You can decorate your model with custom attribute where you set default string and DB id for the resource eg 您可以使用自定义属性装饰模型,在其中设置资源的默认字符串和数据库ID,例如

[MyResource("email", 123)]

You can write custom http delegating handler to get resource out of cache (for example). 您可以编写自定义的http委托处理程序以使资源脱离缓存(例如)。 Once you authenticate client, you know client's language demand and resource id. 对客户端进行身份验证后,您将知道客户端的语言要求和资源ID。 So, the client with Spanish and resource id = 1 will get "Si", the one with English will get "Yes". 因此,西班牙语和资源ID = 1的客户端将获得“ Si”,英语的客户端将获得“是”。 The resource id will be mapped to language-specific string. 资源ID将映射到特定于语言的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM