【发布时间】:2017-05-08 16:44:53
【问题描述】:
我正在尝试构建一个应该以英语和德语提供的 ASP.NET Core 应用程序。我的问题是IViewLocalizer 总是返回德语文本,即使文化设置为英语。如何获得适合当前文化的文本?
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(opt => opt.ResourcesPath = "Resources");
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
var cultures = new[] { new CultureInfo("en"), new CultureInfo("de") };
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en"),
SupportedCultures = cultures,
SupportedUICultures = cultures
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
HomeController.cs
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
索引.cshtml
<!DOCTYPE html>
@using Microsoft.AspNetCore.Mvc.Localization;
@inject IViewLocalizer Localizer
<html>
<body>
<h1>@Localizer["Hello, World!"]</h1>
<ul>
<li>CurrentCulture: @System.Globalization.CultureInfo.CurrentCulture</li>
<li>CurrentUICulture: @System.Globalization.CultureInfo.CurrentUICulture</li>
</ul>
</body>
</html>
资源文件位于Resources\Views.Home.Index.de.resx
预期输出:
你好世界! CurrentCulture: zh CurrentUICulture: zh页面输出:
你好世界! CurrentCulture: zh CurrentUICulture: zh请求标头:
GET / HTTP/1.1
Host: localhost:61904
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,es;q=0.6,de;q=0.4
【问题讨论】:
-
你能分享一下这个请求是什么样子的吗?
-
我在github.com/aspnet/Localization/issues/277 上打开了一个问题,但我们需要重现。你能提供吗?
-
见github.com/aspnet/Localization/issues/277 没有repro,它似乎工作正常。
-
我在 Github 上提供了一些复制代码:github.com/christianheld/ResourceManagerIssue
标签: c# localization asp.net-core asp.net-core-mvc