I have an ASP .NET 5 RC1 website to which I am trying to add localization. Based on the information found I did the following
In the ConfigureService in Startup.cs:
- Enable Localization and setting the ResourcePath to "Resources"
Enable View Localization and Enable Data Annotations Localization
//check http://damienbod.com/2015/10/21/asp-net-5-mvc-6-localization/ services.AddLocalization(options => options.ResourcesPath = "Resources"); // Add MVC services to the services container. //check http://blogs.msdn.com/b/webdev/archive/2015/10/15/announcing-availability-of-asp-net-5-beta8.aspx services.AddMvc().AddViewLocalization().AddDataAnnotationsLocalization();
In the Configure method fin Startup.cs
- Setup the list of supported cultures
Enable Request Localization
//check http://www.jerriepelser.com/blog/setting-thread-culture-aspnet5 //check http://damienbod.com/2015/10/21/asp-net-5-mvc-6-localization/ List<CultureInfo> supportedCultures = new List<CultureInfo>() { new CultureInfo("en"), new CultureInfo("es") }; var requestLocalizationOptions = new RequestLocalizationOptions() { SupportedCultures = supportedCultures, SupportedUICultures = supportedCultures }; app.UseRequestLocalization(requestLocalizationOptions, new RequestCulture(new CultureInfo("es")));Create a Resources folder under the project
- Create the Resources for the Controller. With the convention {Project}.{Controllers}.{ControllerClassName}.{culture}.resx
- Create the Resources for the Views. With the convention Views.{ViewFolder}.{ViewName}.cshtml.{culture}.resx
Use the IHtmlLocalizer in the controller, and access the item. In this case localizer["Title"], which is found and works just fine. However when the culture is set to "es" it is not found and just falls back to the default resource.
private IHtmlLocalizer<HomeController> _htmlLocalizer; public HomeController(IOptions<PTIWebPortal.Configuration.PTIWebPortalConfiguration> pConfiguration, ILoggerFactory factory, IHtmlLocalizer<HomeController> localizer) : base(pConfiguration, factory) { this._htmlLocalizer = localizer; }
The same is happens for the views, it only works with the default resource, but not with the others.
Any ideas on how to fix it?