我需要在我的 View 中从资源文件中获取一些字符串。在 Startup.cs 中:
services.AddMvc()
.AddViewLocalization(options => options.ResourcesPath = "Resources");
和:
app.UseRequestLocalization(new RequestCulture("ru-RU"));
查看:
@using Microsoft.AspNet.Mvc.Localization
@inject IViewLocalizer loc
我在 MSDN 博客中阅读了以下内容:
The
IViewLocalizer
is anIHtmlLocalizer
service that looks for a resource based on the current view name.
那么我应该如何命名 .resx
文件,以便我的本地化字符串出现在我的 View 中?如果我有 Views/Manager/Index.cshtml
那么 Resources/Manager/Index.cshtml.ru-RU.resx
是正确的吗?但是没有找到资源...
最佳答案
这在文档中非常困惑,因为资源名称不是基于当前 View 名称,而是基于当前 View 路径:
https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNet.Mvc.Localization/ViewLocalizer.cs
(第 101 行)
这意味着资源名称应该这样命名:
Views.{ControllerName}.{ViewName}.cshtml.{culture code}.resx
e.g. Views.Home.About.cshtml.de-DE.resx
我已经在一个月前报告了此行为:
https://github.com/aspnet/Mvc/issues/3376
关于c# - View 中的 ASP.Net 5 rc1 本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33915565/