3

I would like to provide localized strings for Identity user name and password error messages in spanish for an asp.net core 2.1 project, since it's always showing messages in english. I tried as indicated in http://www.ziyad.info/en/articles/20-Localizing_Identity_Error_Messages but it doesn't work for me.

Thanks

Aldemar Cuartas Carvajal
  • 1,573
  • 3
  • 20
  • 39
  • have you created the resource files as described in the article? first you need to create `LocalizedIdentityErrorMessages.resx` for the neutral language and all strings should be with public access modifier. Then you need to create the Spanish resource file `LocalizedIdentityErrorMessages.es.resx` to add localized messages. (btw: that is my portal and my article :) – LazZiya Mar 13 '19 at 05:20
  • another question, do you have the rest of localization setup done in your application? this tutorial could help if you need more details: http://www.ziyad.info/en/articles/10-Developing_Multicultural_Web_Application – LazZiya Mar 13 '19 at 05:26

2 Answers2

8

I used a similar approach as in the link provided

Here is a link with details

.Net Core Identity Localization

Here are two simple steps

  1. Mention Error Describer in startup.cs
    services.AddIdentity<ApplicationUser, IdentityRole>(config =>
            {
                // Your Cookie settings
            }).AddErrorDescriber<MultilanguageIdentityErrorDescriber>()
  1. ErrorDescriber class with Language Resource injected
    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 DefaultError() { return new IdentityError { Code = nameof(DefaultError), Description = _localizer["An unknown failure has occurred."] }; }
        public override IdentityError ConcurrencyFailure() { return new IdentityError { Code = nameof(ConcurrencyFailure), Description = _localizer["Optimistic concurrency failure, object has been modified."] }; }
        public override IdentityError PasswordMismatch() { return new IdentityError { Code = nameof(PasswordMismatch), Description = _localizer["Incorrect password."] }; }
        public override IdentityError InvalidToken() { return new IdentityError { Code = nameof(InvalidToken), Description = _localizer["Invalid token."] }; }
        public override IdentityError LoginAlreadyAssociated() { return new IdentityError { Code = nameof(LoginAlreadyAssociated), Description = _localizer["A user with this login already exists."] }; }
        public override IdentityError InvalidUserName(string userName) { return new IdentityError { Code = nameof(InvalidUserName), Description = string.Format(_localizer["User name {0} is invalid, can only contain letters or digits."] , userName) }; }
        public override IdentityError InvalidEmail(string email) { return new IdentityError { Code = nameof(InvalidEmail), Description = string.Format(_localizer["Email {0} is invalid."] , email) }; }
        public override IdentityError DuplicateUserName(string userName) { return new IdentityError { Code = nameof(DuplicateUserName), Description = string.Format(_localizer["User Name {0} is already taken."] , userName) }; }
        //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 InvalidRoleName(string role) { return new IdentityError { Code = nameof(InvalidRoleName), Description = string.Format(_localizer["Role name {0} is invalid."], role) }; }
        public override IdentityError DuplicateRoleName(string role) { return new IdentityError { Code = nameof(DuplicateRoleName), Description = string.Format(_localizer["Role name {0} is already taken."], role) }; }
        public override IdentityError UserAlreadyHasPassword() { return new IdentityError { Code = nameof(UserAlreadyHasPassword), Description = _localizer["User already has a password set."] }; }
        public override IdentityError UserLockoutNotEnabled() { return new IdentityError { Code = nameof(UserLockoutNotEnabled), Description = _localizer["Lockout is not enabled for this user."] }; }
        public override IdentityError UserAlreadyInRole(string role) { return new IdentityError { Code = nameof(UserAlreadyInRole), Description = string.Format(_localizer["User already in role {0}."], role) }; }
        public override IdentityError UserNotInRole(string role) { return new IdentityError { Code = nameof(UserNotInRole), Description = string.Format(_localizer["User is not in role {0}."], role) }; }
        public override IdentityError PasswordTooShort(int length) { return new IdentityError { Code = nameof(PasswordTooShort), Description = string.Format(_localizer["Passwords must be at least {0} characters."], length) }; }
        public override IdentityError PasswordRequiresNonAlphanumeric() { return new IdentityError { Code = nameof(PasswordRequiresNonAlphanumeric), Description = _localizer["Passwords must have at least one non alphanumeric character."] }; }
        public override IdentityError PasswordRequiresDigit() { return new IdentityError { Code = nameof(PasswordRequiresDigit), Description = _localizer["Passwords must have at least one digit ('0'-'9')."] }; }
        public override IdentityError PasswordRequiresLower() { return new IdentityError { Code = nameof(PasswordRequiresLower), Description = _localizer["Passwords must have at least one lowercase ('a'-'z')."] }; }
        public override IdentityError PasswordRequiresUpper() { return new IdentityError { Code = nameof(PasswordRequiresUpper), Description = _localizer["Passwords must have at least one uppercase ('A'-'Z')."] }; }
        // DuplicateUserName, InvalidEmail, DuplicateUserName etc
    }
LazZiya
  • 5,286
  • 2
  • 24
  • 37
Ikram Shah
  • 1,206
  • 1
  • 11
  • 12
0

Create Resources folder in root and add SharedResource.resx in the folder.For example,

enter image description here Besides Ikram Shah's solution,you could also use IStringLocalizerFactory to implement it

public class LocalizedIdentityErrorDescriber : IdentityErrorDescriber
{
    private readonly IStringLocalizer _localizer;

    public SpanishIdentityErrorDescriber(IStringLocalizerFactory factory)
    {
        var type = typeof(SharedResource);
        var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);
        _localizer = factory.Create("SharedResource", assemblyName.Name);
   }
    public override IdentityError DuplicateEmail(string email)
    {
        return new IdentityError
        {
            Code = nameof(DuplicateEmail),
            Description = 
            string.Format(_localizer["Email {0} is already taken."], userName)
        };
    }

//...
Ryan
  • 19,118
  • 10
  • 37
  • 53