CultureInfo.CurrentCulture プロパティ

定義

現在のスレッドとタスク ベースの非同期操作で使用されるカルチャを表す CultureInfo オブジェクトを取得または設定します。

public:
 static property System::Globalization::CultureInfo ^ CurrentCulture { System::Globalization::CultureInfo ^ get(); void set(System::Globalization::CultureInfo ^ value); };
public:
 static property System::Globalization::CultureInfo ^ CurrentCulture { System::Globalization::CultureInfo ^ get(); };
public static System.Globalization.CultureInfo CurrentCulture { get; set; }
public static System.Globalization.CultureInfo CurrentCulture { get; }
static member CurrentCulture : System.Globalization.CultureInfo with get, set
static member CurrentCulture : System.Globalization.CultureInfo
Public Shared Property CurrentCulture As CultureInfo
Public Shared ReadOnly Property CurrentCulture As CultureInfo

プロパティ値

現在のスレッドとタスクベースの非同期操作で使用されるカルチャ。

例外

プロパティは null に設定されます。

次の例では、現在のスレッドの CurrentCultureCurrentUICulture を変更する方法を示します。

using System;
using System.Globalization;

public class Example0
{
   public static void Main()
   {
      // Display the name of the current culture.
      Console.WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name);

      // Change the current culture to th-TH.
      CultureInfo.CurrentCulture = new CultureInfo("th-TH", false);
      Console.WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name);

      // Display the name of the current UI culture.
      Console.WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name);

      // Change the current UI culture to ja-JP.
      CultureInfo.CurrentUICulture = new CultureInfo( "ja-JP", false );
      Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name);
   }
}

// The example displays the following output:
//       CurrentCulture is en-US.
//       CurrentCulture is now th-TH.
//       CurrentUICulture is en-US.
//       CurrentUICulture is now ja-JP.
Imports System.Globalization
Imports System.Threading

Public Module Example2
   Public Sub Run()
      ' Display the name of the current culture.
      Console.WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name)

      ' Change the current culture to th-TH.
      CultureInfo.CurrentCulture = New CultureInfo("th-TH", False)
      Console.WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name)

      ' Display the name of the current UI culture.
      Console.WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name)

      ' Change the current UI culture to ja-JP.
      CultureInfo.CurrentUICulture = New CultureInfo("ja-JP", False)
      Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name)
   End Sub
End Module
' The example displays the following output:
'       CurrentCulture is en-US.
'       CurrentCulture is now th-TH.
'       CurrentUICulture is en-US.
'       CurrentUICulture is now ja-JP.

注釈

CultureInfo プロパティとそれに関連付けられているオブジェクトによって返されるCurrentCulture オブジェクトは、日付、時刻、数値、通貨値の既定の形式、テキストの並べ替え順序、大文字と小文字の区別規則、および文字列比較を決定します。

現在のカルチャは、実行中のスレッドのプロパティです。 このプロパティを新しいカルチャを表す CultureInfo オブジェクトに設定すると、 Thread.CurrentThread.CurrentCulture プロパティの値も変更されます。 ただし、現在のカルチャを取得して設定するには、常に CultureInfo.CurrentCulture プロパティを使用することをお勧めします。

このプロパティが返す CultureInfo オブジェクトは読み取り専用です。 つまり、 DateTimeFormatを変更するなどして、既存のオブジェクトを変更することはできません。 現在のカルチャの日時形式またはその他の側面を変更するには、新しい CultureInfo オブジェクトを作成してプロパティに割り当てます。

スレッドの文化がどのように決定されるか

