Reputation:
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
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 :
Upvotes: 2
Views: 2324
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 :
and the result of this changes :
Upvotes: 3