我发现的每个示例,包括位于https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.1的有关本地化的官方Microsoft文档,都使用Controller来执行设置和保存所需区域性的操作。我的ASP.NET Core 2.1 Web应用程序不是MVC,因此没有控制器。我尝试了几种方法来解决此问题,包括在项目中添加一个虚拟Controller,但仍然无法使Culture开关起作用。
我的启动类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>
首先,没有控制器。如何在非MVC ASP.NET Core Web应用程序中完全实现此功能?
答案 0 :(得分:0)
您不必使用控制器,最近,我发布了逐步教程,以使用ASP.NET Core Razor Pages开发多文化Web应用程序;您可以在这里找到它:
http://www.ziyad.info/en/articles/10-Developing_Multicultural_Web_Application
我使用了溃败值方法,但是您可以扩展它以使用查询字符串,cookie或可接受的标头值进行区域性选择。
在网站上,您可以在github上看到实时演示和项目源代码链接。
另外,您可能还需要检查本地化身份错误消息:
http://ziyad.info/en/articles/20-Localizing_Identity_Error_Messages
我希望这会有所帮助:)
[更新]
我提供的示例使用共享资源文件。如果要使用与视图相关的资源文件批处理,请在“资源”文件夹中为每个视图/区域性创建资源文件,并使资源的文件夹结构与其相关视图相似。
例如,如果我们在页面文件夹中有一个名为“ MyView”的视图:
Pages / MyView.cshtml
资源文件应如下所示:
资源/页面/MyView.tr-TR.resx
资源/页面/MyView.ar-SY.resx
资源/页面/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
资源文件名:
资源/页面/MyViewModel.tr-TR.resx
资源/页面/MyViewModel.ar-SY.resx
资源/页面/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”视图。