Share via


使用 CultureInfo 類別

CultureInfo 類別 (Class) 含有文化特性 (Culture) 的特定資訊,例如語言、國家/地區、日曆和文化特性慣例。 這個類別同時提供執行特定文化特性作業所需的資訊,例如大小寫、格式化日期和數字和比較字串。

CultureInfo 類別會指定每個文化特性的唯一名稱。 如需文化特性名稱的清單,請參閱 CultureInfo 類別的描述。 您的應用程式可以使用 GetCultures 方法擷取所有文化特性的完整清單。 下列範例會顯示所有文化特性的清單。

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);
      }
   }
}

搭配 Unmanaged 程式碼使用 CultureInfo

注意

.NET Framework 應用程式可以使用詳述平台叫用服務存取動態連結程式庫中的 Unmanaged 函式。

若要初始化國家/地區資訊,您的應用程式可以將對應於國家/地區的 CultureInfo 物件傳遞到 RegionInfo 物件。 或者,在 Unmanaged 程式碼中,應用程式可以將 CultureInfo 物件傳遞到 Win32 函式 GetLocaleInfo。

若要使用 CultureInfo 物件初始化 RegionInfo 物件,您的應用程式必須指定表示特定文化特性的 CultureInfo 物件,例如名為 "ar-DZ" 的阿拉伯文 (阿爾及利亞)。 嘗試使用表示中性文化特性的 CultureInfo 物件 (例如阿拉伯文 ("ar")) 來初始化 RegionInfo 物件,便會擲回例外狀況。 中性文化特性不會指定在對應至國家/地區時的必要國家或地區資訊。

GetLocaleInfo 方法之所以不同於 RegionInfo 建構函式,在於它會傳回表示特定文化特性或中性文化特性之 CultureInfo 物件的國家/地區。 例如,如果應用程式將表示中性文化特性阿拉伯文的 CultureInfo 物件傳遞給 GetLocaleInfo,該方法便會將此中性文化特性對應至與其關聯的預設國家/地區。 在這個情況下,GetLocaleInfo 便會擷取沙烏地阿拉伯。 請特別小心使用 GetLocaleInfo 方法,因為其所提供的預設國家/地區對應功能在文化特性上可能並不適合您的應用程式。 若要避免這個不一致情形,請指定應用程式在與 API 函式相互操作時僅使用特定的文化特性。

下列範例示範 RegionInfo 類別建構函式和 GetLocaleInfo 方法如何為相同的 CultureInfo 物件擷取不同的值。 請注意當 CultureInfo 物件表示特定文化特性,如阿拉伯文 (阿爾及利亞),這兩種方法都會擷取阿爾及利亞做為國家/地區。 然而,當 CultureInfo 物件表示中性文化特性阿拉伯文時,結果就會不同。 RegionInfo建構函式就會失敗,來擷取 [國家/地區,而GetLocaleInfo擷取沙烏地阿拉伯國內免費電話。

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

請參閱

參考

CultureInfo

概念

使用 CurrentCulture 屬性

使用 InvariantCulture 屬性

其他資源

全球化和當地語系化。NET Framework 應用程式