我在this文档之后在我的应用程序中实现了本地化,实际上当我通过选择框更改语言时遇到了问题,事实上,当我选择英语时,应用程序仍然使用意大利语。
当我改变语言似乎应用程序执行刷新但语言总是设置为意大利语时,奇怪的是,如果我在url中添加一个查询字符串,其中包含此http://localhost:5000/?culture=en
,那么应用程序将以英语和所选项目运行在选择中也将设置为英语。
在ConfigureService
方法内,我添加了以下内容:
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("it-IT"),
new CultureInfo("en")
};
options.DefaultRequestCulture = new RequestCulture("it-IT");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders = new List<IRequestCultureProvider>
{
new QueryStringRequestCultureProvider(),
new CookieRequestCultureProvider()
};
});
所以你可以看到我只有两种语言:english, italian
他们都有文件夹.resx
中可用的Resources
文件,具体来说,我在正确的文件夹中为每个Controller
添加了文件:
然后在Configure
方法中我添加了这个:在app.UseRequestLocalization();
之前的UseMvc
,如文档中所示。
然后我在View
文件夹中创建了一个名为Shared
的_SelectLanguagePartial.cshtml
,这个View
包含以下内容:
@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();
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>
我在View
页脚加载这个_Layout.cshtml
,其中包含该网站的基本html
:@await Html.PartialAsync("_SelectLanguagePartial")
。
现在我还创建了另一个名为LanguageController
的控制器,其中包含SetLanguage
方法,每当我从select
中的footer
中选择一个项目时它就会被调用
public class LanguageController : Controller
{
public IActionResult Index()
{
return View();
}
[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);
}
}
当我选择英语时,变量culture
包含en
,但该网站始终显示意大利语。
我错过了什么?
UPDATE
这实际上是SelectLanguage
方法的变量内容:
正如你所看到的那样,文化被正确传递,在执行这个方法之后有一个刷新,但是我得到的是英语而不是英语。
更新2
这是我做的操作流程:
对于您当前的设置,您需要执行以下操作:
public class HomeController : Controller
{
private readonly IStringLocalizer<HomeController> _localizer;
public HomeController(IStringLocalizer<HomeController> localizer)
{
_localizer = localizer;
}
public IActionResult Index()
{
ViewData["Title"] = _localizer["Title"];//"Your application description page.";
return View();
}
然后,在视图中使用ViewData["Title"]
来访问licalizedred内容。
如果要在视图中访问如下所示的本地化程序:
@inject IViewLocalizer Localizer
<label>@Localizer["Title"]</label>
您需要创建如下的View localizer:
从来没有像我说的那样,我不是ASP.NET Core
专家,我现在正在学习它,但问题不是由我的问题中发布的代码引起的,而是由此引起的:
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
“bug”就行了:options.CheckConsentNeeded = context => true;
我不知道为什么这会阻止Append
在这种情况下与语言相关的Cookie
,也许这是一个错误?无论如何,我评论了这行代码,但如果有人可以为此提供解释,我会很高兴。