Archive for January, 2009|Monthly archive page

Culture ‘en’ is a neutral culture. It cannot be used in formatting and parsing and therefore cannot be set as the thread’s current culture.

Ran into this one today after a couple of Mac users reported a bug.

The application was having problems converting DateTime values.

When I looked at it I noticed that the users browser language was set to ‘en’.

I drilled down to the offending code:

public CultureInfo VisitorsCultureInfo
{
   get { 
     return CultureInfo.GetCultureInfo(
        HttpContext.Current.Request.UserLanguages[0]
     ); 
   }
}

The developer has used the browser setting which equates to the CurrentUICulture, which as Peter Ritchie explains is incorrect.

.NET has the concept of a UI culture (for localization of resources) and an application culture (for the globalization of data).

Since we are working with dates (data) we should be using the CurrentCulture property.

The following article demonstrates a better approach.

So the bug has been resolved, and I can have a worry free weekend!

Here’s the fix:

public CultureInfo VisitorsCultureInfo
{
  get { return System.Threading.Thread.CurrentThread.CurrentCulture; }
}
Advertisements