5

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?

Set
  • 47,577
  • 22
  • 132
  • 150
Joshua Engelbrecht
  • 185
  • 1
  • 2
  • 9
  • Did you solve it? I'm stuck at this too! – Rovdjuret Jul 28 '16 at 10:02
  • @Rovdjuret, unfortunately the project decided to go with an older version of MVC and I have not had time to circle back around to this. – Joshua Engelbrecht Jul 28 '16 at 14:06
  • I finally found a soluton. See http://stackoverflow.com/questions/38630777/localization-of-data-annotations-in-separate-class-library?noredirect=1#comment64681695_38630777 if interested. – Rovdjuret Jul 30 '16 at 08:43

0 Answers0