I have two projects:
How can I add localization with IStringLocalizer
to MyServices? Where must be .resx
files located?
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 .
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.
This is how I solved it. Thanks to Popa Andrei answer for directing me to the right place.
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]; } } } }
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() ... }
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>
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]; } }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With