I am trying to implement localization for my asp.NET Core 1.0 RTM web application after Microsoft documentation , and I just can't get it working. The problem I am facing is that it always displays strings from the resource file in English no matter how I try to set up the culture.
If anyone has 5 minutes of their time, I would be very grateful for your input.
Here is my Startup.cs content, which deals with localization:
public void ConfigureServices(IServiceCollection services) { ... services.AddMvc() .AddViewLocalization(LanguageViewLocationExpanderFormat.SubFolder) .AddDataAnnotationsLocalization(); services.AddScoped<LocalizationFilter>(); services.AddLocalization(options => options.ResourcesPath = "Resources"); var supportedCultures = new List<CultureInfo>{ new CultureInfo("en-US"), new CultureInfo("sl-SI"), new CultureInfo("de-DE"), new CultureInfo("hr-HR") }; services.Configure<RequestLocalizationOptions>(options => { options.SupportedCultures = supportedCultures; options.SupportedUICultures = supportedCultures; options.DefaultRequestCulture = new RequestCulture(new CultureInfo("en-US"), new CultureInfo("en-US")); }); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { ... var supportedCultures = new List<CultureInfo>{ new CultureInfo("en-US"), new CultureInfo("sl-SI"), new CultureInfo("de-DE"), new CultureInfo("hr-HR") }; var requestLocalizationOptions = new RequestLocalizationOptions { SupportedCultures = supportedCultures, SupportedUICultures = supportedCultures, DefaultRequestCulture = new RequestCulture(new CultureInfo("en-US"), new CultureInfo("en-US")) }; app.UseRequestLocalization(requestLocalizationOptions); }
And this is how I change the culture inside OnActionExecuting in my ActionFilter
actionContext.HttpContext.Response.Cookies.Append( CookieRequestCultureProvider.DefaultCookieName, CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)), new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }); CultureInfo.CurrentCulture = culture; CultureInfo.CurrentUICulture = culture;
I also tried to do this on my controller without any luck.
Then in my views I use @inject IViewLocalizer Localizer to display localized strings using @Localizer["Name"] .
My resoucres are in the Resources / Views / ControllerName folder:
- Resources /Views/ControllerName/ViewName.en.resx
- Resources /Views/ControllerName/ViewName.sl.resx
- ...
The displayed string is always from the .en resource file no matter how I try to change the culture. Is there something I'm missing? Is there anything else I should do? It seems that the problem is with the language setting. Based on the documentation, you should set a cookie with Response.Cookies.Append , but I also tried with CultureInfo.CurrentCulture , since Thread.CurrentThread.CurentCulture no longer available.
I really donβt know what I am missing. Any ideas whatsoever?