Customised error messages are not translated in ASP.NET MVC 4

后端 未结 1 780
自闭症患者
自闭症患者 2021-02-10 07:05

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

Clie         


        
1条回答
  •  后悔当初
    2021-02-10 07:32

    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);
        }
    

    Client messages in English

    Client messages in Spanish

    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.

    0 讨论(0)
提交回复
热议问题