受支持的UICultures无法显示所有文化

时间:2018-08-29 19:59:46

标签: c# asp.net-mvc asp.net-core

我遵循this文档在ASP.NET Core应用程序中实现本地化,并且试图在页脚中显示包含我的应用程序支持的所有语言的选择。

因此,我根据文档建议创建了一个名为_SelectLanguagepartial的文件:

@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>

,我通过以下方式将其加载到页脚中:

@await  Html.PartialAsync("_SelectLanguagePartial")

这很好用,但是问题是我只得到italian语言,但是我应该在我指定了以下语言的Configure方法中得到英语的事实:

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

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

如您所见,我将italian设置为默认语言,但我也使用了english语言,我也尝试用en-US进行编辑,但是在选择中,我只得到了{ {1}}语言。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

对于@inject IOptions<RequestLocalizationOptions> LocOptions,它从RequestLocalizationOptions中检索区域性,您需要像下面这样在RequestLocalizationOptions中配置ConfigureServices

        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;
        });
    }

您可以通过RequestLocalizationOptions进行配置,然后在Configure中使用它,如下所示,以避免重复代码。

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