Yet another MVC localization question...
I'm trying to localize an ASP.Net MVC 3 app using localized Resource files to display texts in the views, as recommended.
The problem is, as usual, when trying to localize the default error messages from data annotations.
I know you can specify the resource file and key in every Attribute:
[Required(
ErrorMessageResourceType = typeof(CustomResourceManager),
ErrorMessageResourceName = "ResourceKey")]
public string Username { get; set; }
and even, which is better and preferred, you can override the default message, like this: Default resource for data annotations in ASP.NET MVC, so you can leave the Attributes like:
[Required]
public string Username { get; set; }
This last approach is the one I was following, and it works, but only when the DataAnnotation you want to override has ONE and ONLY ONE error message, as it always looks for a resource key called the same as the attribute in the custom resource file (e.g. "Required" needs a "RequiredAttribute" entry in the resource file)
Other attributes, like StringLength, have more than one error message, depending on the optional parameters you use. So, if you have a model like:
public class Person
{
[Required]
[StringLengthLocalizedAttribute(10, MinimumLength = 5)]
[Display(Name = "User name")]
public string UserName { get; set; }
}
The error message is "The field User name must be a string with a minimum length of 5 and a maximum length of 10."
And if you change the StringLength Attribute to:
[StringLengthLocalizedAttribute(10)]
the error message changes to "The field User name must be a string with a maximum length of 10." So, in this case, there are at least 2 default error messages to override, and the solutions given by @kim-tranjan fails.
My partial solution to this is implement my own StringLength Attribute like this:
public class StringLengthLocalizedAttribute : StringLengthAttribute
{
public StringLengthLocalizedAttribute(int maximumLength) : base(maximumLength)
{
ErrorMessageResourceType = typeof(CustomValidationResource);
}
public override string FormatErrorMessage(string name)
{
ErrorMessageResourceName = MinimumLength > 0 ? "StringLengthAttributeMinMax" : "StringLengthAttributeMax";
return base.FormatErrorMessage(name);
}
}
Where I have a localized resource "CustomValidationResource" with the validation messages, and set it as the ErrorMessageResourceType. Then, overriding FormatErrorMessage function, I decide which message string should be applied depending on the optional parameters.
So, the question here is: Does anybody know where can we find the whole list of resource keys used by the DataAnnotation Attributes and see then how many different error messages we have in each one without being testing each and every one?
Or even better, can we have the original RESX file to see the string templates and localize them using the same resource keys? This way, changing only the ErrorMessageResourceType should work for all the DataAnnotations Attibutes, and I shouldn't need to guess where to put "{1}" or "{2}" in my localized string.
Thanks, Sergi