A Problem That I Faced In ASP.NET Core Localization And Its Solution

Introduction

After finishing a project, it was time to apply localization. I faced a problem and I would like to share the solution here.

Definitions

  • Globalization is the process of designing apps that support different cultures. Globalization adds support for the input, display, and output of a defined set of language scripts that relate to specific geographic areas.
  • Localization is the process of adapting a globalized app, which you have already processed for localizability, to a particular culture/locale.

The problem scope

I have added the localization in the middleware as below,

public class Startup {
    public void ConfigureServices(IServiceCollection services) {
        services.AddLocalization();
    }
}

In my login page, I added the ability to choose a language from 2 options set (Arabic Or English) as shown in the image below,

A Problem That I Faced In ASP.NET Core Localization And Its Solution

A Problem That I Faced In ASP.NET Core Localization And Its Solution

In my login handling action. I called a function to specify the application culture as seen below,

public IActionResult LoginPost(EmployeeLogin EmployeeLoginModel) {
    if (LoginObj.CheckLogin(_Context, EmployeeLoginModel)) {
        SpecifyCulture();
    }
}
private void SpecifyCulture() {
    string lang = Request.Form["hdnLang"];
    //hdnLang is a hidden field in the login form
    //filled by a default value
    //and this value gets changed when selecting an option from culture drop down
    System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(lang);
    System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);
}

I have also created two resource files as seen below,

A Problem That I Faced In ASP.NET Core Localization And Its Solution

A Problem That I Faced In ASP.NET Core Localization And Its Solution

I began applying localization on a page with little content. So I,

  • Imported the namespace
  • Injected the string localization interface 

The following shows demonstration for these two steps,

@using TaskManagementSystem
@using TaskManagementSystem.Models
@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.Extensions.Localization
@inject IStringLocalizer CloseTaskLocalizer
<div class="form-group">
    <input type="submit" value="@CloseTaskLocalizer["Close"].Value" class="btn btn-default" />
    <!---onclick="return submitForm()"-->
</div> 

After applying the above steps, localization did not work as seen in the following image,

A Problem That I Faced In ASP.NET Core Localization And Its Solution

Solution

After spending a while with the code trying to solve the problem, I applied the following solution.

I went to the resource designer file and put the mouse on the definition of the method that returns the string of the button.

A Problem That I Faced In ASP.NET Core Localization And Its Solution

The thread in which HTTP request is processed holds a property of type CultureInfo.

The ResourceManager class static methods(e.g. GetString) receive the value of the property mentioned above.

Accordingly, I changed the SpecifyCulture method code in the login controller to be as follows,

private void SpecifyCulture() {
    string lang = Request.Form["hdnLang"];
    var cultureInfo = new CultureInfo(Request.Form["hdnLang"]);
    //hdnLang is a hidden field in the login form
    //filled by a default value
    //and this value gets changed when selecting an option from culture drop down
    CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
    CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
}

After running, you can see that the problem has been solved as shown below,

A Problem That I Faced In ASP.NET Core Localization And Its Solution

Hope somebody finds this helpful.