asp.net 没有 mvc 的核心 2.0 razor web 应用程序中的本地化
localization in asp.net core 2.0 razor web application without mvc
我找到的每个示例,包括 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.1 上有关本地化的 Microsoft 官方文档,都使用控制器来执行设置和保存所需文化的操作。我的 ASP.NET Core 2.1 网络应用程序不是 MVC,因此没有控制器。我已经尝试了几种方法来解决这个问题,包括向我的项目添加一个虚拟控制器,但我仍然无法让 Culture 开关起作用。
My Startup class Configure 方法包含以下代码:
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("hi-IN")
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture(DefaultCulture.Name),
// Formatting numbers, dates, etc.
SupportedCultures = supportedCultures,
// UI strings that we have localized.
SupportedUICultures = supportedCultures
});
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc();
ConfigureServices 方法包含此代码:
// Add the localization services to the services container
services.AddLocalization(options => options.ResourcesPath = "Resources");
// Add MVC Services to the Services Collection.
services.AddMvc()
// Add support for finding localized views, based on file name suffix, e.g. Index.fr.cshtml
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
// Add support for localizing strings in data annotations (e.g. validation messages) via the
// IStringLocalizer abstractions.
.AddDataAnnotationsLocalization();
// Configure supported cultures and localization options
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("hi-IN")
};
services.Configure<RequestLocalizationOptions>(options =>
{
// State what the default culture for your application is. This will be used if no specific culture
// can be determined for a given request.
options.DefaultRequestCulture = new RequestCulture(DefaultCulture.Name, DefaultCulture.Name);
// You must explicitly state which cultures your application supports.
// These are the cultures the app supports for formatting numbers, dates, etc.
options.SupportedCultures = supportedCultures;
// These are the cultures the app supports for UI strings, i.e. we have localized resources for.
options.SupportedUICultures = supportedCultures;
});
// Register the email service used for "contacts".
services.AddSingleton<IEmailSender, EmailSender>();
// Configure startup to use the SendGrid options.
services.Configure<AuthMessageSenderOptions>(Configuration);
// Add cross-origin resource sharing services to the specified IServiceCollection.
//
// The Policy specifed as an option will allow any method.
services.AddCors(options => options.AddPolicy("CorsPolicy", b => b.AllowAnyMethod()));
DefaultCulture 是:
DefaultCulture = new CultureInfo(Configuration["Localization:DefaultCulture"]);
其中设置文件包含字符串 "en-US"。
然后我使用本地化文档示例中的 _SelectLanguagePartial.cshtml 代码:
<div title="@Localizer["Request culture provider:"] @requestCulture?.Provider?.GetType().Name">
<form id="selectLanguage" asp-controller="Home"
asp-action="SetLanguage" asp-route-returnUrl="@returnUrl"
method="post" class="form-horizontal" role="form">
<label asp-for="@requestCulture.RequestCulture.UICulture.Name">@Localizer["Language:"]</label>
<select name="culture" onchange="this.form.submit();" asp-for="@requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems"></select>
</form>
首先,没有Controller。我如何才能在非 MVC ASP.NET 核心 Web 应用程序中实现此功能?
您不必使用控制器,最近我发布了一个 step-by-step 教程,使用 ASP.NET Core Razor Pages 开发多文化 Web 应用程序;你可以在这里找到它:
http://www.ziyad.info/en/articles/10-Developing_Multicultural_Web_Application
我使用了路由值方法,但您可以扩展它以使用查询字符串、cookie 或接受的 header 值来进行区域性选择。
在网站上,您可以在 github 上看到现场演示和项目源代码 link。
此外,您可能还需要检查本地化身份错误消息:
http://ziyad.info/en/articles/20-Localizing_Identity_Error_Messages
希望对您有所帮助:)
[更新]
我提供的示例使用的是共享资源文件。如果要使用与视图相关的资源文件方法,请在“Resources”文件夹中为每个 view/culture 创建资源文件,并保持资源的文件夹结构与其相关视图相似。
例如,如果我们在 pages 文件夹中有一个名为“MyView”的视图:
Pages/MyView.cshtml
资源文件应该如下所示:
Resources/Pages/MyView.tr-TR.resx
Resources/Pages/MyView.ar-SY.resx
Resources/Pages/MyView.hi-IN.resx
在视图中使用本地化注入 IViewLocalizer
:
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer _loc
<h4>@_loc["My view title"]</h4>
并且对于 ViewModel/DataAnnotations,为每种文化创建另一个资源文件:
查看模型:
Pages/MyViewModel.cshtml.cs
资源文件名:
Resources/Pages/MyViewModel.tr-TR.resx
Resources/Pages/MyViewModel.ar-SY.resx
Resources/Pages/MyViewModel.hi-IN.resx
用相关模型填充资源文件属性显示名称和数据注释消息,然后通过清除DataAnnotations的共享资源代码修改startup.cs文件并保持无参数:
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddViewLocalization(o=>o.ResourcesPath = "Resources")
// Option A: use this for localization with shared resource
/*
.AddDataAnnotationsLocalization(o=> {
var type = typeof(ViewResource);
var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);
var factory = services.BuildServiceProvider().GetService<IStringLocalizerFactory>();
var localizer = factory.Create("ViewResource", assemblyName.Name);
o.DataAnnotationLocalizerProvider = (t, f) => localizer;
})
*/
// Option B: use this for localization by view specific resource
.AddDataAnnotationsLocalization()
.AddRazorPagesOptions(o => {
o.Conventions.Add(new CultureTemplateRouteModelConvention());
});
顺便说一句,我更新了 GitHub 示例,现在它包含使用视图特定资源文件本地化的“AnotherPage”视图。
我找到的每个示例,包括 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.1 上有关本地化的 Microsoft 官方文档,都使用控制器来执行设置和保存所需文化的操作。我的 ASP.NET Core 2.1 网络应用程序不是 MVC,因此没有控制器。我已经尝试了几种方法来解决这个问题,包括向我的项目添加一个虚拟控制器,但我仍然无法让 Culture 开关起作用。
My Startup class Configure 方法包含以下代码:
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("hi-IN")
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture(DefaultCulture.Name),
// Formatting numbers, dates, etc.
SupportedCultures = supportedCultures,
// UI strings that we have localized.
SupportedUICultures = supportedCultures
});
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc();
ConfigureServices 方法包含此代码:
// Add the localization services to the services container
services.AddLocalization(options => options.ResourcesPath = "Resources");
// Add MVC Services to the Services Collection.
services.AddMvc()
// Add support for finding localized views, based on file name suffix, e.g. Index.fr.cshtml
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
// Add support for localizing strings in data annotations (e.g. validation messages) via the
// IStringLocalizer abstractions.
.AddDataAnnotationsLocalization();
// Configure supported cultures and localization options
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("hi-IN")
};
services.Configure<RequestLocalizationOptions>(options =>
{
// State what the default culture for your application is. This will be used if no specific culture
// can be determined for a given request.
options.DefaultRequestCulture = new RequestCulture(DefaultCulture.Name, DefaultCulture.Name);
// You must explicitly state which cultures your application supports.
// These are the cultures the app supports for formatting numbers, dates, etc.
options.SupportedCultures = supportedCultures;
// These are the cultures the app supports for UI strings, i.e. we have localized resources for.
options.SupportedUICultures = supportedCultures;
});
// Register the email service used for "contacts".
services.AddSingleton<IEmailSender, EmailSender>();
// Configure startup to use the SendGrid options.
services.Configure<AuthMessageSenderOptions>(Configuration);
// Add cross-origin resource sharing services to the specified IServiceCollection.
//
// The Policy specifed as an option will allow any method.
services.AddCors(options => options.AddPolicy("CorsPolicy", b => b.AllowAnyMethod()));
DefaultCulture 是:
DefaultCulture = new CultureInfo(Configuration["Localization:DefaultCulture"]);
其中设置文件包含字符串 "en-US"。
然后我使用本地化文档示例中的 _SelectLanguagePartial.cshtml 代码:
<div title="@Localizer["Request culture provider:"] @requestCulture?.Provider?.GetType().Name">
<form id="selectLanguage" asp-controller="Home"
asp-action="SetLanguage" asp-route-returnUrl="@returnUrl"
method="post" class="form-horizontal" role="form">
<label asp-for="@requestCulture.RequestCulture.UICulture.Name">@Localizer["Language:"]</label>
<select name="culture" onchange="this.form.submit();" asp-for="@requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems"></select>
</form>
首先,没有Controller。我如何才能在非 MVC ASP.NET 核心 Web 应用程序中实现此功能?
您不必使用控制器,最近我发布了一个 step-by-step 教程,使用 ASP.NET Core Razor Pages 开发多文化 Web 应用程序;你可以在这里找到它:
http://www.ziyad.info/en/articles/10-Developing_Multicultural_Web_Application
我使用了路由值方法,但您可以扩展它以使用查询字符串、cookie 或接受的 header 值来进行区域性选择。
在网站上,您可以在 github 上看到现场演示和项目源代码 link。
此外,您可能还需要检查本地化身份错误消息:
http://ziyad.info/en/articles/20-Localizing_Identity_Error_Messages
希望对您有所帮助:)
[更新]
我提供的示例使用的是共享资源文件。如果要使用与视图相关的资源文件方法,请在“Resources”文件夹中为每个 view/culture 创建资源文件,并保持资源的文件夹结构与其相关视图相似。
例如,如果我们在 pages 文件夹中有一个名为“MyView”的视图:
Pages/MyView.cshtml
资源文件应该如下所示:
Resources/Pages/MyView.tr-TR.resx
Resources/Pages/MyView.ar-SY.resx
Resources/Pages/MyView.hi-IN.resx
在视图中使用本地化注入 IViewLocalizer
:
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer _loc
<h4>@_loc["My view title"]</h4>
并且对于 ViewModel/DataAnnotations,为每种文化创建另一个资源文件:
查看模型:
Pages/MyViewModel.cshtml.cs
资源文件名:
Resources/Pages/MyViewModel.tr-TR.resx
Resources/Pages/MyViewModel.ar-SY.resx
Resources/Pages/MyViewModel.hi-IN.resx
用相关模型填充资源文件属性显示名称和数据注释消息,然后通过清除DataAnnotations的共享资源代码修改startup.cs文件并保持无参数:
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddViewLocalization(o=>o.ResourcesPath = "Resources")
// Option A: use this for localization with shared resource
/*
.AddDataAnnotationsLocalization(o=> {
var type = typeof(ViewResource);
var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);
var factory = services.BuildServiceProvider().GetService<IStringLocalizerFactory>();
var localizer = factory.Create("ViewResource", assemblyName.Name);
o.DataAnnotationLocalizerProvider = (t, f) => localizer;
})
*/
// Option B: use this for localization by view specific resource
.AddDataAnnotationsLocalization()
.AddRazorPagesOptions(o => {
o.Conventions.Add(new CultureTemplateRouteModelConvention());
});
顺便说一句,我更新了 GitHub 示例,现在它包含使用视图特定资源文件本地化的“AnotherPage”视图。