单独程序集中的Asp.Net核心本地化资源 [英] Asp.Net Core Localized Resource in Separate Assembly

查看:49
本文介绍了单独程序集中的Asp.Net核心本地化资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用.NET Core的新本地化功能,但在Microsoft在此处提供的简单示例之外,

I am trying to use the new Localization features of .NET Core, but outside the simple sample Microsoft has provided here, https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization#resource-file-naming.

我的Controllers在一个单独的项目ProjectA.Controllers中,而我的共享资源类在一个公共项目ProjectB.Localization中.我已经按照文档中的说明配置了启动类.

I have my Controllers in a separate project, ProjectA.Controllers, while I have a shared resource class in a common project, ProjectB.Localization. I've configured my startup class as prescribed in the docs.

我不清楚要为资源文件命名的名称以及确切放置在何处.我已经配置了存储在目录"Resources"中的选项.是在Web项目中还是我的SharedResource类所在的ProjectB.Localization中?文档说如果是单独的程序集,则应使用完整的名称空间.因此,我将其命名为"WorldCart.Facilities.Localization.SharedResource.es.resx",并将其放置在网站的资源文件夹中.

I am unclear on what to name my resource file and where exactly to put it. I've configured the option to store in the directory "Resources". Is that in the Web project or my ProjectB.Localization where my SharedResource class is? The docs say that if it's a separate assembly, the full namespace should be used. So I've named it, "WorldCart.Facilities.Localization.SharedResource.es.resx" and placed it in the resources folder of the website.

当我运行Web应用程序并在家庭控制器中进行调试时,我没有得到翻译后的字符串,而是得到了英文版本.

When I run the web app, and debug in the home controller, I do not get a translated string, I get the english version.

有什么想法吗?

推荐答案

答案很晚,但可能会对某人有所帮助... 我遇到过类似的情况,我必须将资源文件放在单独的通用程序集中,而不是将其放在邮件web/api项目(Core 2.1)中.原因是,我可能会使用来自诸如Business或DAL层的其他程序集的本地化资源来引发警告/错误/信息消息.这就是我所做的:

Very late answer, but might help someone... I had a similar situation where I had to have the resource files in separate common assembly instead of having it inside the mail web/api project (Core 2.1). The reason being, I could be using the localized resources from other assemblies like Business or DAL layer for throwing warning/error/information messages. This is what I did:

假设我的Web项目名称空间为MyApp.Web,并且我的资源位于单独的类lib MyApp.Resources中.在资源库中,创建一个文件夹(可选),说"Messages"并创建一个类Messages.cs.在遵循命名约定的同一文件夹内创建资源文件.例如,Messages.fr.resx.

Assume that my web project namespace is MyApp.Web and my resources are in separate class lib MyApp.Resources. In the resources library, create a folder (optional), say "Messages" and create a class Messages.cs. Create the resource files inside the same folder adhering to the naming conventions. For eg, Messages.fr.resx.

在主项目的ConfigureServices方法中,添加没有任何资源路径*的本地化:

In the ConfigureServices method of main project, add the localization without any resource path*:

   services.AddLocalization();

   services.Configure<RequestLocalizationOptions>(
                opts =>
                {
                     /* your configurations*/
                    var supportedCultures = new List<CultureInfo>
                    {
                        new CultureInfo("en"),
                        new CultureInfo("fr")
                    };

                    opts.DefaultRequestCulture = new RequestCulture("en", "en");
                    // Formatting numbers, dates, etc.
                    opts.SupportedCultures = supportedCultures;
                    // UI strings that we have localized.
                    opts.SupportedUICultures = supportedCultures;
                });

然后在Configure方法中,添加app.UseRequestLocalization();

在您的控制器中,注入IStringLocalizer<Messages> localizer,其中Messages是您在资源库中创建的类.您所有的本地化资源将在localizer对象(即localizer["your key or default text"])中可用.

In your controller, inject IStringLocalizer<Messages> localizer, where Messages is the class which you created in the Resources library. All your localized resources will be available in the localizer object, i.e., localizer["your key or default text"].

  • services.AddLocalization();选项中未添加任何ResourcePath的原因是由于资源文件(Messages.fr.resx)和虚拟类(Messages.cs)都位于同一路径中.框架将检查相对于我们在IStringLocalizer<>中指定的类的资源文件.如果Messages.cs位于MyApp.Resources lib的根文件夹中,而资源文件位于文件夹"xyz"内,则配置应该为services.AddLocalization(ops => ops.ResourcesPath = "xyz");
  • The reason for not adding any ResourcePath in the services.AddLocalization(); options is due to the reason that both the resource files (Messages.fr.resx) and the dummy class (Messages.cs) are in the same path. The framework will check for the resource file relative to the class which we have specified in IStringLocalizer<>. So had if the Messages.cs was in the root folder of MyApp.Resources lib and the resource files were inside folder "xyz", then the configuration should be services.AddLocalization(ops => ops.ResourcesPath = "xyz");

更新-响应评论中的查询:

UPDATE - Responding to the queries in the comments:

MVC视图 在MVC视图中,已记录的方法是使用IViewLocalizer,但不支持资源共享.因此,您可以在视图中注入IStringLocalizer<>以使用共享资源.例如:

MVC Views In MVC Views, the documented approach is to use IViewLocalizer, but does not support resource sharing. So you can inject IStringLocalizer<> in the view for using the shared resources. For eg:

@inject IStringLocalizer<Messages> localizer
<h2>Information - @localizer["Shared resource access in MVC Views"]</h2>

数据注释 为了在数据注释中使用共享资源,可以在服务中使用factory方法:

Data Annotations In order to use shared resources in the data annotations, you can use the factory method in the service:

services.AddMvc()
          .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
          .AddDataAnnotationsLocalization(options => {
              options.DataAnnotationLocalizerProvider = (type, factory) =>
                  factory.Create(typeof(Messages));
          }); 

其中typeof(Messages)中的Messages是您的共享资源虚拟类.

where the Messages in the typeof(Messages) is your shared resource dummy class.

这篇关于单独程序集中的Asp.Net核心本地化资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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