locked
Change current culture RRS feed

  • Question

  • User-993816716 posted

    Herea is what i have done:

    1. On Application_Start event, i implement change "en-AU" culture to "en-US" culture
      var culture = new CultureInfo("en-US");
      System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
      System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture.Name);
      
    2. On Application_BeginRequest event, i implement check current culture but current culture is "en-AU"

    Why is that? I have checked all event on global.asax that not have any proccess for change current culture. I am use MVC3 framework.
    (all action only return view() without other proccess so they are not dangerous)

    Monday, December 17, 2012 10:05 PM

Answers

  • User1779161005 posted

    So, every thread have a System.Threading.Thread.CurrentThread. When a thread is implemented every time that it re-init System.Threading.Thread.CurrentThread with default value(en-AU), is'n that?

    I don't see why every thread should inherit the setting you happen to set on one particular thread in the app_start code, so I don't think you will get what you're describing here.

    I think setting it on each request is the better approach (like you're doing for Application_PreRequestHandlerExecute).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 18, 2012 10:18 AM

All replies

  • User1779161005 posted

    Because that's a per-thread setting and there are many threads in a web server.

    Monday, December 17, 2012 10:46 PM
  • User614805505 posted

    Hi,

    You have to implement the new culture at 

        Protected Overrides Sub InitializeCulture()

    http://msdn.microsoft.com/en-us/library/bz9tc508(v=vs.80).aspx

    Monday, December 17, 2012 10:46 PM
  • User-993816716 posted

    So, every thread have a System.Threading.Thread.CurrentThread. When a thread is implemented every time that it re-init System.Threading.Thread.CurrentThread with default value(en-AU), is'n that?

    I have checked my mvc application, it have see two thread and every thread is implemented that every System.Threading.Thread.CurrentThread are init with default value(en-AU) whether i have changed System.Threading.Thread.CurrentThread to "en-US" last time.

    I can change culture for every thread on Application_PreRequestHandlerExecute event, can't do?

    void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
            {
                if (HttpContext.Current.Session != null)
                {
                    string errorCode;
                    var culture = HttpContext.Current.Session["culture"] == null ? "en-US" : HttpContext.Current.Session["culture"].ToString();
    
                    var culture = new CultureInfo(nameCulture);
                    System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
                    System.Threading.Thread.CurrentThread.CurrentCulture =
                    CultureInfo.CreateSpecificCulture(culture.Name);
                }
            }



    Tuesday, December 18, 2012 1:46 AM
  • User1779161005 posted

    So, every thread have a System.Threading.Thread.CurrentThread. When a thread is implemented every time that it re-init System.Threading.Thread.CurrentThread with default value(en-AU), is'n that?

    I don't see why every thread should inherit the setting you happen to set on one particular thread in the app_start code, so I don't think you will get what you're describing here.

    I think setting it on each request is the better approach (like you're doing for Application_PreRequestHandlerExecute).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 18, 2012 10:18 AM