简体   繁体   English

默认模型Binder的MVC本地化

[英]MVC Localization of Default Model Binder

I am currently trying to figure out how to localize the error messages generated by MVC. 我目前正在试图找出如何本地化MVC生成的错误消息。 Let me use the default model binder as an example, so I can explain the problem. 让我使用默认的模型绑定器作为示例,所以我可以解释这个问题。

Assuming I have a form, where a user enters thier age. 假设我有一个表单,用户进入他的年龄。 The user then enters "ten" in to the form, but instead of getting the expected error of 然后用户在表单中输入“十”,而不是获得预期的错误

"Age must be beween 18 and 25." “年龄必须在18到25岁之间。”

the message 消息

"The value 'ten' is not valid for Age." “价值'十'对年龄无效。”

is displayed. 被展示。

The entity's age property is defined below: 实体的年龄属性定义如下:

    [Range(18, 25, ErrorMessageResourceType = typeof (Errors), 
        ErrorMessageResourceName = "Age", ErrorMessage = "Range_ErrorMessage")]    
    public int Age { get; set; }

After some digging, I notice that this error text comes from the System.Web.Mvc.Resources.DefaultModelBinder_ValueInvalid in the MvcResources.resx file. 经过一番挖掘,我注意到这个错误文本来自MvcResources.resx文件中的System.Web.Mvc.Resources.DefaultModelBinder_ValueInvalid

Now, how can create localized versions of this file? 现在,如何创建此文件的本地化版本?

As A solution, for example, should I download MVC source and add MvcResources.en_GB.resx , MvcResources.fr_FR.resx , MvcResources.es_ES.resx and MvcResources.de_DE.resx , and then compile my own version of MVC.dll ? 作为一个解决方案,例如,我应该下载MVC源并添加MvcResources.en_GB.resxMvcResources.fr_FR.resxMvcResources.es_ES.resxMvcResources.de_DE.resx ,然后编译我自己的MVC.dll版本?

But I don't like this idea. 但我不喜欢这个想法。 Any one else know a better way? 还有其他人知道更好的方法吗?

See http://forums.asp.net/p/1512140/3608427.aspx , scroll down to Brad Wilson's reply near the bottom of that page (Sat, Jan 09 2010, 3:20 PM). 请参阅http://forums.asp.net/p/1512140/3608427.aspx ,向下滚动到该页底部附近的Brad Wilson的回复(2010年1月9日星期六,下午3:20)。 There are static properties on the DefaultModelBinder that you can set to localize the generic error messages. DefaultModelBinder上有静态属性,您可以设置这些属性来本地化一般错误消息。

The reason a generic error message is used instead of your [Range] message is that [Range] provides a validation error message, but this particular case is a binding error. 使用通用错误消息而不是[Range]消息的原因是[Range]提供了验证错误消息,但是这种特殊情况是绑定错误。 There's absolutely no way the framework can ever hope to convert the string "ten" to an Int32, so it can't even fire the [Range] validator. 框架绝对没有办法希望将字符串“ten”转换为Int32,因此它甚至无法触发[Range]验证器。 This is what the "PropertyValueInvalid" key as mentioned in that forum controls. 这就是该论坛中提到的“PropertyValueInvalid”键控制的内容。

In MVC3 do the following to change default messages: 在MVC3中,执行以下操作以更改默认消息:

  1. add the App_GlobalResources folder to your ASP.NET site 将App_GlobalResources文件夹添加到ASP.NET站点
  2. add a new resource file, call it f.ex. 添加一个新的资源文件,称之为f.ex. MyResources.resx MyResources.resx
  3. add these keys 添加这些键
    • PropertyValueRequired : A value is required. PropertyValueRequired :值是必需的。
    • PropertyValueInvalid : The value '{0}' is not valid for {1}. PropertyValueInvalid :值“{0}”对{1}无效。
  4. in Application_Start of global.asax.cs add the line DefaultModelBinder.ResourceClassKey = "MyResources"; 在global.asax.cs的Application_Start中添加行DefaultModelBinder.ResourceClassKey = "MyResources";

Have you tried: IDataErrorInfo property 您是否尝试过: IDataErrorInfo属性

This article will help 这篇文章会有所帮助

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

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