aspnetcore: Create default `IdentityErrorDescriber` for some languages
Hi, I know that we have the ability to extend IdentityErrorDescriber
and create our custom messages but shouldn’t be better if we have already some default IdentityErrorDescriber
for the most used languages? (I don’t know right now the most used languages)
This is just an issue I would like to comment because I’m translating it right now, but for sure another person is doing the same in his code. So everybody need to create a custom in each new project?
Well, if someone falls into this issue searching how to do it, follow this steps:
- Create a class with the name
CustomIdentityErrorDescriber
, copy the code below:
using Microsoft.AspNetCore.Identity;
namespace YourNamespace
{
public class CustomIdentityErrorDescriber : IdentityErrorDescriber
{
public override IdentityError DefaultError() { return new IdentityError { Code = nameof(DefaultError), Description = $"Um erro desconhecido ocorreu." }; }
public override IdentityError ConcurrencyFailure() { return new IdentityError { Code = nameof(ConcurrencyFailure), Description = "Falha de concorrência otimista, o objeto foi modificado." }; }
public override IdentityError PasswordMismatch() { return new IdentityError { Code = nameof(PasswordMismatch), Description = "Senha incorreta." }; }
public override IdentityError InvalidToken() { return new IdentityError { Code = nameof(InvalidToken), Description = "Token inválido." }; }
public override IdentityError LoginAlreadyAssociated() { return new IdentityError { Code = nameof(LoginAlreadyAssociated), Description = "Já existe um usuário com este login." }; }
public override IdentityError InvalidUserName(string userName) { return new IdentityError { Code = nameof(InvalidUserName), Description = $"Login '{userName}' é inválido, pode conter apenas letras ou dígitos." }; }
public override IdentityError InvalidEmail(string email) { return new IdentityError { Code = nameof(InvalidEmail), Description = $"Email '{email}' é inválido." }; }
public override IdentityError DuplicateUserName(string userName) { return new IdentityError { Code = nameof(DuplicateUserName), Description = $"Login '{userName}' já está sendo utilizado." }; }
public override IdentityError DuplicateEmail(string email) { return new IdentityError { Code = nameof(DuplicateEmail), Description = $"Email '{email}' já está sendo utilizado." }; }
public override IdentityError InvalidRoleName(string role) { return new IdentityError { Code = nameof(InvalidRoleName), Description = $"A permissão '{role}' é inválida." }; }
public override IdentityError DuplicateRoleName(string role) { return new IdentityError { Code = nameof(DuplicateRoleName), Description = $"A permissão '{role}' já está sendo utilizada." }; }
public override IdentityError UserAlreadyHasPassword() { return new IdentityError { Code = nameof(UserAlreadyHasPassword), Description = "Usuário já possui uma senha definida." }; }
public override IdentityError UserLockoutNotEnabled() { return new IdentityError { Code = nameof(UserLockoutNotEnabled), Description = "Lockout não está habilitado para este usuário." }; }
public override IdentityError UserAlreadyInRole(string role) { return new IdentityError { Code = nameof(UserAlreadyInRole), Description = $"Usuário já possui a permissão '{role}'." }; }
public override IdentityError UserNotInRole(string role) { return new IdentityError { Code = nameof(UserNotInRole), Description = $"Usuário não tem a permissão '{role}'." }; }
public override IdentityError PasswordTooShort(int length) { return new IdentityError { Code = nameof(PasswordTooShort), Description = $"Senhas devem conter ao menos {length} caracteres." }; }
public override IdentityError PasswordRequiresNonAlphanumeric() { return new IdentityError { Code = nameof(PasswordRequiresNonAlphanumeric), Description = "Senhas devem conter ao menos um caracter não alfanumérico." }; }
public override IdentityError PasswordRequiresDigit() { return new IdentityError { Code = nameof(PasswordRequiresDigit), Description = "Senhas devem conter ao menos um digito ('0'-'9')." }; }
public override IdentityError PasswordRequiresLower() { return new IdentityError { Code = nameof(PasswordRequiresLower), Description = "Senhas devem conter ao menos um caracter em caixa baixa ('a'-'z')." }; }
public override IdentityError PasswordRequiresUpper() { return new IdentityError { Code = nameof(PasswordRequiresUpper), Description = "Senhas devem conter ao menos um caracter em caixa alta ('A'-'Z')." }; }
}
}
- Go into your
Startup.cs
and when configuring theservices.AddIdentity
part, add the following code:
services.AddIdentity<User, IdentityRole>()
.AddErrorDescriber<CustomIdentityErrorDescriber>();
In this example, the code was translated to pt-BR
.
Would be nice if we have all those translations ready in the Identity package. The custom implementation is really nice but I don’t know if it’s just me felling that I will have to copy and paste this custom class into each project that I need to use.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 2
- Comments: 18 (4 by maintainers)
this is for dear Farsi(Persian) speakers
For anyone looking for multi language approach, see this workaround: http://stackoverflow.com/a/40801250/5338908
Are you sure this is for dotnet core?
No worries, it’s in the backlog now, but I don’t know if pt-BR would make it. We might look at how to make it extensible via 3rd party packages. The problem with community translations, is, well, sometimes words make their way in that some might find offensive, and not speaking the languages, we won’t ever notice.
Sure, I can help with that. Count with me to make the translation to
pt-BR
.Thanks for the reply.