4

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?

Sparky
  • 98,165
  • 25
  • 199
  • 285
Aleksa
  • 2,976
  • 4
  • 30
  • 49

1 Answers1

4

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.

tmg
  • 19,895
  • 5
  • 72
  • 76
  • 1
    Thanks, it works! I have one more question. Is it possible to define that error message globally, so I don't have to specify it everywhere I need Required attribute. – Aleksa Jan 25 '16 at 15:49
  • you want every property on every viewmodel to be required? – tmg Jan 25 '16 at 15:52
  • I want to be able only to write [Requried] and that it automatically fetches it from Resources.Required. If it is possible... – Aleksa Jan 25 '16 at 15:57
  • To define that in only one place, like some global attribute override. – Aleksa Jan 25 '16 at 15:58
  • I found it here http://stackoverflow.com/questions/25084212/globally-override-asp-net-mvc-default-attribute-error-messages – Aleksa Jan 25 '16 at 16:02