2

I'm trying to find out program's language and change my string for this language

CultureInfo culture = new CultureInfo("en");
CultureInfo currentCulture = Thread.CurrentThread.CurrentUICulture;
string msg="";
if (currentCulture == culture)
{
    msg = "Some words";
}

Even though values of culture and currentCulture are the same if statement is not working and my msg string is not changing.

Here is my debug results

Name--Value--Type

culture -- {en} -- System.Globalization.CultureInfo

currentCulture -- {en} -- System.Globalization.CultureInfo

Erdem Ergin
  • 835
  • 1
  • 7
  • 6
  • Maybe your current culture is `en`. Try debugging – Kamil Budziewski Jul 26 '13 at 07:45
  • 1
    Maybe your current culture is `en-US`. Also have a look at the difference between `CurrentCulture` and `CurrentUICulture`: http://stackoverflow.com/questions/329033/what-is-the-difference-between-currentculture-and-currentuiculture-properties-of – Tim Schmelter Jul 26 '13 at 07:47
  • 1
    "`if` statement is not working" - not working *how*? I *guess* it's not throwing an exception. It just evaluates `currentCulture == culture` to be `false` and does not enter the body (in other words: it works perfectly fine). Are you sure, your `CurrentUICulture` is "en" and not maybe "en-US" or something else? – Corak Jul 26 '13 at 07:51
  • I updated my question. My debug results are seems equal. – Erdem Ergin Jul 26 '13 at 08:04

2 Answers2

1

CultureInfo is a reference-type without an override of Equals() so 2 separate instances will always be unequal.

This little piece of code will print False:

  var c1 = new CultureInfo("en");
  var c2 = new CultureInfo("en");
  Console.WriteLine(c1 == c2);

You can compare on a property, Name and LCID seem good candidates.

H H
  • 263,252
  • 30
  • 330
  • 514
0

Your culture could be represented as en-Us. Debug your code first. That might be the problem.

CultureInfo is a class, so it is a reference type. When you compare two different reference with ==, it always return false. You can try to compare them based their CultureInfo.Name property for example. Like;

if(currentCulture.Name == culture.Name)
{
   msg = "Some words";
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364