Search code examples
c#globalization

CurrentUICulture and new created CultureInfo Instance


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


Solution

  • 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";
    }