locked
Localization asp.net core 3.1 project in shared libary not being found. RRS feed

  • Question

  • User-183185495 posted

    Hi i have an asp.net core project that is based on 3.1 , however its not finding my resource when its in a shared libary it works fine in the same libary but not the shared one.

    This is my startup file

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Identity;
    using Microsoft.AspNetCore.Identity.UI;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.HttpsPolicy;
    using Microsoft.EntityFrameworkCore;
    
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using MISSystem.WebApi.Data;
    using MISSystem.Dal;
    using MISSystem.Models;
    using MISSystem.Web.Helpers;
    using MISSystem.Web.Localization;
    using Microsoft.AspNetCore.Mvc.Razor;
    using System.Globalization;
    using Microsoft.AspNetCore.Localization;
    using Microsoft.Extensions.Options;
    
    namespace MISSystem.Web {
        public class Startup {
            public Startup(IConfiguration configuration) {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services) {
                services.AddDbContext<MISDBContext>
              (options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    
                services.AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(
                        Configuration.GetConnectionString("DefaultConnection")));
    
                services.AddDefaultIdentity<ApplicationUser>()
                    .AddEntityFrameworkStores<MISDBContext>().AddClaimsPrincipalFactory<MyUserClaimsPrincipalFactory>();  //<---- add this line 
    
                 
    
                services.AddControllersWithViews();
                services.AddLocalization();
    
                services.AddRazorPages().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
               .AddViewLocalization()
               .AddDataAnnotationsLocalization();
    
    
                services.AddLocalization(o => o.ResourcesPath = "Resources");
                services.Configure<RequestLocalizationOptions>(options => {
                    var supportedCultures = new[] { new CultureInfo("en"), new CultureInfo("fr") };
    
                    options.DefaultRequestCulture = new RequestCulture(culture: "en", uiCulture: "en");
                    options.SupportedCultures = supportedCultures;
                    options.SupportedUICultures = supportedCultures;
                    options.RequestCultureProviders = new List<IRequestCultureProvider>
                {
                    new QueryStringRequestCultureProvider(),
                    new CookieRequestCultureProvider()
                };
                });
    
                services.AddSingleton<ISharedResource, SharedResource>();
    
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
                var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
                app.UseRequestLocalization(locOptions.Value);
                    
                if (env.IsDevelopment()) {
                    app.UseDeveloperExceptionPage();
                    app.UseDatabaseErrorPage();
                } else {
                    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[] { "en-US","en-GB", "fr" };
                var localizationOptions = new RequestLocalizationOptions().SetDefaultCulture(supportedCultures[0])
                    .AddSupportedCultures(supportedCultures)
                    .AddSupportedUICultures(supportedCultures);
                app.UseRequestLocalization(localizationOptions);
              
    
                app.UseHttpsRedirection();
                app.UseStaticFiles();
    
                app.UseRouting();
    
                app.UseAuthentication();
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints => {
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");
                    endpoints.MapRazorPages();
                });
            }
        }
    }
    

    As you see I am setting up the loclization middle ware fine I have installed the nugets for locazliation as well as some said it didnt work until that was done.

    I have two projects

    MISSystem.Web

    MISSystem.Lng I set the default name space ie the root name space to be the same of MSISystem.Web

    Wednesday, July 1, 2020 6:00 AM

Answers

  • User711641945 posted

    Hi roguenidb,

    I have two projects

    MISSystem.Web

    MISSystem.Lng I set the default name space ie the root name space to be the same of MSISystem.Web

    The two project namespace could not be the same.

    Then,please check the following answer:

    https://stackoverflow.com/a/62671043/11398810

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 1, 2020 6:50 AM
  • User711641945 posted

    Hi roguenidb,

    You just provide the startup.cs.Not sure how did you define SharedResource and what is your project structure?

    Notes:No need to change the default namespace.

    This is my working demo:

    In your MISSystem.Lng class library:

    1.Create SharedResource.cs in the root project.

    public class SharedResource
    {
    }

    2.Create Resources/SharedResource.fr.resx:

    3.The project structure:

    In your MISSystem.Web web application:

    1.Add MISSystem.Lng reference to MISSystem.Web(right-click Dependencies which is in MISSystem.Web->Add Reference->Choose MISSystem.Lng):

    2.HomeController:

    using MISSystem.Lng;  //remember to add this reference
    
    public class HomeController : Controller
    {
        private readonly IStringLocalizer<SharedResource> _sharedLocalizer;
    
        public HomeController(IStringLocalizer<SharedResource> sharedLocalizer)
        {
            _sharedLocalizer = sharedLocalizer;
        }
    
        public IActionResult Index()
        {
            ViewData["Message"] = _sharedLocalizer["Hello"];
            return View();
        }
    }

    3.Startup.cs:

    public void ConfigureServices(IServiceCollection services)
    { 
        services.AddControllersWithViews();
        services.AddLocalization(options => options.ResourcesPath = "Resources");
    
        services.AddRazorPages().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
        .AddViewLocalization(options => options.ResourcesPath = "Resources")
        .AddDataAnnotationsLocalization();
    
        services.Configure<RequestLocalizationOptions>(options =>
        {
            var supportedCultures = new[]
            {
                new CultureInfo("en-US"),
                new CultureInfo("en-GB"),
                new CultureInfo("fr")
            };
        
            options.DefaultRequestCulture = new RequestCulture(culture: "en-GB", uiCulture: "en-GB");
            options.SupportedCultures = supportedCultures;
            options.SupportedUICultures = supportedCultures;
        });
            
    }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseRequestLocalization();
    
        app.UseHttpsRedirection();
        app.UseStaticFiles();
    
        app.UseRouting();
    
        app.UseAuthorization();
    
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 2, 2020 8:21 AM

All replies

  • User711641945 posted

    Hi roguenidb,

    I have two projects

    MISSystem.Web

    MISSystem.Lng I set the default name space ie the root name space to be the same of MSISystem.Web

    The two project namespace could not be the same.

    Then,please check the following answer:

    https://stackoverflow.com/a/62671043/11398810

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 1, 2020 6:50 AM
  • User-183185495 posted

    How did you find my source code I did not post it here ?

    Wednesday, July 1, 2020 7:45 AM
  • User-183185495 posted

    I already changed my project to use the shared resource i deleted that question as not correct to my lattest source !

    Wednesday, July 1, 2020 7:52 AM
  • User711641945 posted

    Hi roguenidb,

    You just provide the startup.cs.Not sure how did you define SharedResource and what is your project structure?

    Notes:No need to change the default namespace.

    This is my working demo:

    In your MISSystem.Lng class library:

    1.Create SharedResource.cs in the root project.

    public class SharedResource
    {
    }

    2.Create Resources/SharedResource.fr.resx:

    3.The project structure:

    In your MISSystem.Web web application:

    1.Add MISSystem.Lng reference to MISSystem.Web(right-click Dependencies which is in MISSystem.Web->Add Reference->Choose MISSystem.Lng):

    2.HomeController:

    using MISSystem.Lng;  //remember to add this reference
    
    public class HomeController : Controller
    {
        private readonly IStringLocalizer<SharedResource> _sharedLocalizer;
    
        public HomeController(IStringLocalizer<SharedResource> sharedLocalizer)
        {
            _sharedLocalizer = sharedLocalizer;
        }
    
        public IActionResult Index()
        {
            ViewData["Message"] = _sharedLocalizer["Hello"];
            return View();
        }
    }

    3.Startup.cs:

    public void ConfigureServices(IServiceCollection services)
    { 
        services.AddControllersWithViews();
        services.AddLocalization(options => options.ResourcesPath = "Resources");
    
        services.AddRazorPages().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
        .AddViewLocalization(options => options.ResourcesPath = "Resources")
        .AddDataAnnotationsLocalization();
    
        services.Configure<RequestLocalizationOptions>(options =>
        {
            var supportedCultures = new[]
            {
                new CultureInfo("en-US"),
                new CultureInfo("en-GB"),
                new CultureInfo("fr")
            };
        
            options.DefaultRequestCulture = new RequestCulture(culture: "en-GB", uiCulture: "en-GB");
            options.SupportedCultures = supportedCultures;
            options.SupportedUICultures = supportedCultures;
        });
            
    }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseRequestLocalization();
    
        app.UseHttpsRedirection();
        app.UseStaticFiles();
    
        app.UseRouting();
    
        app.UseAuthorization();
    
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 2, 2020 8:21 AM
  • User-183185495 posted

    It ended up being I had SharedResources.fr.resx

    If you notice the s that is why it was not finding my resource working now thanks.

    Thursday, July 2, 2020 12:11 PM