So far I have been able to translate everything in an ASP.Net Core 2.1 Web Application.
It has proven a little challenge, since the scaffolded Account Pages needed a
You can use ViewModel
and than you can just use DataAnnotations
here is how Controller
, ViewModel
and View
looks in that case (translation is Croatian).
[HttpGet]
public IActionResult Login()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
var result = await signInManager.PasswordSignInAsync(
model.Email, model.Password, model.RememberMe, false);
if (result.Succeeded)
{
return RedirectToAction("index", "home");
}
ModelState.AddModelError(string.Empty, "Neuspješni pokušaj");
}
return View(model);
}
Under ErrorMessage
put your error message
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace MyApplication.ViewModels
{
public class LoginViewModel
{
[Required(ErrorMessage = "Email je obavezan")]
[EmailAddress]
public string Email { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Lozinka")]
[Required(ErrorMessage = "Lozinka je obavezna")]
public string Password { get; set; }
[Display(Name = "Zapamti me")]
public bool RememberMe { get; set; }
}
}
@model LoginViewModel
@{
ViewBag.Title = "Prijava";
}
<h2>Prijava</h2>
<div class="row">
<div class="col-md-12">
<form method="post">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="Email"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password"></label>
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<div class="checkbox">
<label asp-for="RememberMe">
<input asp-for="RememberMe" />
@Html.DisplayNameFor(m => m.RememberMe)
</label>
</div>
</div>
<button type="submit" class="btn btn-primary">Prijava</button>
</form>
</div>
</div>
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
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: