Skip to content

JaySkyworker/AspNetCore.Identity.Localization

Repository files navigation

AspNetCore.Identity.Localization

Build Status

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.

General Localization

Please have basic knowledge of Localization

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.2

Basic setup for Localization

See this commit.

  • In Starup.cs
    • Added RequestLocalizationOptions and supportedCultures.
    • Added app.UseRequestLocalization()
  • Added _SelectLanguagePartial.cshtml and SetLanguageController.cs for a simple UI Language selector.

View / Page / Controller Localization

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:

Model / DataAnnotation Localization

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() after AddMvc().
  • Added DataAnnotationSharedResource.resx.

ConventionalMetadataProviders

    [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 ValidationMetadataSharedResource.resx for Validation ErrorMessages.
  • Added DisplayMetadataSharedResource.resx for Display name.

Ref:

ConventionalDisplayMetadataProvider

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

ConventionalValidationMetadataProvider

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 Validation Error Localization

Identity itself validates Password settings, etc. And generates some ErrorMessages that needs to be localized.

See this commit.

  • Added LocalizedIdentityErrorDescriber.cs and LocalizedIdentityErrorMessages.resx.
  • In Starup.cs
    • Added .AddErrorDescriber<LocalizedIdentityErrorDescriber>() next to AddDefaultIdentity<IdentityUser>().

Ref:

How to contribute a translation

Language contributions are welcome!

How to submit a new translation:

  1. Fork the repo.
  2. Create resource files for the language you want to translate.
  3. Translate it (obviously).
  4. Add the new language to the supportedCultures list before zu-ZA in Startup.cs.
  5. Open a pull request.

I will do my best to integrate PR as fast as possible.

About

A Sample Asp.net Core 2.2 Mvc with Identity Project For Full Localization

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages