Localization of RTM in .NET.NET 1.0 does not work

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?

+6
source share
3 answers

You probably noticed the following: https://github.com/aspnet/Mvc/issues/4692

Basically take a look at the comment: https://github.com/aspnet/Mvc/issues/4692#issuecomment-223671462

Summary: you can create a Resource filter in MVC to solve the problem:

 public class CultureSettingResourceFilter : IResourceFilter, IOrderedFilter { public int Order { get { return int.MinValue; } } public void OnResourceExecuted(ResourceExecutedContext context) { // By this time the response would already have been started, so do not try to modify the response } public void OnResourceExecuting(ResourceExecutingContext context) { var culture = httpContext.GetRouteValue("your-culture-key-name")?.ToString(); // set your culture here CultureInfo.CurrentCulture = culture; CultureInfo.CurrentUICulture = culture; } } services.AddMvc(o => { o.Filters.Add(new CultureSettingResourceFilter()); }); 
+4
source

I experimented with i18n and I got my translations to work when supported cultures matched cultures in resource file names

 var supportedCultures = new List<CultureInfo>{ new CultureInfo("en-US"), new CultureInfo("sl-SI"), new CultureInfo("de-DE"), new CultureInfo("hr-HR") }; 

Change resource file names with

Resources / Views / ControllerName / ViewName.sl.resx

for a specific language culture

Resources / Views / ControllerName / ViewName.sl-SI.resx

+1
source

Change to @Siim Haas.
It works well. In my case: I am using LanguageViewLocationExpanderFormat.SubFolder

In ConfigureServices (IServiceCollection Services)

 public void ConfigureServices(IServiceCollection services) { services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; }); services.AddMvc() .AddViewLocalization( Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.SubFolder, opts => { opts.ResourcesPath = "Resources"; } ) .AddDataAnnotationsLocalization(); services.Configure<RequestLocalizationOptions>(opts => { var supportedCultures = new[] { new CultureInfo("en-AU"), new CultureInfo("en-GB"), new CultureInfo("en-US"), new CultureInfo("en"), new CultureInfo("zh-cn"), new CultureInfo("zh"), }; opts.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-US"); opts.SupportedCultures = supportedCultures; opts.SupportedUICultures = supportedCultures; }); } 

In Configure (application IApplicationBuilder, IHostingEnvironment env, ILoggerFactory loggerFactory)

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseRequestLocalization(); app.UseMvc(routes => { routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}"); }); } 

My resources look like:

{Your project} / Resources / Views / {your controller} /ViewName.en-us.resx

0
source

All Articles