Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SupportedUICultures doesn't display all the culture

I following this documentation to implement the localization inside my ASP.NET Core application, and I'm trying to display in the footer a select which contains all the languages supported by my app.

So I created a file called _SelectLanguagepartial as suggested by the documentation:

@using Microsoft.AspNetCore.Builder
@using Microsoft.AspNetCore.Http.Features
@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();
    var returnUrl = string.IsNullOrEmpty(Context.Request.Path) ? "~/" : $"~{Context.Request.Path.Value}";
}

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

and I load this in the footer in the following way:

@await  Html.PartialAsync("_SelectLanguagePartial")

this works well, but the problem is that I get only the italian language, but I should get instead also the english, infact, inside the Configure method I specified these languages:

var supportedCultures = new[]
{
    new CultureInfo("it-IT"),
    new CultureInfo("en-EN")
};

app.UseRequestLocalization(new RequestLocalizationOptions
{
    DefaultRequestCulture = new RequestCulture("it-IT"),
    SupportedCultures = supportedCultures,
    SupportedUICultures = supportedCultures
});

as you can see I set as default language the italian but I have also the english language, I tried also to edit in en-US but in the select I get only the italian language.

What I did wrong?

like image 476
Charanoglu Avatar asked Aug 29 '18 19:08

Charanoglu


1 Answers

For @inject IOptions<RequestLocalizationOptions> LocOptions, it retrives cultures from RequestLocalizationOptions, you need to configure the RequestLocalizationOptions in ConfigureServices like below:

        public void ConfigureServices(IServiceCollection services)
    {
        services.AddLocalization(options => options.ResourcesPath = "Resources");
        services.Configure<CookiePolicyOptions>(options =>
        {
            //rest code

        services.AddMvc()
                .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

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

            // State what the default culture for your application is. This will be used if no specific culture
            // can be determined for a given request.
            options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");

            // You must explicitly state which cultures your application supports.
            // These are the cultures the app supports for formatting numbers, dates, etc.
            options.SupportedCultures = supportedCultures;

            // These are the cultures the app supports for UI strings, i.e. we have localized resources for.
            options.SupportedUICultures = supportedCultures;
        });
    }

You could configure by RequestLocalizationOptions and then use it in Configure like below to avoid repeat code.

var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
        app.UseRequestLocalization(locOptions.Value);
like image 124
Edward Avatar answered Oct 23 '22 12:10

Edward