使用 CultureInfo 类

CultureInfo 类包含区域性特定的信息,例如语言、国家/地区、日历以及区域性约定。 该类还提供执行区域性特定的操作(如大小写转换、格式化日期和数字以及比较字符串)所需的信息。

CultureInfo 类会为每个区域性指定一个唯一的名称。 有关区域性名称的列表,请参见 CultureInfo 类的说明。 您的应用程序可以使用 GetCultures 方法检索所有区域性的完整列表。 下面的示例显示所有区域性的列表。

Imports System
Imports System.Globalization

public class printClass
   Public Shared Sub Main()  
      Dim ci As CultureInfo
      For Each ci in _
      CultureInfo.GetCultures(CultureTypes.AllCultures)
         Console.WriteLine(ci)
      Next ci
   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);
      }
   }
}

将 CultureInfo 用于非托管代码

注意注意

.NET Framework 应用程序可以使用 平台调用详解 服务访问动态链接库中的非托管函数。

若要初始化国家/地区信息,应用程序可以将一个 CultureInfo 对象传递给与国家/地区对应的 RegionInfo 对象。 或者,在非托管代码中,应用程序也可以将 CultureInfo 对象传递给 Win32 函数 GetLocaleInfo。

若要使用 CultureInfo 对象来初始化 RegionInfo 对象,应用程序必须指定一个 CultureInfo 对象以表示特定区域性,例如阿拉伯语(阿尔及利亚),即“ar-DZ”。 如果尝试使用表示非特定区域性(例如,阿拉伯语(“ar”))的 CultureInfo 对象来初始化 RegionInfo 对象,则会引发异常。 非特定区域性不指定映射到某个国家/地区所需的国家或地区信息。

GetLocaleInfo 方法不同于 RegionInfo 构造函数,原因在于该方法为表示特定区域性或非特定区域性的 CultureInfo 对象返回国家/地区。 例如,如果应用程序将表示非特定区域性阿拉伯语的 CultureInfo 对象传递给 GetLocaleInfo,该方法会将该非特定区域性映射到与之相关联的默认国家/地区。 在此情况下,GetLocaleInfo 会检索沙特阿拉伯。 请慎用 GetLocaleInfo 方法,因为它提供的默认国家/地区映射在文化上可能不适合您的应用程序。 若要消除此差异,在与 API 函数交互操作时,您的应用程序应仅使用特定区域性。

下面的示例演示了 RegionInfo 类构造函数与 GetLocaleInfo 方法如何为同一 CultureInfo 对象检索不同的值。 请注意,当 CultureInfo 对象表示特定区域性阿拉伯语(阿尔及利亚)时,这两种方法都将检索阿尔及利亚并将其作为国家/地区。 但是,当 CultureInfo 对象表示非特定区域性阿拉伯语时,结果将有所不同。 RegionInfo 构造函数无法检索国家/地区,而 GetLocaleInfo 将检索到阿尔及利亚。

Imports System
Imports System.Globalization
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic

Namespace CountryRegionName
   Class 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

      <STAThread()> _
      Public Shared Sub Main(args() As String)
         Dim crn As New CountryRegionName()
         
         ' Create a CultureInfo initialized to the neutral Arabic culture.
         Dim ci1 As New CultureInfo(&H1)
         Console.WriteLine(ControlChars.NewLine + _
            "The .NET Region name: {0}", _
            crn.GetNetCountryRegionName(ci1))
         Console.WriteLine("The Win32 Region name: {0}", _
            crn.GetWinCountryRegionName(ci1))
         
         ' Create a CultureInfo initialized to the specific 
         ' culture Arabic in Algeria.
         Dim ci2 As New CultureInfo(&H1401)
         Console.WriteLine(ControlChars.NewLine + _
            "The .NET Region name: {0}", _
            crn.GetNetCountryRegionName(ci2))
         Console.WriteLine("The Win32 Region name: {0}", _
            crn.GetWinCountryRegionName(ci2))
      End Sub
   End Class
End Namespace
using System;
using System.Globalization;
using System.Runtime.InteropServices;

namespace CountryRegionName
{
  class CountryRegionName
  {
    // The name of a country or region in English
    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.
    public 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.
    public 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;
    }

    [STAThread]
    static void Main(string[] args)
    {
      CountryRegionName crn = new CountryRegionName();

      // Create a CultureInfo initialized to the neutral Arabic culture.
      CultureInfo ci1 = new CultureInfo(0x1);  
      Console.WriteLine("\nThe .NET Region name: {0}", 
         crn.GetNetCountryRegionName(ci1));
      Console.WriteLine("The Win32 Region name: {0}",
         crn.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}", 
         crn.GetNetCountryRegionName(ci2));
      Console.WriteLine("The Win32 Region name: 
         {0}",crn.GetWinCountryRegionName(ci2));
    }
   }
}

该示例产生下面的输出:

The .NET Region name:
The Win32 Region name: Saudi Arabia

The .NET Region name: Algeria
The Win32 Region name: Algeria

请参见

参考

CultureInfo

概念

使用 CurrentUICulture 属性

使用 CurrentCulture 属性

使用 InvariantCulture 属性

与 CultureInfo 对象关联的名称

其他资源

编码和本地化