ASP.NET Core中外部类库的本地化

时间:2017-07-18 12:51:38

标签: c# dll asp.net-core .net-core asp.net-core-webapi

我有两个项目:

  • MyWebApp - ASP.NET Core Web API
  • MyServices - .NET Core类库, 其中包含上述项目的有用服务

如何将IStringLocalizer的本地化添加到 MyServices ?哪里必须有.resx个文件?

3 个答案:

答案 0 :(得分:11)

这就是我解决它的方式。感谢Popa Andrei的回答,指引我到正确的地方。

班级图书馆

解决方案 - >右键单击 - >添加 - >新项目...... - > .Net标准 - >类库 - >我使用了名称ResourceLibrary

ResourceLibrary
|- Resources
|----- SharedResource.resx
|----- SharedResource.he.resx
|- SharedResource.cs

SharedResource.cs代码:

using Microsoft.Extensions.Localization;

namespace ResourceLibrary
{
    public interface ISharedResource
    {
    }
    public class SharedResource : ISharedResource
    {
        private readonly IStringLocalizer _localizer;

        public SharedResource(IStringLocalizer<SharedResource> localizer)
        {
            _localizer = localizer;
        }

        public string this[string index]
        {
            get
            {
                return _localizer[index];
            }
        }
    }
}

网络应用程序

右键单击webapp项目 - &gt;添加 - &gt;参考... - &gt;检查资源库

在你的webapp startup.cs中:

using ResourceLibrary;
...

public void ConfigureServices(IServiceCollection services) {
    ...
    services.AddLocalization(o => { o.ResourcesPath = "Resources"; });

    services.Configure<RequestLocalizationOptions>(options =>
            {
                CultureInfo[] supportedCultures = new[]
                {
                    new CultureInfo("en"),
                    new CultureInfo("he")
                };

                options.DefaultRequestCulture = new RequestCulture("en");
                options.SupportedCultures = supportedCultures;
                options.SupportedUICultures = supportedCultures;
             });
     ...
     }

     public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
        ...
        app.UseRequestLocalization(); //before app.UseMvc()
        ...
        }

控制器中使用的示例:

 using ResourceLibrary;
 ...

 public class ExampleController : Controller
    {
    private readonly IStringLocalizer<SharedResource> _sharedLocalizer;
    public EmailsController(IStringLocalizer<SharedResource> sharedLocalizer)
    {
        _sharedLocalizer = sharedLocalizer;
    }

    [HttpGet]
    public string Get()
    {
        return _sharedLocalizer["StringToTranslate"];
    }

查看示例:

@using Microsoft.AspNetCore.Mvc.Localization
@inject IHtmlLocalizer<ResourceLibrary.SharedResource> SharedLocalizer

<p>@SharedLocalizer["StringToTranslate"]</p>

答案 1 :(得分:3)

一个典型的解决方案是让MyServices程序集返回资源键(而不是返回要在屏幕上显示的实际资源)。您可以将.resx文件作为MyWebApp的一部分,并为每个资源键提供资源值。这样,您的MyService可以被各种UI应用程序使用,每个UI应用程序都有自己的资源表示。

另一种方法是将.resx文件保留为MyService本身的一部分。 MyWebApp可以加载其他程序集并从中读取资源文件。

另一个选择是将资源保留为新程序集,然后再从MyWebApp加载它。

检查以下SO答案以获取有关如何从其他程序集访问.resx文件的更多详细信息 -

How can I read embedded .resx in different assembly

Access strings resources from embedded .resx in dll?

How to access another assembly's .resx?

答案 2 :(得分:3)

您可以将.resx文件存储在 MyServices 项目中,并创建一种基于密钥检索资源的方法。 要从 MyServices 访问IStringLocalizer,您必须安装Microsoft.Extensions.Localization.Abstractions nuget。

基本上本地化配置必须保留在 MyWebApp (启动类)上,但在 MyServices 上,您必须添加该nuget以使用IStringLocalizer并创建一个方法,如GetResourceValueByKey(键)。可以从将引用 MyServices 项目的任何地方调用此方法。

using Microsoft.Extensions.Localization;

namespace GlobalizationLibrary { public class SharedResource:ISharedResource { private readonly IStringLocalizer _localizer;

public SharedResource(IStringLocalizer<SharedResources> localizer) { _localizer = localizer; } public string GetResourceValueByKey(string resourceKey) { return _localizer[resourceKey]; } }}