ASP.NET MVC has a nice feature called DataAnnotations that can simplify validation of user input. I couldn't find a way to work with the builtin data annotations so that the validation message will change if the user is running the spanish version of my application. Can someone put an example that takes multiple languages into account?
Asked
Active
Viewed 999 times
1
-
1See also this post: http://stackoverflow.com/questions/3758512/localize-data-annotations-default-messages-required-stringlength-etc – Pavel Surmenok Jan 22 '11 at 08:14
1 Answers
3
It will get quite ugly fast.
public class User
{
[Required(ErrorMessageResourceName = "Validation_Required", ErrorMessageResourceType = typeof(ModelTranslations))]
public int Id { get; set; }
[Required(ErrorMessageResourceName = "Validation_Required", ErrorMessageResourceType = typeof(ModelTranslations))]
[StringLength(40, ErrorMessageResourceName = "Validation_StringLength", ErrorMessageResourceType = typeof(ModelTranslations))]
public string FirstName { get; set; }
[Required(ErrorMessageResourceName = "Validation_Required", ErrorMessageResourceType = typeof(ModelTranslations))]
[StringLength(40, ErrorMessageResourceName = "Validation_StringLength", ErrorMessageResourceType = typeof(ModelTranslations))]
public string LastName { get; set; }
}
I got a prettier solution in my blog: http://blog.gauffin.org/2010/11/simplified-localization-for-dataannotations/

jgauffin
- 99,844
- 45
- 235
- 372