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.
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