Localization in external class libraries in ASP.NET Core
Asked Answered
M

3

29

I have two projects:

  • MyWebApp - ASP.NET Core Web API
  • MyServices - .NET Core class library, which contains helpful services for project above

How can I add localization with IStringLocalizer to MyServices? Where must be .resx files located?

Malemute answered 18/7, 2017 at 12:51 Comment(0)
S
29

This is how I solved it. Thanks to Popa Andrei answer for directing me to the right place.

Class Library

Solution -> right click -> Add -> New Project ... -> .Net standard -> Class Library -> I used the name ResourceLibrary

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

SharedResource.cs code:

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

web application

Right click on webapp project -> Add -> Reference ... -> Check Resource Library

In your 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()
        ...
        }

Example use in controller:

 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"];
    }

View example:

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

<p>@SharedLocalizer["StringToTranslate"]</p>
Stanley answered 3/6, 2018 at 15:57 Comment(9)
Why did you use ISharedResource?Despot
@Despot this is the standard pattern for dependency injections learn.microsoft.com/en-us/aspnet/core/fundamentals/…Stanley
what about attribute based localizations? (data annotations on viewmodels or enums)Bullington
Great answer. I tried it and it worked with embedded resource build action, however it doesn't work with content build action. Can you give me an alternative to make this method work with content build action type ?Waits
Is it possible to get ResourcePath inside :services.Configure<RequestLocalizationOptions>(options => { CultureInfo[] supportedCultures = new[] { new CultureInfo("en"), new CultureInfo("he") }; options.DefaultRequestCulture = new RequestCulture("en"); options.SupportedCultures = supportedCultures; options.SupportedUICultures = supportedCultures; }); ... }Titanothere
@SoufienHajji I tried this it refuses to work in 3.1 unless the resources our in the main assemlby any ideas.Craven
@csharpdudeni77 Same Here. Any updates on this?Whirl
@MetehanMutlu why asking me i was not the orginal opCraven
How can i use this in a separate Assembly for DTOs with DataAnnotations?Kynan
W
5

You can store the .resx files on MyServices project and create a method for retrieving the resources based on keys. In order to access the IStringLocalizer from MyServices you have to install Microsoft.Extensions.Localization.Abstractions nuget.

Basically localization configurations have to remain on MyWebApp (Startup class), but on MyServices you have to add that nuget for using IStringLocalizer and create a method like GetResourceValueByKey(key). This method can be called from wherever MyServices project will be referenced.

using Microsoft.Extensions.Localization;

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

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

        public string GetResourceValueByKey(string resourceKey)
        {
            return _localizer[resourceKey];
        }
    }}
Worthwhile answered 12/9, 2017 at 14:42 Comment(0)
A
3

One typical solution is for your MyServices assembly to return resource keys (instead of returning the actual resources to be displayed on screen). You can have the .resx file as part of MyWebApp and have resource values for each resource key. This way, your MyService can be utilized by various UI apps each of which have their own resource representations.

Another approach would be to keep the .resx file as part of MyService itself. MyWebApp can load the other assembly and read the resource file from that.

Yet another option would be to keep the resources as a new assembly, and again load it from MyWebApp.

Check the following SO answers to get more details about how to do access .resx files from another assembly -

How can I read embedded .resx in different assembly

Access strings resources from embedded .resx in dll?

How to access another assembly's .resx?

Auld answered 18/7, 2017 at 13:5 Comment(5)
Am I right, that putting .resx files in the root of the MyServices would be enough? What do you mean by returning resource keys?Malemute
Yes, that is right. If you check the Build Action of resource file, it will be is Embedded Resource by default. So it will be available whenever the dll loads.Auld
By returning resource keys, I meant the following -> When you want to return an error message from MyServices, instead of returning the full message "An error occurred in feature x, situation y happened..", you just return a key (Something like Error1001 or ErrorFeatureXSituationY) from the MyServices assembly. Do not have any resource file attached within it. In MyWebApp, have a resource file which has a key Error1001 with the proper error message. This way, your resource file will only be in your MyWebApp.Auld
Seems interesting, but I need .resx files to be included in MyServices, so I will try variant with files in a root!Malemute
This solution and the listed links are not for DotNet Core.Largeminded

© 2022 - 2024 — McMap. All rights reserved.