It is quick and simple to create an english web app with Identity using Asp.Net Core Mvc 2.2 . However, if you want to develop a non-english app, you will find many obstacles prevent you to have the same experiences.
With many project demonstrate how to localized it, we need a more complete solution for most of the issues, especially for error messages.
Enjoy!
NOTE: Not all UI text are localized!
Some less essential pages are not localized, You should use the same method to localize it.
Please have basic knowledge of Localization
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.2
See this commit.
- In
Starup.cs
- Added
RequestLocalizationOptions
andsupportedCultures
. - Added
app.UseRequestLocalization()
- Added
- Added
_SelectLanguagePartial.cshtml
andSetLanguageController.cs
for a simple UI Language selector.
There are many ways to Localized these general text, I choose to use original english text as Key to the resources for quick english fall back.
See this commit and this commit .
Ref:
For Models and DataAnnotations with assigned text, Ex:
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
Use DataAnnotationsLocalization
to localize it,
However this does NOT localize default display name and default ErrorMessages.
I will cover it in next section.
See this commit.
- In
Starup.cs
- Added
.AddDataAnnotationsLocalization()
afterAddMvc()
.
- Added
- Added
DataAnnotationSharedResource.resx
.
[Required]
[EmailAddress]
public string Email { get; set; }
Default Display name for Email
and
Default ErrorMessages for [Required]
, [EmailAddress]
and etc.
can be localized by .ModelMetadataDetailsProviders
See this commit , this commit and this commit .
- In
Starup.cs
- Added
ConventionalDisplayMetadataProvider
,ConventionalValidationMetadataProvider
and related classes. - In Mvc Options, Added
options.SetConventionalMetadataProviders()
to Init.
- Added
- Added
ValidationMetadataSharedResource.resx
for Validation ErrorMessages. - Added
DisplayMetadataSharedResource.resx
for Display name.
Ref:
For Display name, the provider is based on conventions and tries to find resource key based on the following naming scheme:
[[Namespace_]TypeName_]PropertyName
The provider will search resource key from more specific to less, stopping on the first where it succeeds:
App_Areas_Identity_Pages_Account_LoginModel_InputModel_Email
Areas_Identity_Pages_Account_LoginModel_InputModel_Email
Identity_Pages_Account_LoginModel_InputModel_Email
- ...
LoginModel_InputModel_Email
InputModel_Email
Email
For Default Validation ErrorMessages, same rule applied:
[[[Namespace_]TypeName_]PropertyName_]ValidatorType
The provider will search resource key from more specific to less, stopping on the first where it succeeds:
App_Areas_Identity_Pages_Account_LoginModel_InputModel_Email_Required
Areas_Identity_Pages_Account_LoginModel_InputModel_Email_Required
Identity_Pages_Account_LoginModel_InputModel_Email_Required
- ...
InputModel_Email_Required
Email_Required
Required
You should keep one message for each validation type for Default ErrorMessages. See DefaultValidationMessages.resx
Identity itself validates Password settings, etc. And generates some ErrorMessages that needs to be localized.
See this commit.
- Added
LocalizedIdentityErrorDescriber.cs
andLocalizedIdentityErrorMessages.resx
. - In
Starup.cs
- Added
.AddErrorDescriber<LocalizedIdentityErrorDescriber>()
next toAddDefaultIdentity<IdentityUser>()
.
- Added
Ref:
Language contributions are welcome!
How to submit a new translation:
- Fork the repo.
- Create resource files for the language you want to translate.
- Translate it (obviously).
- Add the new language to the
supportedCultures
list beforezu-ZA
inStartup.cs
. - Open a pull request.
I will do my best to integrate PR as fast as possible.