Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localization in external class libraries in ASP.NET Core

Tags:

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?

like image 587
Yurii N. Avatar asked Jul 18 '17 12:07

Yurii N.


People also ask

How ASP NET application are localized?

To opt-in to localization, we need to modify our Startup file. We'll be registering a few services, configuring options, and registering middleware. All steps that are common-place for additions in an ASP.NET application. Starting in our ConfigureServices method, we need to make a call to AddLocalization .

What is localization in asp net?

Localization is the process of customizing the globalized web application to a specific locale and culture. Various resources such as images and text for the specific locale are created. The resource file in localization is scoped to a particular page in an application.


2 Answers

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> 
like image 150
Shiran Dror Avatar answered Sep 19 '22 04:09

Shiran Dror


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 _localizer;

public SharedResource(IStringLocalizer<SharedResources> localizer) { _localizer = localizer; } public string GetResourceValueByKey(string resourceKey) { return _localizer[resourceKey]; } }}
like image 20
Popa Andrei Avatar answered Sep 20 '22 04:09

Popa Andrei