带有 IStringLocalizer 的 RazorPages 不工作
RazorPages with IStringLocalizer not working
我目前正在使用 Razorpages 创建多语言 Web 应用程序。
我根据 this JetBrains tutorial:
设置所有内容
我当前的设置如下所示:
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "Resources");
//services.AddMvc();
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix, options => options.ResourcesPath = "Resources")
.AddDataAnnotationsLocalization();
services
.AddRazorPages()
.AddViewLocalization();
services.Configure<RequestLocalizationOptions>(
opts =>
{
var culturesSupported = new[]
{
new CultureInfo("de"),
new CultureInfo("fr"),
new CultureInfo("it")
};
opts.DefaultRequestCulture = new RequestCulture("de");
opts.SupportedCultures = culturesSupported;
opts.SupportedUICultures = culturesSupported;
});
}
...
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRequestLocalization();
app.UseRouting();
//var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
//app.UseRequestLocalization(locOptions.Value);
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
});
}
我的剃须刀查看页面:
@page
@using Microsoft.Extensions.Localization
@model Template.Pages.EmployeeFormModel
@inject IStringLocalizer<EmployeeForm> Localizer
@{
ViewData["Title"] = "EmployeeForm";
}
@section Styles {
<link href="~/css/formPages.css" rel="stylesheet" />
}
<div class="row align-items-center">
<div class="col-md-6 offset-md-3">
<img src="~/res/pictures/pic.png" class="form-picture" />
<h1>Daten zum Neueintritt</h1>
<hr />
<p>
@Localizer["text_willkommen"]
</p>
但是在“@Inject IStringLocalizer Localizer”这一行我得到了错误:
“找不到类型或命名空间 'EmployeeForm' ...”
我的文件结构如下所示:
Project
--Pages
----EmployeeForm.cshtml
----EmployeeForm.cshtml.cs
--Resources
----Pages
------EmployeeForm.resx
------EmployeeForm.it.resx
------EmployeeForm.fr.resx
--Startup.cs
这是我第一次使用 Localisation,我按照教程进行操作,但它仍然无法正常工作,因为这个错误我什至无法构建应用程序。
@model Template.Pages.EmployeeFormModel
您的 PageModel 似乎被命名为 EmployeeFormModel
,而不是 EmployeeForm
。相应地更改定位器的类型参数:
@inject IStringLocalizer<EmployeeFormModel> Localizer
我目前正在使用 Razorpages 创建多语言 Web 应用程序。 我根据 this JetBrains tutorial:
设置所有内容我当前的设置如下所示:
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "Resources");
//services.AddMvc();
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix, options => options.ResourcesPath = "Resources")
.AddDataAnnotationsLocalization();
services
.AddRazorPages()
.AddViewLocalization();
services.Configure<RequestLocalizationOptions>(
opts =>
{
var culturesSupported = new[]
{
new CultureInfo("de"),
new CultureInfo("fr"),
new CultureInfo("it")
};
opts.DefaultRequestCulture = new RequestCulture("de");
opts.SupportedCultures = culturesSupported;
opts.SupportedUICultures = culturesSupported;
});
}
...
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRequestLocalization();
app.UseRouting();
//var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
//app.UseRequestLocalization(locOptions.Value);
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
});
}
我的剃须刀查看页面:
@page
@using Microsoft.Extensions.Localization
@model Template.Pages.EmployeeFormModel
@inject IStringLocalizer<EmployeeForm> Localizer
@{
ViewData["Title"] = "EmployeeForm";
}
@section Styles {
<link href="~/css/formPages.css" rel="stylesheet" />
}
<div class="row align-items-center">
<div class="col-md-6 offset-md-3">
<img src="~/res/pictures/pic.png" class="form-picture" />
<h1>Daten zum Neueintritt</h1>
<hr />
<p>
@Localizer["text_willkommen"]
</p>
但是在“@Inject IStringLocalizer Localizer”这一行我得到了错误: “找不到类型或命名空间 'EmployeeForm' ...”
我的文件结构如下所示:
Project
--Pages
----EmployeeForm.cshtml
----EmployeeForm.cshtml.cs
--Resources
----Pages
------EmployeeForm.resx
------EmployeeForm.it.resx
------EmployeeForm.fr.resx
--Startup.cs
这是我第一次使用 Localisation,我按照教程进行操作,但它仍然无法正常工作,因为这个错误我什至无法构建应用程序。
@model Template.Pages.EmployeeFormModel
您的 PageModel 似乎被命名为 EmployeeFormModel
,而不是 EmployeeForm
。相应地更改定位器的类型参数:
@inject IStringLocalizer<EmployeeFormModel> Localizer