スレッドが開始されると、そのカルチャは最初に次のように決定されます。

  • プロパティ値がDefaultThreadCurrentCultureされていない場合は、スレッドが実行されているアプリケーション ドメインのnull プロパティで指定されたカルチャを取得します。

  • スレッドがタスク ベースの非同期操作を実行しているスレッド プール スレッドの場合、そのカルチャは呼び出し元スレッドのカルチャによって決定されます。 次の例では、現在のカルチャをポルトガル語 (ブラジル) に変更し、それぞれスレッド ID、タスク ID、および現在のカルチャを表示する 6 つのタスクを起動します。 各タスク (およびスレッド) は、呼び出し元スレッドのカルチャを継承しています。

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Threading;
    using System.Threading.Tasks;
    
    public class Example14
    {
        public static async Task Main()
        {
            var tasks = new List<Task>();
            Console.WriteLine($"The current culture is {Thread.CurrentThread.CurrentCulture.Name}");
            Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-BR");
            // Change the current culture to Portuguese (Brazil).
            Console.WriteLine($"Current culture changed to {Thread.CurrentThread.CurrentCulture.Name}");
            Console.WriteLine($"Application thread is thread {Thread.CurrentThread.ManagedThreadId}");
            // Launch six tasks and display their current culture.
            for (int ctr = 0; ctr <= 5; ctr++)
                tasks.Add(Task.Run(() =>
                {
                    Console.WriteLine($"Culture of task {Task.CurrentId} on thread {Thread.CurrentThread.ManagedThreadId} is {Thread.CurrentThread.CurrentCulture.Name}");
                }));
    
            await Task.WhenAll(tasks.ToArray());
        }
    }
    
    // The example displays output like the following:
    //     The current culture is en-US
    //     Current culture changed to pt-BR
    //     Application thread is thread 9
    //     Culture of task 2 on thread 11 is pt-BR
    //     Culture of task 1 on thread 10 is pt-BR
    //     Culture of task 3 on thread 11 is pt-BR
    //     Culture of task 5 on thread 11 is pt-BR
    //     Culture of task 6 on thread 11 is pt-BR
    //     Culture of task 4 on thread 10 is pt-BR
    
    Imports System.Globalization
    Imports System.Threading
    
    Module Example1
        Public Sub S1()
            Dim tasks As New List(Of Task)
            Console.WriteLine("The current culture is {0}",
                            Thread.CurrentThread.CurrentCulture.Name)
            Thread.CurrentThread.CurrentCulture = New CultureInfo("pt-BR")
            ' Change the current culture to Portuguese (Brazil).
            Console.WriteLine("Current culture changed to {0}",
                            Thread.CurrentThread.CurrentCulture.Name)
            Console.WriteLine("Application thread is thread {0}",
                            Thread.CurrentThread.ManagedThreadId)
            ' Launch six tasks and display their current culture.
            For ctr As Integer = 0 To 5
                tasks.Add(Task.Run(Sub()
                                       Console.WriteLine("Culture of task {0} on thread {1} is {2}",
                                                     Task.CurrentId,
                                                     Thread.CurrentThread.ManagedThreadId,
                                                     Thread.CurrentThread.CurrentCulture.Name)
                                   End Sub))
            Next
            Task.WaitAll(tasks.ToArray())
        End Sub
    End Module
    ' The example displays output like the following:
    '     The current culture is en-US
    '     Current culture changed to pt-BR
    '     Application thread is thread 9
    '     Culture of task 2 on thread 11 is pt-BR
    '     Culture of task 1 on thread 10 is pt-BR
    '     Culture of task 3 on thread 11 is pt-BR
    '     Culture of task 5 on thread 11 is pt-BR
    '     Culture of task 6 on thread 11 is pt-BR
    '     Culture of task 4 on thread 10 is pt-BR
    

    詳細については、「 カルチャとタスクベースの非同期操作」を参照してください。

  • Windows 上で GetUserDefaultLocaleName 関数を呼び出すか、ICU から uloc_getDefault 関数を呼び出します。ICU は現在、UNIX に似たシステムで、カテゴリ setlocaleを持つ POSIX LC_MESSAGES関数を呼び出します。

システムにインストールされているカルチャまたはユーザーの優先カルチャとは異なる特定のカルチャを設定し、アプリケーションが複数のスレッドを開始する場合、スレッドが実行されているアプリケーション ドメインのGetUserDefaultLocaleName プロパティにカルチャを割り当てない限り、これらのスレッドの現在のカルチャはDefaultThreadCurrentCulture関数によって返されるカルチャになります。

スレッドのカルチャを決定する方法の詳細については、 CultureInfo リファレンス ページの「カルチャとスレッド」セクションを参照してください。

現在のカルチャを取得する

CultureInfo.CurrentCulture プロパティはスレッド単位の設定です。つまり、各スレッドは独自のカルチャを持つことができます。 次の例に示すように、 CultureInfo.CurrentCulture プロパティの値を取得することで、現在のスレッドのカルチャを取得します。

using System;
using System.Globalization;

public class Example5
{
   public static void Main()
   {
      CultureInfo culture = CultureInfo.CurrentCulture;
      Console.WriteLine($"The current culture is {culture.NativeName} [{culture.Name}]");
   }
}

// The example displays output like the following:
//       The current culture is English (United States) [en-US]
Imports System.Globalization

Module Example3
    Public Sub S1()
        Dim culture As CultureInfo = CultureInfo.CurrentCulture
        Console.WriteLine("The current culture is {0} [{1}]",
                        culture.NativeName, culture.Name)
    End Sub
End Module
' The example displays output like the following:
'     The current culture is English (United States) [en-US]

CurrentCulture プロパティを明示的に設定する

既存のスレッドによって使用されるカルチャを変更するには、 CultureInfo.CurrentCulture プロパティを新しいカルチャに設定します。 次の例では、現在のスレッド カルチャをオランダ語 (オランダ) に変更します。

