Express Localization

Localizing web apps has never been easy as with LazZiya.ExpressLocalization

ExpressLocalization

Express localization settings for Asp.NetCore 2.x. All below localization settings in one clean step:

  • Global route template: Add {culture} paramter to all routes
  • RouteValueRequestCultureProvider : register route value request culture provider
  • ViewLocalization : shared resource for localizing all razor pages
  • DataAnnotations Localization : All data annotations validation messages and display names attributes localization
  • ModelBinding Localization : localize model binding error messages
  • IdentityErrors Localization : localize identity describer error messages
  • Client Side Validation : include all client side libraries for validating localized input fields

Installation

Install-Package LazZiya.ExpressLocalization -Version 2.0.0

it will install LazZiya.TagHelpers v2.2.0 package as well, it is necessary for adding client side validation libraries for localized input fields like decimal numbers.

How to use

  • Install from nuget as mention above
  • Relevant localization resource files are available in LazZiya.ExpressLocalization.Resources repo. Download the resources project and reference it to your main web project, or just create you own resource files with the relevant key names as in ExpressLocalizationResource.tr.resx file.
  • In your main project' startup.cs file, define supported cultures list then add express localization setup in one step or customized steps as mentioned below

One step setup:

This step will add all localization settings :

//add reference to :
using LazZiya.ExpressLocalization;

//setup express localization under ConfigureServices method:
public void ConfigureServices(IServiceCollection services)
{
    //other configuration settings....
    
    var cultures = new CultureInfo[]
    {
        new CultureInfo("en"),
        new CultureInfo("tr"),
        new CultureInfo("ar")
    };

    services.AddMvc()
        //ExpressLocalizationResource and ViewLocalizationResource are available in :
        // https://github.com/LazZiya/ExpressLocalizationSample
        .AddExpressLocalization<ExpressLocalizationResource, ViewLocalizationResource>(
            exOps =>
            {
                exOps.RequestLocalizationOptions = ops =>
                {
                    ops.SupportedCultures = cultures;
                    ops.SupportedUICultures = cultures;
                    ops.DefaultRequestCulture = new RequestCulture("en");
                };
            })
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

//configure the app to use RequestLocalizationMiddleware :
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    //other codes...
    
    //add localization middleware to the app
    app.UseRequestLocalization();

    app.UseMvc();
}

 

Customized steps (optional)

If you don't need all settings in one step, you can use below methods for manually configuring localizaton steps. For example if you need to provide separate localization resouce files for each of DataAnnotations, Identity and ModelBinding:

//configure request localizaton options
services.Configure<RequestLocalizationOptions>(
    ops =>
    {
        ops.SupportedCultures = cultures;
        ops.SupportedUICultures = cultures;
        ops.DefaultRequestCulture = new RequestCulture("en");
    });
    
services.AddMvc()
    //add view localization
    .AddViewLocalization()
    
    //register route value request culture provider, 
    //and add route parameter {culture} at the beginning of every url
    .ExAddRouteValueRequestCultureProvider(cultures, "en")

    //add shared view localization, 
    //use by injecting SharedCultureLocalizer to the views as below:
    //@inject SharedCultureLocalizer _loc
    //_loc.Text("Hello world")
    .ExAddSharedCultureLocalizer<ViewLocalizationResource>()

    //add DataAnnotations localization
    .ExAddDataAnnotationsLocalization<DataAnnotationsResource>()

    //add ModelBinding localization
    .ExAddModelBindingLocalization<ModelBindingResource>()

    //add IdentityErrors localization
    .ExAddIdentityErrorMessagesLocalization<IdentityErrorsResource>()
    
    //add client side validation libraries for localized inputs
    .ExAddClientSideLocalizationValidationScripts()

    .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

 

Notic: if you are creating your own resource files, the relevant key names must be defined as in ExpressLocalizationResource file.

DataAnnotations

All system data annotations error messages are defined in ExpressLocalizationResource. You can add your own localized texts to the same file.

For easy access there is a struct with all pre-defined validation messages can be accessed as below:

using LazZiya.ExpressLocalization.Messages

public class MyModel
{
    [Required(ErrorMessage = DataAnnotationsErrorMessages.RequiredAttribute_ValidationError)]
    [StringLength(maximumLength: 25, 
                ErrorMessage = DataAnnotationsErrorMessages.StringLengthAttribute_ValidationErrorIncludingMinimum, 
                MinimumLength = 3)]
    [Display(Name = "Name")]
    public string Name { get; set; }
}

 

View localization

Option 1

Use Localize tag helper for localizing views easily:

<localize>Home page</localize>

 

For more details see : LocalizeTagHelper

Option 2

  • inject shared culture localizer directly to the view or to _ViewImports.cshtml :
@using LazZiya.ExpressLocalization
@inject SharedCultureLocalizer _loc

 

  • call localization function to get localized strings in views:
    <h1 class="display-4">@_loc.Text("Welcome")</h1>

     

Client side validation libraries

All required libraries to valdiate localized inputs like decimal numbers

  • register TagHelpers in _ViewImports.cshtml :
@using LazZiya.TagHelpers
@addTagHelper *, LazZiya.TagHelpers

 

  • add tag helper to the view to validate localized input:
<localization-validation-scripts></localization-validation-scripts>

For more details see LazZiya.TagHelpers v2.2.0

Sample project

See this sample project : https://github.com/LazZiya/ExpressLocalizationSample

 

More

To easily create a language navigation dropdown for changing the culture use LazZiya.TagHelpers