How localization works in ASP.NET? How CultureInfo.CurrentUICulture works? Manual change culture for Localization

Solution for How localization works in ASP.NET? How CultureInfo.CurrentUICulture works? Manual change culture for Localization
is Given Below:

I read in the documentation (https://docs.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.currentuiculture?view=net-5.0) that the CultureInfo.CurrentUICulture field is set for the thread. But I immediately had a question: is it ASP.NET can’t handle multiple requests in the same thread? In this case, a conflict is possible.

I want to create a website application and a tg-bot in one. I decided to use the built-in localization method using ILocalizedString. But the question arose how to properly install Cultureinfo for the user in the bot. Information about the language is stored in the database, each user has its own. But in the new versions of NET, there is no longer a WithCulture method for ILocalizedString. The only option is to process each user’s updates in a separate thread?

Explanation this fully in detail is quite overwhelming as your question is just basically how CultureInfo work in ASP.NET. So I’ll make it as simplest as possible for me.

To take the most of this answer, I highly recommend you to take a tour on Async/await and HttpContext in Asp.net.
Then, you should take a look at this.

Now, I’m assuming you get the difference between thread synchronous context and HttpContext from Http request.

Now as default, it’s true that CultureInfo.CurrentUICulture field is set for the thread. But you can determine your own logic to provide replacement one for each Http request, as describe in detailed here.

Behind the scene, app.UseRequestLocalization(); register a middleware that called RequestLocalizationMiddleware to your pipeline and use it to provide the CultureInfo to each of your http request to the server, then the result stick with the HttpRequest itself.

Then when Async/await process happened, the thread go back to the thread pool as we all know, when the task finish processing, a thread come back from the thread pool and continue handling it.

When the CultureInfo is need, the thread will take it out of the HttpContext(which is default to the thread itself if you not use app.UseRequestLocalization(); or default by the middleware it self if you just add the app.UseRequestLocalization(); but not provide custom logic to RequestLocalizationOptions) instead of the CultureInfo of the thread it self.

That’s it… the beautiful of layering design.