What is the difference between CurrentCulture and CurrentUICulture properties of CultureInfo in .NET?
Asked Answered
B

5

401

In .NET there is the CultureInfo class in the System.Globalization namespace. It has two similar properties both returning values of the CultureInfo type: CurrentCulture and CurrentUICulture.

What is the difference between them?

Which one should I use when and why?

Blocked answered 30/11, 2008 at 16:26 Comment(3)
Bear-in-mind that Microsoft, in their wisdom, don't separate the UI cultures of US English and (British) English or other Englishes. There's no MUI for English, which means the CurrentUICulture will always be en-US on an English-language machine, regardless of the CurrentCulture, which can be set to localise the Regional Settings.Protrusion
Yes. I'm in the UK and find CurrentCulture is 'en-GB', but CurrentUICulture is 'en-US'.Attend
Just found this article explaining this in detail: github.com/jbe2277/waf/wiki/CurrentCulture-vs.-CurrentUICultureChosen
U
383

CurrentCulture is the .NET representation of the default user locale of the system. This controls default number and date formatting and the like.

CurrentUICulture refers to the default user interface language, a setting introduced in Windows 2000. This is primarily regarding the UI localization/translation part of your app.

Whatever regional options the system is configured to have will be the "Current" values in your .NET app.

Often times they are both the same. But on my system they would be different: I prefer my numbers and dates in the German format, so the CurrentCulture would be German, but I also prefer all my applications in English, so the CurrentUICulture would be English.

There is a nice article on the topic: Sorting it all Out: Why we have both CurrentCulture and CurrentUICulture

Undercool answered 30/11, 2008 at 16:35 Comment(4)
Actually this might be a good alternative for in-depth explanation: forums.asp.net/post/1080435.aspxCryometer
@Cryometer Thanks for the hint. I've updated the link. The post you link to is decent as well, maybe even better.Undercool
Those are terrible names, then, since CurrentUICulture really means CurrentLanguageCulture and CurrentCulture really means CurrentDataCulture.Coaptation
@Pxtl: I'd opt for CurrentFormattingCulture rather than CurrentDataCulture. Still, note that these may not be as obviously disjoint as even your names suggest. For instance, I'd argue the long date format (D) can contain quite some "language": en-US returns "Friday, April 6, 2018", whereas ru-RU returns "6 апреля 2018 г.".Semination
S
116

This is a simple trick I use to remember which one to use:

(date, currency, double).tostring = CurrentCulture

resource.fr-CA.resx file = currentUICulture
Somnus answered 2/9, 2010 at 12:23 Comment(1)
If the comments by nicodemus13 and Colonel Panic are to be trusted, it would seem the second part of your trick shouldn't work.Semination
P
21

A good way to make a difference in addition to the nice explanations done by fellow users, and an important aspect in web application development is the following:

  • CurrentCulture represents the setup of the web server. For example, if your ASP.NET web application is hosted in Germany, the value of CutlureInfo.CurrentCulture would most probably be de-DE. Thus, the default .ToString() formatting for IFormattable types would use the default German formattings, or the ones which have been set up on the server OS as defaults.

  • CurrentUICulture can be captured from the user agent, and may represent the user interface culture of the client connecting to the website. For example, if you load that website from Russia, your local settings are set to use Russian language, and your user agent sends your locale settings to the server (Opera and IE do this automatically, not sure for Chrome and FireFox), the CurrenUICulture would represent ru-RU. This will cause any resources like localized strings retrieved via ResourceManager or localization expressions in ASP.NET aspx/ascx files to be in Russian (if translations are available).

Pulsatory answered 26/11, 2015 at 14:2 Comment(0)
N
19

Differences:

  1. CurrentCulture is for formatting of dates and currency while CurrentUICulture goes with language/translations. It will be used by ResourceManager to look up resources by culture.
  2. Namespace of CurrentCulture class is in System.Globalization while CurrentUICulture comes from System.Threading.
  3. CurrentCulture is persisted across different requests in the session while CurrentUICulture needs to be set with every request.

Likeness:

They both are System.Globalization.CultureInfo instances.

Naif answered 26/11, 2015 at 13:18 Comment(0)
W
14

It is worth noting that the CurrentUICulture supports non-country specific locales such as 'en' (Neutral cultures) whereas CurrentCulture ONLY supports country specific locales such as 'en-GB'. Setting CurrentCulture to a neutral culture will throw an ArgumentException.

I assume that this is because formats such as dates and currency are more strongly linked to the country itself, but the language displayed is often interchangeable between countries.

Weedy answered 12/5, 2016 at 13:52 Comment(1)
A very good point indeed. Nevertheless, I would argue if CurrentUICulture is designed to be used for Neutral cultures only. You could successfully assign a specific culture with the CurrentUICulture and it would make sense if you have differences in the localization for certain countries operating with the same language.Pulsatory

© 2022 - 2024 — McMap. All rights reserved.