How to localize ErrorMessage in DataAnnotation?

Using MVC 5 I need to localize ErrorMessage for DataAnnotation attributes. I get the following error

ERROR

The attribute argument must be a constant expression, a typeof expression, or an array creation attribute type attribute expression

In the model

[Compare("Password", ErrorMessage = Resources.Account_Register_ConfirmPasswordErrorMessage)] public string ConfirmPassword { get; set; } 

Any idea how to fix this?

+6
source share
2 answers

You need to use the ErrorMessageResourceName and ErrorMessageResourceType .

For example, for example:

 [Compare("Password", ErrorMessageResourceName = "ConfirmPasswordErrorMessage", ErrorMessageResourceType=typeof(<<type_of_your_resoruce_class>>)] public string ConfirmPassword { get; set; } 

There is also a link to MSDN, where you can find an explanation for both properties.

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.compareattribute(v=vs.110).aspx

Hope this helps!

Regards, Uros

+7
source

You do not need anything, just create your resource file in the right place.

For example, Resources> ViewModels> LoginVm.en-US.resx

in LoginVm: [Required (ErrorMessage = "แƒกแƒแƒฎแƒ”แƒšแƒ˜ แƒแƒ แƒ˜แƒก แƒแƒฃแƒชแƒ˜แƒšแƒ”แƒ‘แƒ”แƒšแƒ˜")] <(this is Georgian)

and in LoginVm.en-US.resx just add

แƒกแƒแƒฎแƒ”แƒšแƒ˜ แƒแƒ แƒ˜แƒก แƒแƒฃแƒชแƒ˜แƒšแƒ”แƒ‘แƒ”แƒšแƒ˜> Required username

and everything is done.

0
source

All Articles