我目前正在尝试在我的应用程序中构建一个本地化程序。阅读完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
也无法正常工作答案 0 :(得分:0)
我找到了解决方案:
快乐编码!