【发布时间】:2018-06-14 07:04:00
【问题描述】:
我目前正在尝试在我的应用程序中构建本地化程序。阅读 Microsoft 文档后,我添加了所有必要的部分,因此理论上它应该可以工作。
但是在第一次启动和重新加载页面时,我只能看到按键而不是文本。
这是我的 Startup.cs
public void ConfigureServices(IServiceCollection services)
{
//...
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.AddViewLocalization()
.AddDataAnnotationsLocalization();
services.Configure<RequestLocalizationOptions>(options =>
{
var supported = new[]
{
new CultureInfo("en"),
new CultureInfo("de"),
new CultureInfo("fr")
};
options.DefaultRequestCulture = new RequestCulture(culture: "en", uiCulture: "en");
options.SupportedCultures = supported;
options.SupportedUICultures = supported;
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
/...
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
资源和观点:
在视图中:
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
...
@Localizer["Views_Contract_my_key_on_this_page"]
将参数 LanguageViewLocationExpanderFormat.SubFolder 添加到 AddViewLocalization 也不起作用
【问题讨论】:
-
您不应该在选项中的某处添加资源文件(名称)吗?
-
@Silvermind 据我所知:docs.microsoft.com/en-us/aspnet/core/fundamentals/…
标签: c# asp.net-mvc .net-core resx asp.net-core-localization