gpt4 book ai didi

asp.net-core - ASP.NET 5 (MVC 6) - 资源本地化

转载 作者:行者123 更新时间:2023-12-04 08:23:03 24 4
gpt4 key购买 nike

我花了大约一周的时间试图了解本地化将如何在 ASP.NET Core 1.0 中工作。我已经测试了很多选项,但我无法使其工作。

我已经阅读了 Visual Studio 中的错误。 ,我已经阅读了所有关于它现在如何工作的文章(Article1Article2Article3)并且我已经检查并测试了所有关于 example 的内容。在官方 GitHub 存储库中。

我的目标:

我只是想让它像在 ASP.NET MVC 5 中一样工作。

我已经像这样配置了我的 Startup.cs:

配置部分:

var requestLocalizationOptions = new RequestLocalizationOptions
{
// Set options here to change middleware behavior
SupportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("es-ES")
},
SupportedUICultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("es-ES")

}
};

app.UseRequestLocalization(requestLocalizationOptions, defaultRequestCulture: new RequestCulture("en-US"));

配置服务部分:
// Add MVC services to the services container.
services
.AddMvc()
.AddViewLocalization(options => options.ResourcesPath = "Resources")
.AddDataAnnotationsLocalization();

在我的资源文件夹中,我有我的 .resx 文件。我已经从官方示例中复制了它,但没办法......没有错误,只是不工作。

如果我测试官方 Repo 的本地化示例,它可以工作。但我无法修改以适应 MVC 6。

我在 GitHub 上为我的代码创建了一个存储库并对其进行了测试。 ( https://github.com/chemitaxis/Localization.StackOverflow)

有人可以帮帮我吗?我想很多人都有这些问题。

谢谢!!

最佳答案

好的,我解决了...我明天将在 GitHub 上更新我的示例。

我创建了一个 _ViewImports ,并添加它:

@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
@using System.Threading.Tasks
@using AspNet5Localization
@using AspNet5Localization.Resources
@using Microsoft.AspNet.Mvc.Localization
@using Microsoft.Extensions.Localization
@inject IStringLocalizer<AmazingResource> SR

之后,我创建了一个 首页 Controller View /主页/Index.cshtml 文件。

就像我在 中注入(inject)的 View 一样_ViewImports IStringLocalizer SR 我可以在 Razor Views 中使用它,只需:
@SR["Name"]

我不知道这是否是最好的方法,但它确实有效。如果有人可以解释最好的方法,请回答这个问题。

完整的解决方案工作: https://github.com/chemitaxis/Localization.StackOverflow

谢谢。

关于asp.net-core - ASP.NET 5 (MVC 6) - 资源本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35433059/

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