Code Monkey home page Code Monkey logo

aspnetcore.identity.localization's Introduction

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.

aspnetcore.identity.localization's People

Contributors

jayskyworker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

aspnetcore.identity.localization's Issues

User-specified ErrorMessage on DataTypeAttribute get overwritten by ValidationMetadataProvider

[EmailAddress(ErrorMessage = "Known issue: This will be overwritten by default localized message")]
public string Email { get; set; }

[EmailAddress]
public string Email2 { get; set; }

User-specified ErrorMessage on DataTypeAttribute (EmailAddress, Phone, FileExtensions, Url, and CreditCard) get overwritten by ValidationMetadataProvider.

this is due to ValidationAttribute.ErrorMessage for DataTypeAttribute, is not null, when no ErrorMessage was assigned ( Such as Email2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.