ASP.NET 核心 IViewLocalizer 不提取我的字典查找值
ASP.NET Core IViewLocalizer Not pulling my dictionary look-up values
对此感到非常沮丧,因为我可以让它在 "Hello World" 应用程序上运行,但不能在我的实际应用程序上运行。这是我的配置方式:
配置服务:
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
}).AddViewLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
配置:
IList<CultureInfo> supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("es-ES"),
};
app.UseDefaultFiles();
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
app.UseStaticFiles();
app.UseSession();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Dashboard}/{action=Index}/{id?}");
});
_ViewImports.cshtml(添加了 taghelpers nuget pkg)
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers.Localization
/Views/Account/Login.cshtml
@inject IViewLocalizer Localizer
@{
ViewData["Title"] = "Log in";
}
<h1>@Localizer["test"]</h1>
/资源/Views/Account/Login.en-US.resx
"test" -> "result" 映射
但是当我 运行 我的网站时,Localizer 只显示密钥 "test" 而不是 "result"
我是不是在某处缺少配置?
只是在手机上简单地看到了这个,但是在 this localization docs page 我看到了:
The localization middleware must be configured before any middleware which might check the request culture (for example, app.UseMvcWithDefaultRoute()).
在您上面的示例中,您在使用 Mvc 之前使用了请求本地化,这意味着在中间件管道中,文化可能尚未设置。
如果有帮助请告诉我;想知道是不是这样。
如果您的程序集名称 != 默认命名空间,这似乎是一个问题。使它们匹配并且一切按预期工作。
来自 doc:
If your targeted class's namespace isn't the same as the assembly name you will need the full type name.
对此感到非常沮丧,因为我可以让它在 "Hello World" 应用程序上运行,但不能在我的实际应用程序上运行。这是我的配置方式:
配置服务:
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
}).AddViewLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
配置:
IList<CultureInfo> supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("es-ES"),
};
app.UseDefaultFiles();
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
app.UseStaticFiles();
app.UseSession();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Dashboard}/{action=Index}/{id?}");
});
_ViewImports.cshtml(添加了 taghelpers nuget pkg)
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers.Localization
/Views/Account/Login.cshtml
@inject IViewLocalizer Localizer
@{
ViewData["Title"] = "Log in";
}
<h1>@Localizer["test"]</h1>
/资源/Views/Account/Login.en-US.resx
"test" -> "result" 映射
但是当我 运行 我的网站时,Localizer 只显示密钥 "test" 而不是 "result"
我是不是在某处缺少配置?
只是在手机上简单地看到了这个,但是在 this localization docs page 我看到了:
The localization middleware must be configured before any middleware which might check the request culture (for example, app.UseMvcWithDefaultRoute()).
在您上面的示例中,您在使用 Mvc 之前使用了请求本地化,这意味着在中间件管道中,文化可能尚未设置。
如果有帮助请告诉我;想知道是不是这样。
如果您的程序集名称 != 默认命名空间,这似乎是一个问题。使它们匹配并且一切按预期工作。
来自 doc:
If your targeted class's namespace isn't the same as the assembly name you will need the full type name.