【发布时间】:2019-09-17 21:26:00
【问题描述】:
我有一个 .NET Core 2.2 项目
我的启动中有以下代码
配置服务方法
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
配置方式
app.UseStaticFiles();
app.UseCookiePolicy();
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("es-ES"),
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
// Formatting numbers, dates, etc.
SupportedCultures = supportedCultures,
// UI strings that we have localized.
SupportedUICultures = supportedCultures
});
app.UseStaticFiles();
app.UseStaticFiles();
app.UseMvc();
我在我的索引 Page.csHtml 中有
@page
@using Microsoft.AspNetCore.Mvc.Localization
@model IndexModel
@inject IViewLocalizer Localizer
@{
ViewData["Title"] = "Error";
}
<h3>Welcome to ASP.NET Localization</h3>
<p>This is Localization</p>
<h2>@Localizer["Overview"]</h2>
我在我的 Index.cshtml.cs 中使用以下方法
private readonly IStringLocalizer<IndexModel> Localizer;
public IndexModel(IStringLocalizer<IndexModel> localizer)
{
Localizer = localizer;
}
我已经创建了一个资源文件
\Resources\Index.es.resx
这目前有一个概览键。
我加载应用程序并尝试将文化更改为
https://localhost:44345/?culture=es-ES
但字符串并没有像我预期的那样改变!
【问题讨论】:
-
您需要更改浏览器的语言才能使代码正常工作
-
很酷,但我们希望用户能够选择?最终我需要一个下拉列表!
-
那么我认为您可以重新加载页面,以便用户看到更改
-
我正在通过将 ?culture=es-ES 添加到 url 来更改上面的 url,但它不会改变语言
-
生病检查 QueryStringRequestCultureProvider
标签: c# .net asp.net-core localization razor-pages