Share via


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.

Imports System.Globalization

Public Class Example
   Public Shared Sub Main()  
      For Each ci As CultureInfo In CultureInfo.GetCultures(CultureTypes.AllCultures)
         Console.WriteLine(ci)
      Next
   End Sub
End Class
using System;
using System.Globalization;

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

Using CultureInfo with Unmanaged Code

Note

.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.

Imports System.Globalization
Imports System.Runtime.InteropServices

Module CountryRegionName
   ' The name of a country or region in English.
   Private LOCALE_SENGCOUNTRY As Integer = &H1002

   ' Use COM interop to call the Win32 API GetLocalInfo.
   Declare Unicode Function GetLocaleInfoW Lib "Kernel32.dll" _
      (Locale As Integer, LCType As Integer,<[In](), _
       MarshalAs(UnmanagedType.LPWStr)> lpLCData As String, _
       cchData As Integer) As Integer

   ' A method to retrieve the .NET Framework Country/Region
   ' that maps to the specified CultureInfo.
   Public Function GetNetCountryRegionName(ci As CultureInfo) As String
      ' If the specified CultureInfo represents a specific culture,
      ' the attempt to create a RegionInfo succeeds.
      Try
         Dim ri As 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
      End Try
   End Function

   ' A method to retrieve the Win32 API Country/Region
   ' that maps to the specified CultureInfo.
   Public Function GetWinCountryRegionName(ci As CultureInfo) As String
      Dim size As Integer = GetLocaleInfoW(ci.LCID, _
         LOCALE_SENGCOUNTRY, Nothing, 0)
      Dim str As New String(" "c, size)
      Dim err As Integer = GetLocaleInfoW(ci.LCID, _
         LOCALE_SENGCOUNTRY, str, size)
      ' If the string is not empty, GetLocaleInfoW succeeded.
      ' It will succeed regardless of whether ci represents
      ' a neutral or specific culture.
      If err <> 0 Then
         Return str
      Else
         Return String.Empty
      End If
   End Function

   Public Sub Main()
      ' Create a CultureInfo initialized to the neutral Arabic culture.
      Dim ci1 As New CultureInfo("ar")
      Console.WriteLine(ControlChars.NewLine + _
         "The .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.
      Dim ci2 As New CultureInfo("ar-DZ")
      Console.WriteLine(ControlChars.NewLine + _
         "The .NET Region name: {0}", _
         GetNetCountryRegionName(ci2))
      Console.WriteLine("The Win32 Region name: {0}", _
         GetWinCountryRegionName(ci2))
   End Sub
End Module
' 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
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

See Also

Reference

CultureInfo

Concepts

Using the CurrentUICulture Property

Using the CurrentCulture Property

Using the InvariantCulture Property

Names Associated with a CultureInfo Object

Other Resources

Encoding and Localization