Export (0) Print
Expand All

Using the CultureInfo Class

The CultureInfo class contains culture-specific information, such as the language, country/region, calendar, and cultural conventions. This class also provides the information required for performing culture-specific operations, such as casing, formatting dates and numbers, and comparing strings.

The CultureInfo class specifies a unique name for each culture. For a list of culture names, see the description of the CultureInfo class. Your application can use the GetCultures method to retrieve a complete list of all cultures. The following example displays a list of all cultures.

using System;
using System.Globalization;

public class printClass
{
   public static void Main()
   {
      foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
      {
         Console.WriteLine(ci);
      }
   }
}

NoteNote:

.NET Framework applications can access unmanaged functions in dynamic link libraries using the A Closer Look at Platform Invoke service.

To initialize country/region information, your application can pass a CultureInfo object to a RegionInfo object that corresponds to the country/region. Alternatively, in unmanaged code, the application can pass a CultureInfo object to the Win32 function GetLocaleInfo.

To initialize a RegionInfo object using a CultureInfo object, your application must specify a CultureInfo object that represents a specific culture, such as Arabic (Algeria), named "ar-DZ". An attempt to initialize a RegionInfo object with a CultureInfo object that represents a neutral culture, such as Arabic ("ar"), throws an exception. The neutral culture does not specify the country or region information necessary for mapping to a country/region.

The GetLocaleInfo method differs from the RegionInfo constructor in that it returns a country/region for a CultureInfo object that represents either a specific culture or a neutral culture. For example, if the application passes a CultureInfo object that represents the neutral culture Arabic to GetLocaleInfo, the method maps the neutral culture to the default country/region with which it is associated. In this case, GetLocaleInfo retrieves Saudi Arabia. Be careful when using the GetLocaleInfo method, as the default country/region mapping it provides might be culturally inappropriate for your application. To eliminate this discrepancy, have your application use only specific cultures when interoperating with an API function.

The following example demonstrates how the RegionInfo class constructor and the GetLocaleInfo method can retrieve different values for the same CultureInfo object. Note that when the CultureInfo object represents the specific culture Arabic (Algeria), both methods retrieve Algeria as the country/region. However, when the CultureInfo object represents the neutral culture Arabic, the results differ. The RegionInfo constructor fails to retrieve a country/region, while GetLocaleInfo retrieves Saudi Arabia.

using System;
using System.Globalization;
using System.Runtime.InteropServices;

class CountryRegionName
{
   // The name of a country or region in English 
   const int LOCALE_SENGCOUNTRY     = 0x1002;

   // Use COM interop to call the Win32 API GetLocalInfo.
   [DllImport("kernel32.dll", CharSet=CharSet.Unicode)]
   public static extern int GetLocaleInfo(
      // The locale identifier. 
      int Locale,
      // The information type. 
      int LCType,
      // The buffer size.
      [In, MarshalAs(UnmanagedType.LPWStr)] string lpLCData,int cchData
   );

   // A method to retrieve the .NET Framework Country/Region 
   // that maps to the specified CultureInfo. 
   static String GetNetCountryRegionName(CultureInfo ci)
   {
      // If the specified CultureInfo represents a specific culture, 
      // the attempt to create a RegionInfo succeeds. 
      try
      {
         RegionInfo ri = new RegionInfo(ci.LCID);
         return ri.EnglishName;
      }
      // Otherwise, the specified CultureInfo represents a neutral 
      // culture, and the attempt to create a RegionInfo fails. 
      catch
      {
         return String.Empty;
      }
   }

   // A method to retrieve the Win32 API Country/Region 
   // that maps to the specified CultureInfo. 
   static String GetWinCountryRegionName(CultureInfo ci)
   {
      int size = GetLocaleInfo(ci.LCID, LOCALE_SENGCOUNTRY, null, 0);
      String str = new String(' ', size);
      int err  = GetLocaleInfo(ci.LCID, LOCALE_SENGCOUNTRY, str, size);
      // If the string is not empty, GetLocaleInfo succeeded. 
      // It will succeed regardless of whether ci represents 
      // a neutral or specific culture. 
      if(err != 0)  
         return str;
      else 
         return String.Empty;
   }

   static void Main()
   {
      // Create a CultureInfo initialized to the neutral Arabic culture.
      CultureInfo ci1 = new CultureInfo("ar");  
      Console.WriteLine("\nThe .NET Region name: {0}", 
         GetNetCountryRegionName(ci1));
      Console.WriteLine("The Win32 Region name: {0}",
         GetWinCountryRegionName(ci1));

      // Create a CultureInfo initialized to the specific  
      // culture Arabic in Algeria.
      CultureInfo ci2 = new CultureInfo(0x1401);  
      Console.WriteLine("\nThe .NET Region name: {0}", 
         GetNetCountryRegionName(ci2));
      Console.WriteLine("The Win32 Region name: {0}",
         GetWinCountryRegionName(ci2));
   }
}
// The example displays the following output: 
//   The .NET Region name: 
//   The Win32 Region name: Saudi Arabia 
// 
//   The .NET Region name: Algeria 
//   The Win32 Region name: Algeria

Community Additions

ADD
Show:
© 2014 Microsoft