user6746850
user6746850

Reputation:

Resource file not found in implementing multi-language ASP.NET core MVC Project

i have ASP.NET core project and in this project i want Implement multi-language application. for my problem i check this question Views are localized but resources are not found but I did not find the right answer. the question referred is about localized views and with different config .

so this is my code and couldn't find localize resources file in controller

Resource file location:

/Resources/Controllers/HomeController.fa.resx

resource file location

Startup:

 public class Startup
{
    ..... 

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddLocalization(o => { o.ResourcesPath = "Resources"; });
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
       .....

        app.UseStaticFiles();

        IList<CultureInfo> supportedCultures = new List<CultureInfo>
        {
                new CultureInfo("en-US"),
                new CultureInfo("fa-IR"),
        };

        var options = new RequestLocalizationOptions
        {
            DefaultRequestCulture = new RequestCulture("fa-IR"),
            SupportedCultures = supportedCultures,
            SupportedUICultures = supportedCultures
        };
        app.UseRequestLocalization(options);

       ....
    }

..... 


}

HomeController:

 public class HomeController : Controller
{
    .....

    private readonly IStringLocalizer<HomeController> _stringLocalizer;

    public HomeController(IStringLocalizer<HomeController> stringLocalizer)
    {
        _stringLocalizer = stringLocalizer;
    }

    public IActionResult About()

    {
        ViewData["Message"] = _stringLocalizer["Hello"];
        return View();
    }

     .....

}

this is my test :

request method

the result of request

Upvotes: 2

Views: 2324

Answers (1)

user6746850
user6746850

Reputation:

Eventually After 3 Hours I was able to solve this problem, to fix this problem, the following changes are required:

startup :

public class Startup
{
  ......

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddLocalization(o => { o.ResourcesPath = "Resources"; });
        services.AddMvc();
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
            app.UseRequestLocalization();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        IList<CultureInfo> supportedCultures = new List<CultureInfo>
        {
                new CultureInfo("en-US"),
                new CultureInfo("fa-IR"),
        };
        var options = new RequestLocalizationOptions
        {
            DefaultRequestCulture = new RequestCulture("fa-IR","fa-IR"),
            SupportedCultures = supportedCultures,
            SupportedUICultures = supportedCultures
        };

        app.UseRequestLocalization(options);
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

my resource name and path changes like this figure :

enter image description here

and the result of this changes :

enter image description here

Upvotes: 3

Related Questions