Microsoft & .NET.NETA Guide to the CultureInfo Class in .NET

A Guide to the CultureInfo Class in .NET

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

The CultureInfo class provides information about a specific culture (locale). The information it provides includes the following:

  • Names for the culture
  • Writing system
  • Calendar used
  • Sort order of strings
  • Date formatting
  • Number formatting

A quick Console App with the following code will basically show most of these and more.

   static void Main(string[] args)
   {
      CultureInfo myCIintl = new CultureInfo("af-ZA", false);

      Console.WriteLine("CompareInfo " +  myCIintl.CompareInfo);
      Console.WriteLine("DisplayName " + myCIintl.DisplayName);
      Console.WriteLine("EnglishName " + myCIintl.EnglishName);
      Console.WriteLine("IsNeutralCulture " +
         myCIintl.IsNeutralCulture);
      Console.WriteLine("Name " + myCIintl.Name);
      Console.WriteLine("NativeName " + myCIintl.NativeName);
      Console.WriteLine("TextInfo " + myCIintl.TextInfo);
      Console.WriteLine("ThreeLetterISOLanguageName " +
         myCIintl.ThreeLetterISOLanguageName);
      Console.WriteLine("ThreeLetterWindowsLanguageName " +
         myCIintl.ThreeLetterWindowsLanguageName);
      Console.WriteLine("TwoLetterISOLanguageName " +
         myCIintl.TwoLetterISOLanguageName);
      Console.WriteLine("DateTimeFormat " +
         myCIintl.DateTimeFormat.ToString());
      Console.WriteLine("NumberFormat " +
         myCIintl.NumberFormat.ToString());
      Console.WriteLine("Calendar " +
         myCIintl.Calendar.ToString());
      Console.ReadLine();

   }

Get Culture Info
Figure 1: Get Culture Info

The culture “code” I have supplied was for a language called Afrikaans (my mother tongue), and now it produces all the property values I requested.

To get a list of all available cultures, code such as the following would do the trick:

   public static void Main()
   {
      List<string> strCultures = new List<string>();

      foreach (CultureInfo ci in
         CultureInfo.GetCultures(CultureTypes.AllCultures))
      {
         string strSpecialName = "(none)";
         try
         {
            strSpecialName =
               CultureInfo.CreateSpecificCulture(ci.Name).Name;
         }
         catch
         {

         }

         strCultures.Add(ci.Name);
      }

      strCultures.Sort();

      foreach (string str in strCultures)
         Console.WriteLine(str);

      Console.ReadLine();
   }

All Cultures
Figure 2: All Cultures

In the preceding code, a List of Culture objects is created, and it gets populated with all the Culture’s names. Then, all of them get written to the Console window.

Conclusion

Short and sweet. Knowing how to obtain a computer’s culture information is a nice little trick and it gives the user a much more personalized experience.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories