I am working on an ASP.Net Core web application and need to localize all the static strings we use in the app.
I have the Controller and the View strings localized by implementing the IStringLocalizerFactory
and adding it as a service in the Startup.cs.
I am now trying to add localization to the Model Validation when using Annotations. In my User model, I have
[Required(ErrorMessageResourceType = typeof(UserModelErrorMessages), ErrorMessageResourceName = "FullNameRequired")]
[MinLength(2, ErrorMessageResourceType = typeof(UserModelErrorMessages), ErrorMessageResourceName ="FullNameMinLength")]
[MaxLength(100, ErrorMessageResourceType =typeof(UserModelErrorMessages), ErrorMessageResourceName ="FullNameMaxLength")]
public string FullName { get; set; }
I have placed "FullNameRequired", "FullNameMinLength", "FullNameMaxLength" in the UserModelErrorMessage.resx file. I also have a UserModelErrorMessage.fr.resx file with the same values but the French equivalent messages. The correct error messages show when the browser is set to English. When I switch my browser to use the French ('fr') language, my static strings that I have defined in the view and controller show up in French but the data validation messages still show in English. I have tried Models.User.fr.resx and also generating the .resource files manually all to no avail.
In Startup.cs ConfigureServices:
services.AddMvc()
.AddViewLocalization(options=>options.ResourcesPath="Resources")
.AddDataAnnotationsLocalization();
In Startup.cs - Configure
app.UseRequestLocalization(new RequestLocalizationOptions
{
SupportedCultures = new List<CultureInfo>
{
new CultureInfo("fr"),
new CultureInfo("zh"),
new CultureInfo("tr"),
new CultureInfo("en"),
new CultureInfo("en-US"),
},
SupportedUICultures = new List<CultureInfo>
{
new CultureInfo("fr"),
new CultureInfo("zh"),
new CultureInfo("tr"),
new CultureInfo("en"),
new CultureInfo("en-US"),
}
}, new RequestCulture("en-US")
);
What am I missing to get the Data Annotation Validation Messages in French?