Translate to German Error messages from Microsoft. AspNetCore. Identity. EntityFrameworkCore
In one Web Api Net Core solition I used Microsoft.AspNetCore.Identity.EntityFrameworkCore for Authentication. It’s really comfortable to work with this package because you a free from the implementation of all the methods and don’t spare much time. It also returns the text of errors, for example, that password is to short or special character is required. For me everything was ok, but the customer wanted to see the text in German.
I was sure that there’s language pack and I thought it will be easy to make translations. But, unfortunately, it’s not so and I started to look for solutions.
Obviously, there’s no a solution out of box.
The best thing, that I found, is to override all the methods in IdentityErrorDescriber.
For that, create a class CustomIdentityErrorDescriber based on IdentityErrorDescriber. Then generate all the overrides and implement all the methods 🙂 Also you need to set your class as a value for the ‘AddErrorDescriber’ method like this:
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Password.RequiredLength = 8;
options.Password.RequireUppercase = true;
}).AddEntityFrameworkStores<MDBContext>()
.AddDefaultTokenProviders().AddErrorDescriber<CustomIdentityErrorDescriber>();
For the German translation you can use my implementation.
public class CustomIdentityErrorDescriber : IdentityErrorDescriber
{
public override IdentityError DefaultError() { return new IdentityError { Code = nameof(DefaultError), Description = $"Ein unbekannter Fehler ist aufgetreten." }; }
public override IdentityError ConcurrencyFailure() { return new IdentityError { Code = nameof(ConcurrencyFailure), Description = "Optimistischer Sperrfehler, das Objekt wurde geändert." }; }
public override IdentityError PasswordMismatch() { return new IdentityError { Code = nameof(PasswordMismatch), Description = "Falsches Passwort." }; }
public override IdentityError InvalidToken() { return new IdentityError { Code = nameof(InvalidToken), Description = "Ungültiges Token." }; }
public override IdentityError LoginAlreadyAssociated() { return new IdentityError { Code = nameof(LoginAlreadyAssociated), Description = "Ein Benutzer mit diesem Login existiert bereits." }; }
public override IdentityError InvalidUserName(string userName) { return new IdentityError { Code = nameof(InvalidUserName), Description = $"Der Login '{userName}' ist ungültig, er kann nur Buchstaben oder Zahlen enthalten." }; }
public override IdentityError InvalidEmail(string email) { return new IdentityError { Code = nameof(InvalidEmail), Description = $"Die E-Mail '{email}' ist ungültig." }; }
public override IdentityError DuplicateUserName(string userName) { return new IdentityError { Code = nameof(DuplicateUserName), Description = $"Der Login '{userName}' wird bereits verwendet." }; }
public override IdentityError DuplicateEmail(string email) { return new IdentityError { Code = nameof(DuplicateEmail), Description = $"Die E-Mail '{email}' wird bereits verwendet." }; }
public override IdentityError InvalidRoleName(string role) { return new IdentityError { Code = nameof(InvalidRoleName), Description = $"Die Rolle '{role}' ist ungültig." }; }
public override IdentityError DuplicateRoleName(string role) { return new IdentityError { Code = nameof(DuplicateRoleName), Description = $"Die Rolle '{role}' wird bereits verwendet." }; }
public override IdentityError UserAlreadyHasPassword() { return new IdentityError { Code = nameof(UserAlreadyHasPassword), Description = "Der Benutzer hat bereits ein Passwort festgelegt." }; }
public override IdentityError UserLockoutNotEnabled() { return new IdentityError { Code = nameof(UserLockoutNotEnabled), Description = "Die Sperrung ist für diesen Benutzer nicht aktiviert." }; }
public override IdentityError UserAlreadyInRole(string role) { return new IdentityError { Code = nameof(UserAlreadyInRole), Description = $"Der Benutzer hat bereits die Rolle '{role}'." }; }
public override IdentityError UserNotInRole(string role) { return new IdentityError { Code = nameof(UserNotInRole), Description = $"Der Benutzer hat nicht die Rolle '{role}'." }; }
public override IdentityError PasswordTooShort(int length) { return new IdentityError { Code = nameof(PasswordTooShort), Description = $"Passwörter müssen mindestens {length} Zeichen lang sein." }; }
public override IdentityError PasswordRequiresNonAlphanumeric() { return new IdentityError { Code = nameof(PasswordRequiresNonAlphanumeric), Description = "Passwörter müssen mindestens ein nicht alphanumerisches Zeichen enthalten." }; }
public override IdentityError PasswordRequiresDigit() { return new IdentityError { Code = nameof(PasswordRequiresDigit), Description = "Passwörter müssen mindestens eine Ziffer ('0'-'9') enthalten." }; }
public override IdentityError PasswordRequiresLower() { return new IdentityError { Code = nameof(PasswordRequiresLower), Description = "Passwörter müssen mindestens ein kleingeschriebenes Zeichen ('a'-'z') enthalten." }; }
public override IdentityError PasswordRequiresUpper() { return new IdentityError { Code = nameof(PasswordRequiresUpper), Description = "Passwörter müssen mindestens ein großgeschriebenes Zeichen ('A'-'Z') enthalten." }; }
}