使用 CultureInfo 類別
更新:2007 年 11 月
CultureInfo 類別 (Class) 含有文化特性 (Culture) 的特定資訊,例如語言、國家/地區、日曆和文化特性慣例。這個類別同時提供執行特定文化特性作業所需的資訊,例如大小寫、格式化日期和數字和比較字串。
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);
}
}
}
搭配 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
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