using System;
using System.Globalization;
using System.Threading;

public class Info11 : MarshalByRefObject
{
    public void ShowCurrentCulture()
    {
        Console.WriteLine($"Culture of thread {Thread.CurrentThread.Name}: {CultureInfo.CurrentCulture.Name}");
    }
}

public class SetCultureExample
{
    public static void Run()
    {
        Info11 inf = new Info11();
        // Set the current culture to Dutch (Netherlands).
        Thread.CurrentThread.Name = "MainThread";
        CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("nl-NL");
        inf.ShowCurrentCulture();
    }
}

// The example displays the following output:
//
//     Culture of thread MainThread: nl-NL

Note

CultureInfo.CurrentCulture プロパティを使用してカルチャを変更するには、SecurityPermission 値の設定と ControlThread アクセス許可が必要です。 スレッドの操作は、スレッドに関連付けられているセキュリティ状態のために危険です。 そのため、このアクセス許可は信頼できるコードにのみ付与し、必要に応じてのみ付与する必要があります。 半信頼コードでスレッド カルチャを変更することはできません。

現在のスレッド カルチャを、特定のカルチャ (フランス語 (カナダ) など) またはニュートラル カルチャ (フランス語など) に明示的に変更できます。 CultureInfo オブジェクトがニュートラル カルチャを表す場合、CultureInfoCalendarCompareInfoDateTimeFormatNumberFormatなどのTextInfoプロパティの値には、ニュートラル カルチャに関連付けられている特定のカルチャが反映されます。 たとえば、英語のニュートラル カルチャの主要なカルチャは英語 (米国) です。ドイツ文化の主要な文化はドイツ語 (ドイツ) です。 次の例は、現在のカルチャが特定のカルチャ、フランス語 (カナダ)、ニュートラル カルチャであるフランス語に設定されている場合の書式設定の違いを示しています。

using System;
using System.Globalization;
using System.Threading;

public class Example12
{
   public static void Main()
   {
      double value = 1634.92;
      CultureInfo.CurrentCulture = new CultureInfo("fr-CA");
      Console.WriteLine($"Current Culture: {CultureInfo.CurrentCulture.Name}");
      Console.WriteLine($"{value:C2}\n");

      Thread.CurrentThread.CurrentCulture = new CultureInfo("fr");
      Console.WriteLine($"Current Culture: {CultureInfo.CurrentCulture.Name}");
      Console.WriteLine($"{value:C2}");
   }
}

// The example displays the following output:
//       Current Culture: fr-CA
//       1 634,92 $
//
//       Current Culture: fr
//       1 634,92 €
Imports System.Globalization
Imports System.Threading

Module Example4
    Public Sub S1()
        Dim value As Double = 1634.92
        CultureInfo.CurrentCulture = New CultureInfo("fr-CA")
        Console.WriteLine("Current Culture: {0}",
                        CultureInfo.CurrentCulture.Name)
        Console.WriteLine("{0:C2}", value)
        Console.WriteLine()

        Thread.CurrentThread.CurrentCulture = New CultureInfo("fr")
        Console.WriteLine("Current Culture: {0}",
                        CultureInfo.CurrentCulture.Name)
        Console.WriteLine("{0:C2}", value)
    End Sub
End Module
' The example displays the following output:
'       Current Culture: fr-CA
'       1 634,92 $
'       
'       Current Culture: fr
'       1 634,92 €

現在のカルチャとユーザーのオーバーライド

Windows では、コントロール パネルのCultureInfoを使用して、 オブジェクトとその関連オブジェクトの標準プロパティ値をオーバーライドできます。 CultureInfo プロパティによって返されるCurrentCulture オブジェクトは、次の場合にこれらのユーザーのオーバーライドを反映します。

  • 現在のスレッド カルチャが Windows GetUserDefaultLocaleName 関数によって暗黙的に設定されている場合。
  • DefaultThreadCurrentCulture プロパティで定義されている現在のスレッド カルチャが現在の Windows システム カルチャに対応する場合。
  • 現在のスレッド カルチャが、 CreateSpecificCulture メソッドによって返されるカルチャに明示的に設定され、そのカルチャが現在の Windows システム カルチャに対応する場合。
  • 現在のスレッド カルチャが、 CultureInfo(String) コンストラクターによってインスタンス化されたカルチャに明示的に設定され、そのカルチャが現在の Windows システム カルチャに対応する場合。

場合によっては、特にサーバー アプリケーションの場合、ユーザーのオーバーライドを反映する CultureInfo オブジェクトに現在のカルチャを設定することは望ましくない場合があります。 代わりに、次の方法でユーザーのオーバーライドを反映しない CultureInfo オブジェクトに現在のカルチャを設定できます。

適用対象

こちらもご覧ください