Localization and Globalization in ASP.NET Core MVC (.NET 5)

983 Views Asked by At

I've been Following this tutorial "https://www.youtube.com/watch?v=Hy9G30nncMM". with my own modifications.

ConfigureServices:

        services.AddLocalization(opt => { opt.ResourcesPath = "Resources"; });
        services.AddMvc().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
            .AddDataAnnotationsLocalization();
        services.AddControllersWithViews();
        

Configure:

        var supportedCultures = new[] {"en","ar"};
        var localizationOptions = new RequestLocalizationOptions()
            .SetDefaultCulture(supportedCultures[0])
            .AddSupportedCultures(supportedCultures)
            .AddSupportedUICultures(supportedCultures);

        app.UseRequestLocalization(localizationOptions);

Folder Hierarchy:

enter image description here

Result: Even that i set the default culture to supportedCultures[0] which is "en", the app gets the "ar". enter image description here

Any Help on this matter would be appreciated. Thanks in Advance.

2

There are 2 best solutions below

2
On BEST ANSWER

By default Asp.Net Core uses below RequestCultureProviders in order to detect the request culture:

So, depending on the supported cultures list provided in startup, it will try to match cultures in the list by the request culture in the providers till it find the first match and respond accordingly.

e.g.:

  • we have en and ar as supported culture list,
  • we set the ar as DefaultRequestCulture
  • we did not provide a request culture in the quesry string ?culture=xx

So the localization middleware will first try with query string, but since there is no culture defined there, it will check the next provider CookieRequestCultreProvider (below is a sample culture cookie for TR culture.)

enter image description here

and if there is no culture param in the cookie it will check the last culture provider AcceptedLanguageHeaderRequestCultureProvider which provides a list of accepted cultures.

Below you can see a screenshot of chrome supported cultures list, you may change the order of your browsers cultures and see how it affect the request culture.

enter image description here

If it can't detect the request culture in any of the above providers then it will use the DefaultRequestCulture that has been defined in RequestLocalizationOptions in startup.

In most cases we may insert RouteDataRequestCultureProvider at 0 position to provide culture in the route data, so it will be the first provider to look at during request localization:

services.Configure<RequestLocalizationOptions>(ops =>
{
    // ...
    ops.AddInitialRequestCultureProvider(new RouteDataRequestCultureProvider());
});

or

services.Configure<RequestLocalizationOptions>(ops =>
{
    // ...
    ops.RequestCultureProviders.Insert(0, new RouteDataRequestCultureProvider());
});
0
On

You can change your code like below.

 var supportedCultures = new[] { new CultureInfo("en"), new CultureInfo("ar") };

        var localizationOptions = new RequestLocalizationOptions
        {
            SupportedCultures = supportedCultures,
            SupportedUICultures = supportedCultures,
            DefaultRequestCulture = new RequestCulture("en"),
        };

        localizationOptions.RequestCultureProviders.Clear();

        localizationOptions.RequestCultureProviders.Add(new QueryStringRequestCultureProvider() { QueryStringKey = "lang" });

        app.UseRequestLocalization(localizationOptions);

Test result:

enter image description here