Verwenden der CultureInfo-Klasse

Die CultureInfo-Klasse enthält kulturbezogene Informationen (z. B. die Sprache, das Land/die Region, den Kalender und kulturelle Konventionen für die jeweilige Kultur). Diese Klasse enthält darüber hinaus Informationen, die für die Durchführung kulturabhängiger Operationen erforderlich sind (z. B. für die Groß-/Kleinschreibung, für die Formatierung von Datumsangaben und Zahlen sowie für Vergleiche von Zeichenfolgen).

Die CultureInfo-Klasse gibt einen eindeutigen Namen für jede Kultur an. Eine Liste der Kulturnamen finden Sie in der Beschreibung der CultureInfo-Klasse. Die Anwendung kann mithilfe der CultureInfo.GetCultures-Methode eine vollständige Liste aller Kulturen abrufen. Das folgende Beispiel zeigt eine Liste aller Kulturen.

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

Verwenden von "CultureInfo" bei nicht verwaltetem Code

.NET Framework-Anwendungen können mithilfe des Dienstes für Plattformaufrufe auf nicht verwaltete Funktionen in DLLs (Dynamic Link Libraries) zugreifen. Wenn die Anwendung jedoch ein CultureInfo-Objekt an die Win32-Funktion GetLocaleInfo übergibt, sollten Sie beachten, dass die Rückgabewerte der Funktion nicht immer mit den Rückgabewerten des RegionInfo-Klassenkonstruktors konsistent sind.

Ein RegionInfo-Objekt aus .NET Framework entspricht einem Land/einer Region. Zur Initialisierung eines RegionInfo-Objekts mithilfe eines CultureInfo-Objekts muss die Anwendung ein CultureInfo-Objekt angeben, das eine spezifische Kultur darstellt, wie ar-DZ für Arabisch (Algerien). Bei dem Versuch, ein RegionInfo-Objekt mit einem CultureInfo-Objekt zu initialisieren, das eine neutrale Kultur darstellt (z. B. "ar" für Arabisch), wird eine Ausnahme ausgelöst. Die neutrale Kultur gibt die Informationen zur Region bzw. zum Land nicht an, die für die Zuordnung zu einem Land oder einer Region erforderlich sind.

Die GetLocaleInfo-API-Funktion unterscheidet sich vom RegionInfo-Konstruktor darin, dass sie ein Land/eine Region für ein CultureInfo-Objekt zurückgibt, das entweder eine bestimmte oder eine neutrale Kultur darstellt. Wenn die Anwendung z. B. ein CultureInfo-Objekt, das die neutrale Kultur "ar" (Arabisch) darstellt, an GetLocaleInfo übergibt, ordnet die Funktion die neutrale Kultur dem Standardland bzw. der Standardregion zu, mit dem bzw. der sie verbunden ist. In diesem Fall wird von GetLocaleInfo Saudi-Arabien zurückgegeben. Gehen Sie bei Verwendung der GetLocaleInfo-Funktion sorgfältig vor, da die von ihr standardmäßig bereitgestellte Länder-/Regionszuordnung für Ihre Anwendung kulturell ungeeignet sein kann. Zur Beseitigung dieser Diskrepanz sorgen Sie dafür, dass die Anwendung bei der Interoperation mit einer API-Funktion nur bestimmte Kulturen verwendet.

Das folgende Beispiel veranschaulicht, wie der RegionInfo-Klassenkonstruktor und die GetLocaleInfo-Funktion bei der Übergabe desselben CultureInfo-Objekts verschiedene Werte zurückgeben können. Wenn das CultureInfo-Objekt die spezifische Kultur ar-DZ für Arabisch (Algerien) darstellt, geben beide Methoden Algerien als Länder-/Regionsnamen zurück. Wenn das CultureInfo-Objekt jedoch die neutrale Kultur ar (Arabisch) darstellt, unterscheiden sich die Ergebnisse. Die Rückgabe eines Landes/einer Region durch den RegionInfo-Konstruktor schlägt fehl, während GetLocaleInfo Algerien zurückgibt.

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

Dieses Beispiel erzeugt folgende Ausgabe:

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

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

Siehe auch

Referenz

CultureInfo Class

Konzepte

Verwenden der CurrentUICulture-Eigenschaft
Verwenden der CurrentCulture-Eigenschaft
Verwenden der InvariantCulture-Eigenschaft
Einem CultureInfo-Objekt zugeordnete Namen

Weitere Ressourcen

Codierung und Lokalisierung