RE: [aus-dotnet] Default Culture


    [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
    • From: Ted Harper
    • Subject: RE: [aus-dotnet] Default Culture
    • Date: Tue, 05 Jul 2005 11:33:37 +1000

    All browsers from 4.0 onwards (and perhaps a bit earlier too) provide a header which indicates the user’s language/locale settings. What we do in Page_Load() is call a method which looks at that header and sets the culture:

     

    Here is the call (put in each Page_Load()):

     

                // Set the culture and UI culture to the browser's accept language

                IBWUtilities.SetCulture(Request);

     

     

    … and here is what is called:

     

     

                /// <summary>

                /// Set the display culture for the current thread to a particular named culture.

                /// </summary>

                /// <param name="cultureName">The name of the culture to be set for the thread</param>

                private static void SetCulture(string cultureName)

                {

                      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName);

                      Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);

                }

     

                /// <summary>

                /// Set the display culture for the current thread to the most appropriate culture from the user's incoming Http "request" object.

                /// </summary>

                /// <param name="request"></param>

                public static void SetCulture(HttpRequest request)

                {

                      if (request != null)

                      {

                            if (request.UserLanguages != null)

                            {

                                  if (request.UserLanguages.Length > -1)

                                  {

                                        string cultureName = request.UserLanguages[0];

                                        IBWUtilities.SetCulture(cultureName);

                                  }

                            }

                            // TODO: Set to a (system-wide, or possibly user-specified) default culture if the browser didn't give us any clues.

                      }

                }

     

    Once that has been called for a page, then all your formatting of input and output (and resource file lookups if you have internationalised your app) will automatically be with respect to the individual user’s browser setting – including date and time formats. In Internet Explorer, this is changeable in Tools/Internet Options/Languages… (where the first language in the list will be the one that the logic above will use), but the normal/default situation for an end user is that Windows will default it appropriately for the primary languages chosen for each user without them needing to do any setup, and your ASP.Net website can then provide them a localised experience without them feeling that they needed to do anything to accomplish it.

     

     

    ted.h.

     

     

     

     

    Ted Harper
    IT Manager and Chief Architect

    Impact Investing Pty Ltd
    Level 11, 131 Macquarie St
    (GPO Box 3311)
    Sydney NSW 2000
    Australia

    Ted.Harper@xxxxxxxxxxxxxxxxxxx
    IM: ted.harper@xxxxxxxxxxxxxxxxxxx
    http://www.impactinvesting.com

    tel:
    fax:
    mobile:

    +61-2-9241-1333
    +61-2-9241-1366
    +61-418-442-342

     

    Add me to your address book...

     


    From: dotnet-owner@xxxxxxxxxxx [mailto:dotnet-owner@xxxxxxxxxxx] On Behalf Of jey
    Sent: Tuesday, 5 July 2005 9:39 AM
    To: dotnet@xxxxxxxxxxx
    Subject: [aus-dotnet] Default Culture

     

    ByDefault, an ASP.NET app would use the Server's language settings, right?
    I have found my server and client's regional settings to be Australia and still the
    App was using american date format in a datagrid at times. This behaviour was intermittent.

    Any idea what is causing this?

    rgds,
    jey

    GIF image




    (Click here for more information on the aus-dotnet mailling list)