'.NET Core Localization with Class Library

It works fine when the resources folder is on the webUI.how do i store the resources file of my Views in class library . Thanks for help

[WebUI] [Class Library]

Program.cs

builder.Services.AddLocalization(options =>
{
    options.ResourcesPath = "Resources";
});

builder.Services.AddControllersWithViews()
    .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix, option =>
    {
        option.ResourcesPath = "Resources";
    })
    .AddDataAnnotationsLocalization()
     .AddRazorRuntimeCompilation();

builder.Services.Configure<RequestLocalizationOptions>(options =>
{
    options.DefaultRequestCulture = new(builder.Configuration["DefaultLanguage"]);

    CultureInfo[] cultures = new CultureInfo[]
    {
        new("en-US"),
        new("tr-TR"),
        new("fr-FR")
    };
        options.RequestCultureProviders = new List<IRequestCultureProvider>
        {
            new CookieRequestCultureProvider(),
        };
    options.SupportedCultures = cultures;
    options.SupportedUICultures = cultures;
});


Solution 1:[1]

Here is my working demo , you could refer to :

ResourceLibrary:

enter image description here

SharedResource.cs , put it in the root folder of project and it does not need to contain any data, just the class declaration.

MVC project , Program.cs

using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc.Razor;
using System.Globalization;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services.AddControllersWithViews().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                .AddDataAnnotationsLocalization(); ;

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}
var supportedCultures = new[]
         {
                new CultureInfo("en-US"),
                new CultureInfo("de"),//you can add more language as you want...
            };

app.UseRequestLocalization(new RequestLocalizationOptions
{
    DefaultRequestCulture = new RequestCulture("en-US"),
    // Formatting numbers, dates, etc.
    SupportedCultures = supportedCultures,
    // UI strings that we have localized.
    SupportedUICultures = supportedCultures
});
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

Controller localization,first add reference to ResourceLibrary.

public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IStringLocalizer<SharedResource> _sharedLocalizer;
        public HomeController(ILogger<HomeController> logger, IStringLocalizer<SharedResource> sharedLocalizer)
        {
            _logger = logger;
            _sharedLocalizer = sharedLocalizer;
        }

        public IActionResult Index()
        {
            var value = _sharedLocalizer["Privacy"];
            return View();
        }
    }

Index localization

@using Microsoft.Extensions.Localization
@using Microsoft.AspNetCore.Mvc.Localization
@using ResourceLibrary

@inject IStringLocalizer<SharedResource> Localizer
@inject IHtmlLocalizer<SharedResource> HtmlLocalizer

@{
    ViewData["Title"] = @Localizer["Home"];
}

<div class="text-center">   
    <p>
        @HtmlLocalizer["Privacy Policy"]
    </p>
</div>

result:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Qing Guo