gpt4 book ai didi

c# - SupportedUICultures 不显示所有文化

转载 作者:行者123 更新时间:2023-11-30 22:59:37 24 4
gpt4 key购买 nike

我正在关注 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 中编辑但在选择中我只得到 italian语言。

我做错了什么?

最佳答案

对于 @inject IOptions<RequestLocalizationOptions> LocOptions , 它从 RequestLocalizationOptions 中检索文化, 你需要配置 RequestLocalizationOptionsConfigureServices如下所示:

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

关于c# - SupportedUICultures 不显示所有文化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52085239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com