问题描述
我遵循和教程,为我的应用程序添加语言切换。
I follow the Globalization and localization and Building simple multilingual ASP.NET Core website tutorials to add a language switch for my application.
因此,我创建了部分视图
So, I created a partial view
@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();
}
<div title="@Localizer["Request culture provider:"] @requestCulture?.Provider?.GetType().Name">
<form id="selectLanguage" asp-controller="Home"
asp-action="SetLanguage" asp-route-returnUrl="@Context.Request.Path"
method="post" class="form-horizontal" role="form">
@Localizer["Language:"]
<select name="culture"
onchange="this.form.submit();"
asp-for="@requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems"></select>
</form>
</div>
添加到 _Layout
的页脚中
<div class="col-md-6 text-right">
@await Html.PartialAsync("_SelectLanguagePartial")
</div>
并配置Startup.cs,如下所示:
and configure the Startup.cs like this:
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("es"),
new CultureInfo("en")
};
options.DefaultRequestCulture = new RequestCulture(culture: "es", uiCulture: "es");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
我在其中添加了 Resources
文件夹
Views.Home.Index.es.resx
Views.Home.Index.en.resx
Views.Shared._Layout.es.resx
Views.Shared._Layout.en.resx
我也做了 Install-Package Microsoft.AspNetCore.Authentication.Cookies
加载主视图时选择西班牙语选项。但是,当我尝试选择英语时,它会加载,但会回滚西班牙语版本,因此我最终看不到英语内容。问题出在哪里?
When I load the Home View I have the Spanish option selected. But when I try to select "English" it loads, but rolls-back the Spanish version, so I can't finally see my content in English. Where is the problem?
推荐答案
您添加了JavaScript来使语言选择器回发吗?
Did you add javascript to make the language selector postback?
(function () {
$("#selectLanguage select").change(function () {
$(this).parent().submit();
});
}());
您是否在Configure中连接了本地化中间件?
Did you wire up therequest localization middleware in Configure?
var locOptions= app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
您是否在HomeController中添加了这样的方法:
Did you add a method in HomeController like this:
[HttpPost]
public IActionResult SetLanguage(string culture, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);
return LocalRedirect(returnUrl);
}
这篇关于语言在ASP.NET Core Web应用程序中未更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!