ASP.NET 5(MVC 6)-资源本地化 [英] ASP.NET 5 (MVC 6) - Resources Localization
问题描述
我花了大约一周的时间来了解本地化在ASP.NET Core 1.0中的工作方式.我已经测试了很多选项,但无法使其正常工作.
I have spent about one week trying to understand how Localization is going to work in ASP.NET Core 1.0. I have tested a lot of options, but I can't make it working.
我已阅读有关 Visual Studio 中的错误的信息,我已经阅读了有关以下内容的所有文章现在的工作方式( Article1 , Article2 ,示例在GitHub官方存储库中.
I have read about the bug in Visual Studio, I have read all articles about how it's working right now (Article1, Article2, Article3) and I have check and tested all about the example in the Official GitHub Repository.
我的目标:
我只想使其像在ASP.NET MVC 5中一样工作.
I just want to make it works like I did in ASP.NET MVC 5.
我已经这样配置了Startup.cs:
I have configured my Startup.cs like this:
配置部分:
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();
在我的文件夹Resources中,我有.resx文件.我已经从官方示例中复制了它,但是没有办法...没有错误,只是无法正常工作.
In my folder Resources, I have my .resx files. I have copied it from the official example, but no way... No errors, just not working.
如果我测试官方Repo的Localization示例,则可以使用.但是我无法修改以适应MVC 6.
If I test the Localization example of the official Repo, it works. But I can't modified to adapt to MVC 6.
我已经在GitHub上为我的代码创建了一个存储库并对其进行测试.( https://github.com/chemitaxis/Localization.StackOverflow )
I have created a repository on GitHub for my code and test it. (https://github.com/chemitaxis/Localization.StackOverflow)
有人可以帮我吗?我认为很多人都遇到这些问题.
Can someone please help me? I think a lot of people is having these problems.
谢谢!
推荐答案
好,我解决了...明天我将在GitHub上更新我的示例.
Ok, I solved it... I will update my example on GitHub tomorrow.
我已经创建了一个 _ViewImports ,并将其添加:
I have created a _ViewImports, and add it:
@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
之后,我创建了一个 HomeController 和 Views/Home/Index.cshtml 文件.
After, I have created a HomeController and Views/Home/Index.cshtml file.
就像我在 _ViewImports 的 IStringLocalizer SR 中插入了 Views 一样,我可以使用它在Razor Views中使用它:
Like I have injected in my Views in _ViewImports the IStringLocalizer SR I can use it in my Razor Views using just:
@SR["Name"]
我不知道这是否是最好的方法,但是它有效.如果有人可以解释做到这一点的最佳方法,请回答此问题.
I don't know if it the best way to do that, but it works. If someone can explain the best way to do that, please answer this question.
完成解决方案的工作: https://github.com/chemitaxis/Localization.StackOverflow
Complete solution working: https://github.com/chemitaxis/Localization.StackOverflow
谢谢.
这篇关于ASP.NET 5(MVC 6)-资源本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!