在SharedResources的帮助下进行ASP.NET Core本地化 [英] ASP.NET Core Localization with help of SharedResources

查看:204
本文介绍了在SharedResources的帮助下进行ASP.NET Core本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对SharedResources文件有疑问.在本教程中可以浏览一下:

Hi I have a question about the SharedResources file. It is glanced over in the tutorial here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization, and I'm not sure if I get it correctly.

我应该创建一个SharedResources.cs类,但是我应该将它放在哪里并且应该为空?还是需要用一些数据填充它?

I am supposed to create a SharedResources.cs class, but where should i put it and should it be empty or do I need to fill it with some data?

资源文件也一样,我应该创建一个SharedResources.da.resx文件并将所有共享字符串放在那里吗?应该去哪里?

Same goes for the resource file, should I create a SharedResources.da.resx file and put all my shared strings there? Where should it go?

当我使用IHtmlLocalizer<SharedResources>时,是否只写@using并将其指向SharedResources.cs所在的命名空间?

And when I use IHtmlLocalizer<SharedResources> do I just write @using and point it to the namespace where SharedResources.cs resides?

我尝试将SharedResources.csSharedResources.da.resx放在Resources文件夹中,并用它来将网站语言更改为丹麦语,但它不起作用.使用Index.da.resxIViewLocalizer之类的专用资源文件可以很好地工作,但是IHtmlLocalizer<SharedResources>似乎不起作用.

I tried putting SharedResources.cs and SharedResources.da.resx in the Resources folder and use it to change website language to Danish, but it does not work. Using dedicated Resource file like Index.da.resx and IViewLocalizer works fine, but IHtmlLocalizer<SharedResources> does not seem to work.

当我查看链接到页面底部的示例项目时,我没有发现使用SharedResources的任何地方,如果有人用示例更新它就好了.

When I looked at the example project linked to at the bottom of the page I didn't find any place where SharedResources is used, it would be great if somebody updated it with an example of that.

这是我尝试执行的操作:

Here's how I tried to do it:

Views/Home/Index.cshtml:

Views/Home/Index.cshtml:

@using Funkipedia.Resources
@using Microsoft.AspNetCore.Mvc.Localization
@inject IHtmlLocalizer<Shared> SharedLocalizer
...
<p>@SharedLocalizer["Hei"]</p>
...

在Startup.cs中的ConfigureServices顶部:

At top of ConfigureServices in Startup.cs:

services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
  .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
  .AddDataAnnotationsLocalization();

在Startup.cs中的配置"顶部:

At top of Configure in Startup.cs:

var supportedCultures = new List<CultureInfo>
{
       new CultureInfo("nb-NO"),
       new CultureInfo("sv-SE"),
       new CultureInfo("da-DK")
};

app.UseRequestLocalization(new RequestLocalizationOptions
{
       DefaultRequestCulture = new RequestCulture("nb-NO"),
       SupportedCultures = supportedCultures,
       SupportedUICultures = supportedCultures
});

资源文件夹包含名为Shared.csShared.da.resx的空类,其中包含共享字符串.我可能需要将其名称更改为SharedResources.csSharedResources.da.resx吗?

Resources folder contains empty class called Shared.cs and Shared.da.resx which contains shared strings. Do I maybe need to change the name of it to SharedResources.cs and SharedResources.da.resx?

推荐答案

好吧,经过一番挖掘甚至更多的尝试和错误之后,我已经找到了问题的答案并可以正常工作.这是我发现的:

Okay, after some digging around and even more trial and error I've found answers to my questions and got everything to work. Here's what I found:

我应该创建一个SharedResources.cs类,但是我应该将它放在哪里并且应该为空?还是需要用一些数据填充它?

I am supposed to create a SharedResources.cs class, but where should i put it and should it be empty or do I need to fill it with some data?

ANSWER: SharedResources.cs可以放置在项目的根文件夹或Resources文件夹中,但最重要的是名称空间应设置为项目的根目录.就我而言,namespace Funkipedia.而且它不需要包含任何数据,只需包含类声明即可.

ANSWER: SharedResources.cs can be placed in the root folder of project or in Resources folder, but the most important thing is that namespace should be set to the root of the project. In my case namespace Funkipedia. And it does not need to contain any data, just the class declaration.

资源文件也一样,我应该创建一个SharedResources.da.resx文件并将所有共享字符串放在那里吗?应该去哪里?

Same goes for the resource file, should I create a SharedResources.da.resx file and put all my shared strings there? Where should it go?

答案:是的,您需要创建一个与.cs文件相同的资源文件,并且需要将其放置在Resources文件夹中.

ANSWER: Yes, you need to create a resource file called the same as the .cs file and it needs to be put in the Resources folder.

当我使用IHtmlLocalizer<SharedResources>时,是否只写@using并将其指向SharedResources.cs所在的命名空间?

And when I use IHtmlLocalizer<SharedResources> do I just write @using and point it to the namespace where SharedResources.cs resides?

答案:在视图中使用IHtmlLocalizer和/或IStringLocalizer时,您需要在.cshtml文件的顶部编写此代码:

ANSWER: When it comes to using IHtmlLocalizer and/or IStringLocalizer in view you need to write this at the top of .cshtml file:

@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.Extensions.Localization
@inject IViewLocalizer Localizer
@inject IStringLocalizer<SharedResources> SharedLocalizer
@inject IHtmlLocalizer<SharedResources> SharedHtmlLocalizer

请注意,只有在使用IStringLocalizer

我希望这会对那些对资源文件和ASP.NET Core应用程序本地化不熟悉的人有所帮助.

I hope that this will help others who might be new to resource files and localization of ASP.NET Core applications.

这篇关于在SharedResources的帮助下进行ASP.NET Core本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