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

问题描述

我有一个关于 SharedResources 文件的问题.在此处的教程中对其进行了浏览:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization,我不确定我是否理解正确.

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 时,我是不是只写了 @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 似乎不起作用.

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:

视图/主页/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
});

Resources 文件夹包含名为 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要设置在根目录下项目的.就我而言,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 时,我是不是只写了 @using 并将其指向 SharedResources.cs 所在的命名空间?

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

ANSWER: 在视图中使用 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

注意 @using Microsoft.Extensions.Localization 仅当您使用 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 本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 00:49