How to translate Identity Password validation messages
These error messages are generated using IdentityErrorDescriber
. Here's a sample of what the class itself looks like:
public class IdentityErrorDescriber
{
...
public virtual IdentityError PasswordTooShort(int length)
{
return new IdentityError
{
Code = nameof(PasswordTooShort),
Description = Resources.FormatPasswordTooShort(length)
};
}
...
}
To customise a specific message, create your own IdentityErrorDescriber
implementation. Here's an example:
public class MyIdentityErrorDescriber : IdentityErrorDescriber
{
public override IdentityError PasswordTooShort(int length)
{
return new IdentityError
{
Code = nameof(PasswordTooShort),
Description = "Your description goes here."
};
}
}
To use this new implementation, add it to the DI container in Startup.ConfigureServices
:
services.AddScoped<IdentityErrorDescriber, MyIdentityErrorDescriber>();
This can be done by localizing identity error messages, there are 22 message that has to be localized.
First, create a shared resource file "its keys defined with public access modifier" and type all error messages with localized versions as in the below image:
then create a new class that implements IdentityErrorDescriber
and override all default messages with reference to the shared resource file; in this sample the shared resource file name is LocalizedIdentityErrorMessages:
public class LocalizedIdentityErrorDescriber : IdentityErrorDescriber
{
public override IdentityError DuplicateEmail(string email)
{
return new IdentityError
{
Code = nameof(DuplicateEmail),
Description = string.Format(LocalizedIdentityErrorMessages.DuplicateEmail, email)
};
}
public override IdentityError DuplicateUserName(string userName)
{
return new IdentityError
{
Code = nameof(DuplicateUserName),
Description = string.Format(LocalizedIdentityErrorMessages.DuplicateUserName, userName)
};
}
public override IdentityError InvalidEmail(string email)
{
return new IdentityError
{
Code = nameof(InvalidEmail),
Description = string.Format(LocalizedIdentityErrorMessages.InvalidEmail, email)
};
}
public override IdentityError DuplicateRoleName(string role)
{
return new IdentityError
{
Code = nameof(DuplicateRoleName),
Description = string.Format(LocalizedIdentityErrorMessages.DuplicateRoleName, role)
};
}
public override IdentityError InvalidRoleName(string role)
{
return new IdentityError
{
Code = nameof(InvalidRoleName),
Description = string.Format(LocalizedIdentityErrorMessages.InvalidRoleName, role)
};
}
public override IdentityError InvalidToken()
{
return new IdentityError
{
Code = nameof(InvalidToken),
Description = LocalizedIdentityErrorMessages.InvalidToken
};
}
public override IdentityError InvalidUserName(string userName)
{
return new IdentityError
{
Code = nameof(InvalidUserName),
Description = string.Format(LocalizedIdentityErrorMessages.InvalidUserName, userName)
};
}
public override IdentityError LoginAlreadyAssociated()
{
return new IdentityError
{
Code = nameof(LoginAlreadyAssociated),
Description = LocalizedIdentityErrorMessages.LoginAlreadyAssociated
};
}
public override IdentityError PasswordMismatch()
{
return new IdentityError
{
Code = nameof(PasswordMismatch),
Description = LocalizedIdentityErrorMessages.PasswordMismatch
};
}
public override IdentityError PasswordRequiresDigit()
{
return new IdentityError
{
Code = nameof(PasswordRequiresDigit),
Description = LocalizedIdentityErrorMessages.PasswordRequiresDigit
};
}
public override IdentityError PasswordRequiresLower()
{
return new IdentityError
{
Code = nameof(PasswordRequiresLower),
Description = LocalizedIdentityErrorMessages.PasswordRequiresLower
};
}
public override IdentityError PasswordRequiresNonAlphanumeric()
{
return new IdentityError
{
Code = nameof(PasswordRequiresNonAlphanumeric),
Description = LocalizedIdentityErrorMessages.PasswordRequiresNonAlphanumeric
};
}
public override IdentityError PasswordRequiresUniqueChars(int uniqueChars)
{
return new IdentityError
{
Code = nameof(PasswordRequiresUniqueChars),
Description = string.Format(LocalizedIdentityErrorMessages.PasswordRequiresUniqueChars, uniqueChars)
};
}
public override IdentityError PasswordRequiresUpper()
{
return new IdentityError
{
Code = nameof(PasswordRequiresUpper),
Description = LocalizedIdentityErrorMessages.PasswordRequiresUpper
};
}
public override IdentityError PasswordTooShort(int length)
{
return new IdentityError
{
Code = nameof(PasswordTooShort),
Description = string.Format(LocalizedIdentityErrorMessages.PasswordTooShort, length)
};
}
public override IdentityError UserAlreadyHasPassword()
{
return new IdentityError
{
Code = nameof(UserAlreadyHasPassword),
Description = LocalizedIdentityErrorMessages.UserAlreadyHasPassword
};
}
public override IdentityError UserAlreadyInRole(string role)
{
return new IdentityError
{
Code = nameof(UserAlreadyInRole),
Description = string.Format(LocalizedIdentityErrorMessages.UserAlreadyInRole, role)
};
}
public override IdentityError UserNotInRole(string role)
{
return new IdentityError
{
Code = nameof(UserNotInRole),
Description = string.Format(LocalizedIdentityErrorMessages.UserNotInRole, role)
};
}
public override IdentityError UserLockoutNotEnabled()
{
return new IdentityError
{
Code = nameof(UserLockoutNotEnabled),
Description = LocalizedIdentityErrorMessages.UserLockoutNotEnabled
};
}
public override IdentityError RecoveryCodeRedemptionFailed()
{
return new IdentityError
{
Code = nameof(RecoveryCodeRedemptionFailed),
Description = LocalizedIdentityErrorMessages.RecoveryCodeRedemptionFailed
};
}
public override IdentityError ConcurrencyFailure()
{
return new IdentityError
{
Code = nameof(ConcurrencyFailure),
Description = LocalizedIdentityErrorMessages.ConcurrencyFailure
};
}
public override IdentityError DefaultError()
{
return new IdentityError
{
Code = nameof(DefaultError),
Description = LocalizedIdentityErrorMessages.DefaultIdentityError
};
}
}
finally, add the localized error describer to identity setup under ConfigureServices method in startup class:
services.AddIdentity<AppUser, AppRole>()
// localize identity error messages
.AddErrorDescriber<LocalizedIdentityErrorDescriber>()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
resource: http://www.ziyad.info/en/articles/20-Localizing_Identity_Error_Messages
Additionally you may need to read the step-by-step localization articles: http://www.ziyad.info/en/articles/10-Developing_Multicultural_Web_Application
UPDATE - Dec. 2020
Recently I've developed a new nuget package (XLocalizer), it simplifies the localization setup of Asp.Net Core web apps, it supports auto online translation and auto resource creating. Additionaly, all identity errors, model binding errors and validation errors can be customized easily in a json file.
References:
- XLocalizer Docs: https://docs.ziyad.info
- XLocalizer Repo: https://github.com/LazZiya/XLocalizer
- Tutorial: https://medium.com/swlh/xlocalizer-for-asp-net-core-1185b6b9458c