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 :)