Set culture for the session

Each user of my application will choose their country, after which it will be saved in a cookie and saved for future requests. Everything is working fine, but I need to establish a culture at the beginning of the session. I am currently experimenting by setting the culture in web.config as en-GB, and then using Global.asax to override the culture for the session in en-US. Code below

protected void Session_Start(object sender, EventArgs e) { if (Globals.CountryID == 8) { Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US"); } } 

The country identifier is 8, and the culture is set to en-US in the following code. However, when I go to the page with ToString ("C") installed, it is still displayed in pounds sterling, and the culture is still within the UK.

Any suggestions?

+4
source share
2 answers

You assume that the thread that will serve the page request will be the same thread that started the session, as in your code - this is not guaranteed.

You can save the culture in the Session variable and use the InitializeCulture override on your pages, as described in: How to customize the culture and culture of the user interface to globalize ASP.NET web pages .

+8
source

You must set a culture for each call, Session_Start is only started when a session is created. Thus, your culture is only correct when you first call or when you recreate the session.

+1
source

Source: https://habr.com/ru/post/1386079/


All Articles