Reputation: 411
I cannot figure how to work with Localization on an ASP.NET Core MVC website : I've follow this website but I think I forgot something :
Startup :
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(
config =>
{
config.Filters.Add<ActionFilter>();
})
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
services.Configure<RequestLocalizationOptions>(option =>
{
var supportedCultures = new[]
{
new CultureInfo("en"),
new CultureInfo("fr"),
};
option.DefaultRequestCulture = new RequestCulture(culture: "fr", uiCulture: "fr");
option.SupportedCultures = supportedCultures;
option.SupportedUICultures = supportedCultures;
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
HomeController :
public class HomeController : Controller
{
private readonly IStringLocalizer<HomeController> _localizer;
public HomeController(IStringLocalizer<HomeController> localizer)
{
_localizer = localizer;
}
public IActionResult Index()
{
return View()
}
}
Index.cshtml
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
@{
ViewData["Title"] = Localizer["Home"];
}
<h1>News</h1>
<h2>@Localizer["Home"]</h2>
And I have a Resources Folder
with a file HomeController.fr.resx
inside where Home => Accueil relation is defined.
The function Request.HttpContext.Features.Get<IRequestCultureFeature>().RequestCulture.Culture.Name
return fr
but the page always display Home
instead of Accueil
.
Am I missing something to do for the localization ?
Upvotes: 1
Views: 234
Reputation: 411
I finally figure why it's not working, I just forget to add the Localization.AspNetCore.TagHelpers NugetPackage.
Upvotes: 1