gpt4 book ai didi

c# - 使用 Core 2.2 本地化的 Razor 页面不起作用

转载 作者:行者123 更新时间:2023-12-04 15:45:39 25 4
gpt4 key购买 nike

我有一个 .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();

我的 Index 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

但字符串并没有像我预期的那样改变!

最佳答案

我用的是dotnet core2.1,步骤应该是一样的。

  • 创建路由文化提供者:
public class RouteValueRequestCultureProvider : IRequestCultureProvider
{
private readonly CultureInfo[] _cultures;

public RouteValueRequestCultureProvider(CultureInfo[] cultures)
{
_cultures = cultures;
}

/// <summary>
/// get {culture} route value from path string,
/// </summary>
/// <param name="httpContext"></param>
/// <returns>ProviderCultureResult depends on path {culture} route parameter, or default culture</returns>
public Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
{
var defaultCulture = "en";

var path = httpContext.Request.Path;

if (string.IsNullOrWhiteSpace(path))
{
return Task.FromResult(new ProviderCultureResult(defaultCulture));
}

var routeValues = httpContext.Request.Path.Value.Split('/');
if (routeValues.Count() <= 1)
{
return Task.FromResult(new ProviderCultureResult(defaultCulture));
}

if (!_cultures.Any(x => x.Name.ToLower() == routeValues[1].ToLower()))
{
return Task.FromResult(new ProviderCultureResult(defaultCulture));
}

return Task.FromResult(new ProviderCultureResult(routeValues[1]));
}
}

  • RouteValueRequestCultureProvider 添加到文化提供者列表的顶部:
services.Configure<RequestLocalizationOptions>(ops =>
{
ops.DefaultRequestCulture = new RequestCulture("en");
ops.SupportedCultures = cultures.OrderBy(x=>x.EnglishName).ToList();
ops.SupportedUICultures = cultures.OrderBy(x => x.EnglishName).ToList();

// add RouteValueRequestCultureProvider to the beginning of the providers list.
ops.RequestCultureProviders.Insert(0,
new RouteValueRequestCultureProvider(cultures));
});

public class CultureTemplateRouteModelConvention : IPageRouteModelConvention
{
public void Apply(PageRouteModel model)
{
var selectorCount = model.Selectors.Count;
for (var i = 0; i < selectorCount; i++)
{
var selector = model.Selectors[i];
model.Selectors.Add(new SelectorModel
{
AttributeRouteModel = new AttributeRouteModel
{
Order = -1,
Template = AttributeRouteModel.CombineTemplates(
"{culture?}",
selector.AttributeRouteModel.Template),
}
});
}
}
}

  • 配置服务以使用文化路线模板:
services.AddMvc()
.AddRazorPagesOptions(o => {
o.Conventions.Add(new CultureTemplateRouteModelConvention());
});

查看完整教程 here .

  • 要创建语言下拉列表,您可以按照说明手动创建 here或者你可以使用 this nugget package用更少的代码自动创建它:)

关于c# - 使用 Core 2.2 本地化的 Razor 页面不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55900226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com