CultureInfo クラスの使用

更新 : 2007 年 11 月

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 の使用

87k6sx8t.alert_note(ja-jp,VS.90).gifメモ :

.NET Framework アプリケーションは、「プラットフォーム呼び出しの詳細」で説明されているサービスを使用してダイナミック リンク ライブラリ内のアンマネージ関数にアクセスできます。

アプリケーションで国または地域の情報を初期化するには、CultureInfo オブジェクトを国または地域に対応する RegionInfo オブジェクトに渡します。また、アンマネージ コードでは、アプリケーションが CultureInfo オブジェクトを Win32 関数 GetLocaleInfo に渡すこともできます。

CultureInfo オブジェクトを使用して RegionInfo オブジェクトを初期化するには、"ar-DZ" で示されるアラビア語 (アルジェリア) などの特定のカルチャを表す CultureInfo オブジェクトを指定する必要があります。アラビア語 ("ar") などのニュートラル カルチャを表す CultureInfo オブジェクトで RegionInfo オブジェクトを初期化しようとすると、例外がスローされます。ニュートラル カルチャでは、カルチャを国や地域に割り当てるために必要な地域や国の情報が指定されません。

GetLocaleInfo メソッドが RegionInfo コンストラクタと異なる点は、特定のカルチャまたはニュートラル カルチャを表す CultureInfo オブジェクトの国や地域を返すことです。たとえば、アプリケーションがアラビア語のニュートラル カルチャを表す CultureInfo オブジェクトを GetLocaleInfo に渡すと、このメソッドはニュートラル カルチャをそのカルチャが関連付けられた既定の国や地域に割り当てます。この場合、GetLocaleInfo は Saudi Arabia (サウジアラビア) を取得します。GetLocaleInfo メソッドを使用する場合、このメソッドが提供する既定の国または地域の割り当てはアプリケーションのカルチャとしては適切ではない場合があります。この不一致を回避するには、API 関数と相互運用する場合は、アプリケーションで特定のカルチャだけを使用するようにします。

同じ CultureInfo オブジェクトを渡したときに、RegionInfo クラス コンストラクタと GetLocaleInfo メソッドが異なる値を取得する場合があることを次の例に示します。CultureInfo オブジェクトがアラビア語 (アルジェリア) の特定のカルチャを表す場合は、どちらのメソッドも国または地域として Algeria (アルジェリア) を取得します。ただし、CultureInfo オブジェクトがアラビア語のニュートラル カルチャを表す場合、結果は異なります。RegionInfo のコンストラクタは国または地域の取得に失敗しますが、GetLocaleInfo は Algeria (アルジェリア) を取得します。

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

参照

概念

CurrentUICulture プロパティの使用

CurrentCulture プロパティの使用

InvariantCulture プロパティの使用

CultureInfo オブジェクトに関連付けられている名前

参照

CultureInfo

その他の技術情報

エンコーディングとローカリゼーション