Quick Tip: Differences between Culture and UICulture

I’m currently studying to take the WebApp MCTS certification exam, this will be my first certification so I’m a little nervous and don’t know what to expect from it.  So I purchased the corresponding practice test from UCertify and been taking a couple of simulation test to discover any weak spots.

I recently discovered one of this weak spots and it’s the difference between Culture and UICulture. I’m sure a lot of people have the concept crystal clear but I’m going to allow myself a post about it so I don’t ever forget again, or have a place to look in case I DO forget xD.

Basically when you set the Culture property you specify the language to use in objects with no UI, so we’re talking about things like dates, time, time zones, numbers, etc.

On the other hand, when setting the UICulture your telling the runtime to load specific resource file to compile with the current page and to replace localized values accordingly.

A very important detail about these two properties, and a very uncommonly stated fact, is that they are tied to the system’s control panel regional configuration, even if it’s misconfigured.  The Culture property is tied to the default regional settings and the UICulture is bound to the “keyboard and other input” settings. This is actually very important because it means that the defined formats for each culture or language will be used by the .NET runtime when parsing values.

I remember having a very tedious bug a while ago, when I had recently purchased my previous laptop, which would throw an exception when parsing perfectly valid datetime strings.  After a LOT of head-banging against the keyboard I discovered that the source of the problem was the regional configuration for my OS. You see the laptop came with built-in Windows 7 in English from the manufacturer, but me being a Spanish speaking fellow am used to dates in this format ‘dd-mm-yyyy’ and would often get confuse with the English date format ‘mm-dd-yyyy’. So I messed with the configuration of the English formats in control panel to have it displayed the way I wanted it, instead of installing a different language or whatever. I assumed that configuration was used for displaying purposes only but it turns out that it was affecting the way in which the .NET framework was handling the parsing of strings to date in the English culture. So I advice you not to do that, and when receiving an exception parsing a perfectly formed date string that’s the place to look.

0 comments: