【发布时间】:2018-09-11 17:05:05
【问题描述】:
我发现的每个示例,包括位于https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.1 的有关本地化的官方 Microsoft 文档,都使用控制器来执行设置和保存所需文化的操作。我的 ASP.NET Core 2.1 Web 应用程序不是 MVC,因此没有控制器。我尝试了几种方法来解决这个问题,包括在我的项目中添加一个虚拟控制器,但我仍然无法让文化开关工作。
我的 Startup 类的 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 应用中实现此功能?
【问题讨论】:
-
你的问题真的没有意义。您的网络应用需要 something 来处理路由。那是控制器或 Razor 页面。无论哪种情况,文档采用的方法大致相同。
标签: asp.net razor asp.net-core localization asp.net-core-localization