DataAnnotations and multi lingual web applications
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 languag开发者_Go百科es into account?
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/
精彩评论