5

I would like to know how I can determine whether the CultureInfo instance returned by CultureInfo.CurrentCulture has all it's default values or whether it has been customised by the user.

I have done some testing using the following LINQPad snippet and the LINQPad command line runner.

var ci = CultureInfo.CurrentCulture;
var dtf = ci.DateTimeFormat;

ci.Name.Dump("CultureInfo");
dtf.FullDateTimePattern.Dump("FullDateTimePattern");
dtf.LongDatePattern.Dump("LongDatePattern");
dtf.LongTimePattern.Dump("LongTimePattern");

I select English (South Africa) as my format in the region dialog and my snippet reports the following.

E:\Junk>lprun CurrentCulture.linq
CultureInfo: en-ZA
FullDateTimePattern: dd MMMM yyyy hh:mm:ss tt
LongDatePattern: dd MMMM yyyy
LongTimePattern: hh:mm:ss tt

Then I change the long date format to hide leading zero on the day and the long and short time to be 24 hour time. The snippet now reports this.

E:\Junk>lprun CurrentCulture.linq
CultureInfo: en-ZA
FullDateTimePattern: d MMMM yyyy HH:mm:ss
LongDatePattern: d MMMM yyyy
LongTimePattern: HH:mm:ss

Although the culture names remain the same the properties I've changed are showing me the new values.

My question is whether there is a single property or method that will tell me whether the culture returned by CurrentCulture has been changed from its default (en-ZA in this case) or not. I thought that CultureInfo.UseUserOverride might be that property but it returns true in either case.

Steve Crane
  • 4,340
  • 5
  • 40
  • 63
  • 1
    I think you could simply compare all needed values between `CurrentCulture` and the culture, which you *create* for the same locale as current. – Sinatr Jan 22 '14 at 10:49
  • Why? Ideally your application shouldn't depend on the culture that the user set on his machine. – Grzenio Jan 22 '14 at 10:50
  • Yes @Sinatr, I could compare properties but with the CultureInfo class being so rich I imagined it would surely be able to give me this information. – Steve Crane Jan 22 '14 at 10:56
  • @Grzenio, have you never asked a question simply to know the answer? – Steve Crane Jan 22 '14 at 10:59
  • @SteveCrane, fair enough :). I just tend to spend way too much time working with legacy code. – Grzenio Jan 22 '14 at 11:43

1 Answers1

6

You can't ask if the culture has been changed by the user but you can request a culture that has not been changed by the user.

new CultureInfo(CultureInfo.CurrentCulture.Name, false);

or

CultureInfo::GetCultureInfo(CultureInfo.CurrentCulture.Name);

In either of these, the UseUserOverrides property will be false.

Only the default culture can have its data overridden by the user. An administrator can override the data in any culture by installing a custom replacement culture. You can detect that by checking the CultureTypes property for the presence of the CultureTypes.ReplacementCultures flag. There is no way to get the original data in that case.

Note that if you are actually trying to write code that changes its behavior based on this, you probably are writing code that will be broken by future releases. The data does change between releases as bugs are fixed.

Eric MSFT
  • 3,246
  • 1
  • 18
  • 28