39951

Always same culture: en-US

<h3>Question</h3>

I have an asp.net core project where I want to set the default language to nl-BE. For some reason it always take the language en-US

See code below (ps: I created my own ApplicationLocalizer who fetched the resources from a database => works fine).

Startup.cs

public void ConfigureServices(IServiceCollection services) { //Add MVC services.AddMvc() .AddViewLocalization(); //Localization factory services.AddSingleton<IStringLocalizerFactory, ApplicationLocalizerFactory>(); ... } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ApplicationDbContext context) { ... //Localization var supportedCultures = new[] { new CultureInfo("en-US"), new CultureInfo("nl-BE") }; var options = new RequestLocalizationOptions { DefaultRequestCulture = new RequestCulture("nl-BE"), SupportedCultures = supportedCultures, SupportedUICultures = supportedCultures }; app.UseRequestLocalization(options); ... }

A controller

public string Index() { return CultureInfo.CurrentCulture.Name; }

This snippet of code always returns en-US

Can someone help me solve this problem?


<h3>Answer1:</h3>

After hours of searching for solution with no result, I went for heavy debugging and realized that the middleware services are executed in an order as declared in the Configure(IApplicationBuilder app...). Therefore the localization middleware was invoked after the controller action was executed.

When I moved the <em>app.UseRequestLocalization(options)</em> invocation at the very beginning I got properly localized Thread/CultureInfo in controller actions.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); // THIS HAS TO BE A VERY FIRST MIDDLEWARE REGISTRATION SetUpLocalization(app); app.UseCors("AllowAllOrigins"); app.UseMvc(); appLifetime.ApplicationStopped.Register(() => ApplicationContainer.Dispose()); } private static void SetUpLocalization(IApplicationBuilder app) { var supportedCultures = new[] { new CultureInfo("en-US"), new CultureInfo("pl-PL"), new CultureInfo("pl") }; var options = new RequestLocalizationOptions { DefaultRequestCulture = new RequestCulture("en-US", "en-US"), SupportedCultures = supportedCultures, SupportedUICultures = supportedCultures }; // Configure the Localization middleware app.UseRequestLocalization(options); }

来源:https://stackoverflow.com/questions/40997851/always-same-culture-en-us

Recommend

  • How to use buttons in Asp.Net to call a method
  • in ASP.NET Core, is there any way to set up middleware from Program.cs?
  • ASP.NET Core WebAPI 500 Internal error in IIS 7.5
  • How to Inflate Extended LinearLayout from XML
  • Tomcat 7 and ScheduledExecutorService.shutdown
  • Check if a function was called as a decorator
  • Dropzone is not defined
  • Are there circumstances where a hash algorithm can be guaranteed unique?
  • How to generate strong-naming SNK key file with .net libraries
  • Cannot find package java.nio.file
  • Why Can't I Use JS Function Called 'evaluate' in HTML?
  • How can I write a binary array as an image in Python?
  • The GridView 'OrdersGridView' fired event RowDeleting which wasn't handled
  • Angular 6 ng lint combineLatest is deprecated
  • MVC Controller Action only receiving nulls from AJAX
  • How To call Lua function from Javascript
  • How to retrieve a very big cassandra table and delete some unuse data from it?
  • DocuSign API to obtain the response (signed) documents from Power Form
  • Hide button if div empty
  • ruby: how to iterate elements in a hash efficiently
  • Mouse clickAndHold() not working properly on Firefox Using Selenium Webdriver
  • Can an Entity access a Repository?
  • Draggable nested inside Draggable drags both when dragging child in IE
  • ARKit – Rendering a 3D object under an invisible plane
  • Failure to Read Updated AnyLogic DB Values
  • Google Cloud dataflow : Read from a file with dynamic filename
  • How to read contents of a directory recursively in Linux Kernel?
  • Issue with Terrain Collision using Three.js
  • Set initial vuetify v-select value
  • Arraylist of strings into one comma separated string
  • Spotify cocoalibspotify offline status set to 1 but all tracks stuck at waiting
  • Floating parent div grows to hypothetical width of floating child div
  • Will this work on all screen sizes?
  • Codeigniniter insert data through models and controller
  • Jersey serializes character value to ASCII equivalent numeric string
  • Spring Boot not autowiring @Repository
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}