question

DavidMasshardt-8277 avatar image
0 Votes"
DavidMasshardt-8277 asked BrandoZhang-MSFT answered

Localize AspNetCore Identity error messages

I would like to localize the error text in the following resource file of the latest version of the AspNetCore Identity framework:

https://github.com/dotnet/aspnetcore/blob/main/src/Identity/Extensions.Core/src/Resources.resx

Sadly I only found guide how to do this for older versions of the library. Also there seams to be no official dll from Microsoft with translations to other languages. Is there a guide somewhere how to translate this resource file?

dotnet-aspnet-core-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

BrandoZhang-MSFT avatar image
0 Votes"
BrandoZhang-MSFT answered

Hi @DavidMasshardt-8277 ,

According to your description, I suggest you could try to create a custom class that implements IdentityErrorDescriber and override all default messages with reference to the shared resource file.

For example, you could create a custom Resources.resx file based on that Resources.resx. Then you could create a custom class which inherits IdentityErrorDescriber and use IStringLocalizer to localize the error message.

Details, you could refer to below codes:

You could create a class like below:

     public class MultilanguageIdentityErrorDescriber : IdentityErrorDescriber
     {
         private readonly IStringLocalizer<SharedResource> _localizer;
    
         public MultilanguageIdentityErrorDescriber(IStringLocalizer<SharedResource> localizer)
         {
             _localizer = localizer;
         }
    
         public override IdentityError DuplicateEmail(string email)
         {
             return new IdentityError()
             {
                 Code = nameof(DuplicateEmail),
                 Description = string.Format(_localizer["Email {0} is already taken."], email)
             };
         }
    
         public override IdentityError DuplicateUserName(string userName)
         {
     
             return new IdentityError()
             {
                 Code = nameof(DuplicateEmail),
                 Description = string.Format(_localizer["Email {0} is already taken."], userName)
             };
         }
     }


Then add below method inside the startup.cs identity service:

             services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                 .AddEntityFrameworkStores<ApplicationDbContext>().AddErrorDescriber<MultilanguageIdentityErrorDescriber>();


Inside the SharedResource, you could localize the Email {0} is already taken. with your different language message.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.