1
votes

I've followed this guide and I've successfully configured localization in my web application.

There's only two things that I don't understand.

Let's see some code:

Startup.cs (ConfigureServices)

services.Configure<RequestLocalizationOptions>(options =>
        {
            var supportedCultures = new[]
            {
                new CultureInfo("it-IT"),
                new CultureInfo("en-US"),
                new CultureInfo("en-GB")
            };

            options.DefaultRequestCulture = new RequestCulture(culture: "it-IT", uiCulture: "it-IT");


        });

Startup.cs (Configure)

var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);

As explained in the tutorial linked above I've create a _SelectLanguagePartial.cshtml (and added the suggested method in my controllers) to change language programmatically.

_SelectLanguagePartial.cshtml

@using Microsoft.AspNetCore.Builder
@using Microsoft.AspNetCore.Localization
@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.Extensions.Options

@inject IViewLocalizer Localizer
@inject IOptions<RequestLocalizationOptions> LocOptions

@{
   var requestCulture = Context.Features.Get<IRequestCultureFeature>();
   var cultureItems = LocOptions.Value.SupportedUICultures
    .Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName })
    .ToList();
}

<div title="@Localizer["RequestCultureProvider"] @requestCulture?.Provider?.GetType().Name">
<form id="selectLanguage"
      asp-action="SetLanguage" asp-route-returnUrl="@Context.Request.Path"
      method="post" class="form-horizontal" role="form" onchange="submit()">
    @Localizer["Language"]: <select name="culture" asp-for="@requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems" class="btn btn-default"></select>
</form>

The first problem is that, at the first startup, when there is no language cookie, the site is shown with en-US culture although I've configured it-IT as default language. Despite this I can change language correctly and if a language cookie is present the site is shown in the correct language. Why the localization framework load en-US as default language?

The second problem is that, if I change language, localization of "cultureItems" loaded in the cshtml doesn't change and remains the one loaded ad startup. What am I missing?

Thank you all in advance :)

1
For your first question, the DefaultRequestCulture property sets the default culture to use when none of the other supported cultures are available. Either remove the en-US supported culture or change your browser's culture to a non-supported culture and you'll see this in action. As for the second question, what is the value of the list of languages are you seeing? I see them as names of the languages in their native language. - Justin Saraceno
Ok for the first question but why the framework loads en-US when my browser culture is it-IT? (That is a supported culture as well). For the second question, I see the list of languages in the selected culture so, in that case, en-US. - Androidian

1 Answers

0
votes

firstly, and as you see, you do need to set the .SupportedCulture and .SupportedUICulture properties on the options object:

options.SupportedCultures = supportedCultures; options.SupportedUICultures = supportedCultures;

what I find interesting, and this behavior is sporadic, but I just removed the default request culture setting, and the names came back in their own native languages again - perhaps thats the intended behavior.

//options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");

hope this helps.