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

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

问题描述

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

UPDATE - 回复评论中的查询:

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

@inject IStringLocalizer<Messages>定位器<h2>信息 - @localizer[MVC 视图中的共享资源访问"]</h2>

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

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

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

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.

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.

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.

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

Any ideas?

解决方案

Very late answer, but it 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:

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 example, Messages.fr.resx.

In the ConfigureServices method of the 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;
    });

And in the Configure method, add app.UseRequestLocalization();

In your controller, inject IStringLocalizer<Messages> localizer, where Messages is the class 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"].

  • 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<>. 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 Views In MVC Views, the documented approach uses IViewLocalizer, but does not support resource sharing. So you can inject IStringLocalizer<> in the view for using the shared resources. For example:

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

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));
      }); 

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

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

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