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; }
}
About these ads

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: