CurrentCulture vs CurrentUICulture, what's the difference?
Thursday, March 19, 2009 11:39 PM
This week at work, I noticed a piece of code setting the Thread.CurrentThread.CurrentCulture to a specific culture. No big deal right? Except shouldn't that be CurrentUICulture? hhhhhmmmm Good question! What's the difference between them? Please read on for a bit of a descriptive and some code.
This week I was working on some back end asp.net code for our website. I saw a piece of code setting up the user's specific culture on the current thread. No big deal, except the original developer only setup the Thread.CurrentThread.CurrentCulture (or was it the CurrentUICulture?) property. What happened to the other one? What's it matter you ask?
Great question! I thought, oh, I'll just "whip" up a demo to illustrate this. hhmmm easier said than done! This thing's turned into a two night exercise, but it has been fun and enlightening. So here goes!
This is a quick'n'dirty winforms app to try to illustrate the differences between Culture and CultureUI. The app starts off with a list of cultures on your left with the three buttons at the bottom to flip things to English, French or Italian. To make things REALLY interesting you'll need to check/uncheck the checkboxes on the top right then hit the Refresh button to change the controls in the middle GroupBox. The GroupBox on the right will not change and is meant as your control (sorry, excuse the pun :>).
- to reset the entire form, select English, both checkboxes and click Refresh, that will put everything back to en-US, this way, it'll be much easier to see the differences when they change
- when you're changing JUST one of the checkboxes, you have to remember in your head what the previous culture settings were cause otherwise it'll get confusing fast, see the first bullet to reset the form
- uncheck both options checkboxes and the refresh appears to not do anything (which is exacly correct)
- selecting Italy button, checking only CultureUI then refresh will change all the controls on the middle GroupBox to Italian (internet translations, so they might not be 100 accurate :>) but the Importo (Amount) will still be formatted in the default culture.
- select US button, make sure both checkboxes are checked then hit Refresh (this resets the form), then Italy button, then check only Culture checkbox then refresh, you'll see only the Amount: value is formatted in Euros (some other changes are done too like the days of the week and today)
- reset the form (hit US button, check both checkboxes then refresh), then hit Italy button and refresh, you'll see everything change
- you can play with the two Week Days listboxes to compare their values to each other, the most interesting is comparing Italy to the default culture (fr-CA) and compare both versions of Tuesday
The culture information on the current thread gets set quite easily (selectedCulture gets set on the SelectedIndexChanged for the listbox).
This is used for anything that has to do with the controls on the page/form which require translations to support your specific culture/language (aka Internationalization). So for example, when you setup your WinForm for language specifics, this is the property you're using. Good rule of thumb is, if you can see it, you're using CultureUIInfo.
In my application above, you'll see two explicit languages supported (French Canadian and Italian) as well as the default language (which I just happen to be using english as the invariant culture. When you select those languages (quick access via bottom buttons) then refresh, you'll see the GUI/WinForm dynamically reload with the new language. That's key, the form basically has to reload/reinitialize itself when you're changing the culture.
There are a few links out there which talk about a boat load of new code to make this work. I just figured I would let the auto-generated code the VS Designer creates for me do all the heavy lifting.
Anything that has to do with numbers or date formatting, comparisons or logic is using CultureInfo (aka Localization). Good rule of thumb is, if it has to do with business logic/LOB stuff, you're using CultureInfo. Although the number format of dollar signs or euros might intially look like a UI thing, you don't actually translate that, you set the culture and you get the appropriate currency symbol autmagically. The other hiccup I've seen people trip over is, with the CultureInfo, it doesn't do anything with conversion rates, so if you have a decimal of 1.23 dollars in the US, it's still going to be 1.23 when you format it with Italian culture and get the euro symbol. What I mean is, just cause you set the Italian culture doesn't mean it knows what the going currency exchange rate is and automatically convert over for you. It's smart but not THAT smart (yet? :>). In my application above, you'll see this is used for comparing the days of the week.
Other interesting TidBits
It's actually more work to setup and use a different culture than what's currently being used, you have to pass a CultureInfo object to most/all of the ToString(). The first ToString() just, well, does it's job. The second one has to have the defaultCulture passed into it.
Getting the current day of the week is non-trivial.
Well, it's getting late, oops too late, it's very late (2AM). So that's all for me tonight. I hope you learned a little bit about the differences between CultureInfo and CultureUIInfo. Check out the code for any further answers you might want.
Grab a coffee and get coding! :>
Michael Kaplan: Why we have both CurrentCulture and CurrentUICulture
1 comment(s) so far...
By phenry on
Monday, March 23, 2009 3:22 PM
Re: CurrentCulture vs CurrentUICulture, what's the difference?
I was talking with SG about this today and something came up which I guess I missed here and should mention. If you're "playing" with translation resource files (resx files), YOU ARE dealing with the CurrentUICulture object and only that object. If you are looking to format a number for a number format, or are looking to compare two string being sensitive to culture info, then you are dealing with CurrentCulture.
Hope that helps add a bit more info to your day. ;>