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?
I have two projects:
How can I add localization with IStringLocalizer
to MyServices? Where must be .resx
files located?
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<SharedResources> _localizer;
public SharedResource(IStringLocalizer<SharedResources> localizer)
{
_localizer = localizer;
}
public string GetResourceValueByKey(string resourceKey)
{
return _localizer[resourceKey];
}
}}
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
.resx
files in the root of the MyServices
would be enough? What do you mean by returning resource keys? –
Malemute .resx
files to be included in MyServices
, so I will try variant with files in a root! –
Malemute © 2022 - 2024 — McMap. All rights reserved.