Thursday, June 12, 2008

CurrentCulture vs CurrentUICulture

I have been guilty of using the CurrentUICulture where I shouldn’t. I made the assumption that the CurrentUICulture would be the thing I want to use to do things on my UI like formating and displaying a date. *WRONG*!

After one of the guys at work asked me why his FxCop told him off for not passing a IFormatProvider into his string.Format(..) I told him to just pass in CultureInfo.CurrentUICulture. Well I was wrong. We are in Australia and it started formatting his dates as mm/dd/yyyy not dd/mm/yyyy. A quick Google showed me the error of my ways. Basically always just use CultureInfo.CurrentCulture. The CultureInfo.CurrentUICulture is actually used by ResourceManager to identify which resource dictionary to use to show text to the user. Now this is great for Globalization but not to be used for localization. Gee don’t they sound like the same thing….?


Anonymous said...

And why is CurrentUICulture always set to en-US?

Lee Campbell said...

Using the CurrentUICulture Property explains that if the .net app does not explicity set this value then it is evaluated from the UI language set in the "regional and language options" in Control Panel.

Anonymous said...

Well. It is not actually true.

The article says that the CurrentUICulture is configurable only if you have Windows Multilingual User Interface (MUI) installation.

For most of the Windows installations, what you set in "regional and language options" affects only the CurrentCulture. And the CurrentUICulture is always by default equal to your Windows installation language.

You should change it in your code (or application settings in ASP.NET).

Lee Campbell said...

Sorry if there is some confusion here.
CurrentCulture is to be used for formatting strings (Dates and currency etc).
CurrentUICulture is used to identify which set of Resources to be used.

To allow the user to define the way they want dates and currency formated on windows applications you should specify the CurrentCulture when you make calls to string.Format for example. The value used here will be taken from the "regional and language settings".

If you want to provide different text depending on the language of the user on your windows application you will need to use resources instead of hard coded strings. This will only be available on PCs with MUI windows install. (Im not sure with ASP.NET)