Search code examples
jqueryasp.net-mvclocalizationunobtrusive-validation

Jquery validation localization


I use jquery validate unobtrusive in the Asp.Net MVC 5 project. And validation works in English, but I want it in other language, Norwegian for instance.

I found messages_no.js here https://github.com/jzaefferer/jquery-validation/tree/master/src/localization and included that js file after jquery.validate.js and jquery.validate.unobtrusive.js. I can see in developer tools that it is included in page properly but error messages are still in English.

Am I missing something?


Solution

  • The error messsage are retrieved from html attributes(data-val-) of controls. Controls are generated server side (Html.EditorFor), using the DataAnnotations of the property.

    So for example instead of

    [Required(ErrorMessage = "Required")]
    public string Email { get; set; }
    

    you should use resources (.resx files)

    [Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "Required"]    
    public string Email { get; set; }
    

    Make sure Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture are set in the desired culture.