定制的错误信息未在ASP.NET MVC 4翻译 [英] Customised error messages are not translated in ASP.NET MVC 4
问题描述
我要翻译的验证消息字段的日期必须是最新的。
我在Global.asax中添加下列键进入的Application_Start()
ClientDataTypeModelValidatorProvider.ResourceClassKey =ModelBinders;
DefaultModelBinder.ResourceClassKey =ModelBinders;
我在创建App_GlobalResources文件ModelBinders.resx,ModelBinders.nl.resx,ModelBinders.fr.resx。
我在.resx文件添加以下字符串资源(或翻译):
名称值
==== =====
FieldMustBeDate字段{0}必须是日期。
FieldMustBeNumeric字段{0}必须是数字。
PropertyValueInvalid值{0}是无效的{1}。
PropertyValueRequired值是必需的。
块引用>当我提出一个字符串的日期,我会收到译名为FieldMustBeDate。当我提交了一个无效日期(例如01/01 / 201A)我收到未翻译的消息PropertyValueInvalid,即在默认ModelBinders.resx定义,而不是翻译...我如何可以显示正确的翻译对于PropertyValueInvalid?
解决方案我将解释如何我特定的客户端的消息。首先,在模型中设置的资源:
[必需的(ErrorMessageResourceType = typeof运算(Resources.ModelBinders),ErrorMessageResourceName =必需)]
[显示(名称=用户名的ResourceType = typeof运算(Resources.ModelBinders))]
公共字符串用户名{获得;组; }第二,在控制器您覆盖线程的文化,我得到了从路线,例如在初始化方法:
保护覆盖无效初始化(RequestContext的的RequestContext)
{
字符串的CultureInfo = requestContext.RouteData.GetRequiredString(CultureInfo的);
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(CultureInfo的);
System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(CultureInfo的);
base.Initialize(RequestContext的);
}
这是非常重要的资源格式正确:ModelBinders.resx,ModelBinders.es-ES.resx,ModelBinders.en-US.resx ......没别的,它为我好。我希望这种方法会帮助你。
I want to translate the validation message "The field Date must be a date."
I've added the following keys into Application_Start() at Global.asax
ClientDataTypeModelValidatorProvider.ResourceClassKey = "ModelBinders"; DefaultModelBinder.ResourceClassKey = "ModelBinders";
I've created ModelBinders.resx, ModelBinders.nl.resx, ModelBinders.fr.resx in App_GlobalResources.
I've added the following string resources (or translations) in the .resx files:
Name Value ==== ===== FieldMustBeDate The field {0} must be a date. FieldMustBeNumeric The field {0} must be a number. PropertyValueInvalid The value '{0}' is not valid for {1}. PropertyValueRequired A value is required.
When I submit a string for a date, I will receive the translation for "FieldMustBeDate". When I submit an invalid date (for example "01/01/201a") I receive the untranslated message for "PropertyValueInvalid" that is defined in the default ModelBinders.resx , instead of the translation... How can I show the correct translation for PropertyValueInvalid?
解决方案I'll explain how I specific client messages. First, in the model you set the resource:
[Required(ErrorMessageResourceType = typeof(Resources.ModelBinders), ErrorMessageResourceName = "Required")] [Display(Name = "UserName", ResourceType = typeof(Resources.ModelBinders))] public string UserName { get; set; }
Second, in the the controller you overwrite thread culture, I get that from a route, for example in the Initialize method:
protected override void Initialize(RequestContext requestContext) { string cultureInfo = requestContext.RouteData.GetRequiredString("cultureInfo"); System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(cultureInfo); System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(cultureInfo); base.Initialize(requestContext); }
It is important that resources are properly formatted: ModelBinders.resx, ModelBinders.es-ES.resx, ModelBinders.en-US.resx ... And nothing else, It works for me well. I hope this approach will help you.
这篇关于定制的错误信息未在ASP.NET MVC 4翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!