6

I'm trying to implement this: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.1#dataannotations-localization

My code:

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
...
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
    .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
    .AddDataAnnotationsLocalization(options =>
    {
        options.DataAnnotationLocalizerProvider = (type, factory) =>
            factory.Create(typeof(SharedResource));
    });
...
}

SharedResource.cs

namespace MyProj.Classes
{
    /// <summary>
    /// Dummy class to group shared resources
    /// </summary>
    public class SharedResource
    {

    }
}

FooViewModel.cs

public class FooViewModel
{
    [Required(ErrorMessage = "EmailRequired")]
    [EmailAddress(ErrorMessage = "EmailIsNotValid")]
    [Display(Name = "Email")]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
}

FooPage.cshtml

...
<input asp-for="Email" class="form-control">
<div class="invalid-feedback" style="display:block;">
    <span asp-validation-for="Email"></span>
</div>
...

And I have the resource file Resources\SharedResource.resx

Email Email
EmailIsNotValid The Email field is not a valid email address.
EmailRequired The Email field is required.

Doesn't work, no errors during compilation or runtime. Instead of a translation, it shows me 'EmailIsNotValid' or 'EmailRequired'. What could go wrong?

Sergey Zykov
  • 687
  • 2
  • 9
  • 15
  • You want the `ErrorMessageResourceName` and `ErrorMessageResourceType` properties rather than `ErrorMessage` – Camilo Terevinto Jun 20 '18 at 19:09
  • Possible duplicate of [How localize ErrorMessage in DataAnnotation?](https://stackoverflow.com/questions/20699594/how-localize-errormessage-in-dataannotation) – Camilo Terevinto Jun 20 '18 at 19:10
  • I assume things changed since mvc 5, in official docs there's no ErrorMessageResourceName/Type info (in my link). It might be duplicate of https://stackoverflow.com/questions/48769199/localization-of-requiredattribute-in-asp-net-core-2-0 – Sergey Zykov Jun 20 '18 at 19:26
  • @CamiloTerevinto This is .net core. It handles localization differently – JSON Mar 26 '19 at 15:19

1 Answers1

2

Duplicate of Localization of RequiredAttribute in ASP.NET Core 2.0 But there are no marked answers and it's hidden in the comments.

Dummy SharedResource class should be in the same namespace as a web application (startup.cs). In my case, I removed '.classes'. Docs were unclear.

update: for dummy classes as mine same file name rules apply as in docs: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.1#resource-file-naming

Resources are named for the full type name of their class minus the assembly name. For example, a French resource in a project whose main assembly is LocalizationWebsite.Web.dll for the class LocalizationWebsite.Web.Startup would be named Startup.fr.resx. A resource for the class LocalizationWebsite.Web.Controllers.HomeController would be named Controllers.HomeController.fr.resx. If your targeted class's namespace isn't the same as the assembly name you will need the full type name. For example, in the sample project a resource for the type ExtraNamespace.Tools would be named ExtraNamespace.Tools.fr.resx.

Another workaround would be to name resource file as 'Classes.SharedResource.resx' or put it in a folder Resources\Classes.

Sergey Zykov
  • 687
  • 2
  • 9
  • 15
  • Maybe dummy SharedResource class should be in the same root namespace, not full (meaning folder name is not important)? – Niksr Jan 22 '22 at 13:05