注
この記事では、この API のリファレンス ドキュメントに補足的な解説を提供します。
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.Runtime.Versioning; 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-BRImports 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を持つ POSIXLC_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.CurrentThread.Name} in application domain {AppDomain.CurrentDomain.FriendlyName}: {CultureInfo.CurrentCulture.Name}");
}
}
public class Example11
{
public static void Main()
{
Info11 inf = new Info11();
// Set the current culture to Dutch (Netherlands).
Thread.CurrentThread.Name = "MainThread";
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("nl-NL");
inf.ShowCurrentCulture();
// Create a new application domain.
AppDomain ad = AppDomain.CreateDomain("Domain2");
Info11 inf2 = (Info11)ad.CreateInstanceAndUnwrap(typeof(Info11).Assembly.FullName, "Info11");
inf2.ShowCurrentCulture();
}
}
// The example displays the following output:
// Culture of MainThread in application domain ChangeCulture1.exe: nl-NL
// Culture of MainThread in application domain Domain2: nl-NL
Imports System.Globalization
Imports System.Threading
Public Class Info : Inherits MarshalByRefObject
Public Sub ShowCurrentCulture()
Console.WriteLine("Culture of {0} in application domain {1}: {2}",
Thread.CurrentThread.Name,
AppDomain.CurrentDomain.FriendlyName,
CultureInfo.CurrentCulture.Name)
End Sub
End Class
Module Example2
Public Sub S1()
Dim inf As New Info()
' Set the current culture to Dutch (Netherlands).
Thread.CurrentThread.Name = "MainThread"
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("nl-NL")
inf.ShowCurrentCulture()
' Create a new application domain.
Dim ad As AppDomain = AppDomain.CreateDomain("Domain2")
Dim inf2 As Info = CType(ad.CreateInstanceAndUnwrap(GetType(Info).Assembly.FullName, "Info"),
Info)
inf2.ShowCurrentCulture()
End Sub
End Module
' This example displays the following output:
' Culture of MainThread in application domain Example.exe: nl-NL
' Culture of MainThread in application domain Domain2: nl-NL
注
CultureInfo.CurrentCulture プロパティを使用してカルチャを変更するには、SecurityPermission 値の設定と ControlThread アクセス許可が必要です。 スレッドの操作は、スレッドに関連付けられているセキュリティ状態のために危険です。 そのため、このアクセス許可は信頼できるコードにのみ付与し、必要に応じてのみ付与する必要があります。 半信頼コードでスレッド カルチャを変更することはできません。
.NET Framework 4 以降では、現在のスレッド カルチャを、特定のカルチャ (フランス語 (カナダ) など) またはニュートラル カルチャ (フランス語など) に明示的に変更できます。 CultureInfo オブジェクトがニュートラル カルチャを表す場合、CultureInfo、Calendar、CompareInfo、DateTimeFormat、NumberFormatなどの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 €
次の例に示すように、 CultureInfo.CurrentCulture プロパティと HttpRequest.UserLanguages プロパティを使用して、ASP.NET アプリケーションの CurrentCulture プロパティをユーザーの優先カルチャに設定することもできます。
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture(Request13.UserLanguages[0]);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages(0))
現在のカルチャとユーザーのオーバーライド
Windows では、コントロール パネルのCultureInfoを使用して、 オブジェクトとその関連オブジェクトの標準プロパティ値をオーバーライドできます。 CultureInfo プロパティによって返されるCurrentCulture オブジェクトは、次の場合にこれらのユーザーのオーバーライドを反映します。
現在のスレッド カルチャが Windows
GetUserDefaultLocaleName関数によって暗黙的に設定されている場合。DefaultThreadCurrentCulture プロパティで定義されている現在のスレッド カルチャが現在の Windows システム カルチャに対応する場合。
現在のスレッド カルチャが、 CreateSpecificCulture メソッドによって返されるカルチャに明示的に設定され、そのカルチャが現在の Windows システム カルチャに対応する場合。
現在のスレッド カルチャが、 CultureInfo(String) コンストラクターによってインスタンス化されたカルチャに明示的に設定され、そのカルチャが現在の Windows システム カルチャに対応する場合。
場合によっては、特にサーバー アプリケーションの場合、ユーザーのオーバーライドを反映する CultureInfo オブジェクトに現在のカルチャを設定することは望ましくない場合があります。 代わりに、次の方法でユーザーのオーバーライドを反映しない CultureInfo オブジェクトに現在のカルチャを設定できます。
CultureInfo(String, Boolean)引数の
false値を使用してuseUserOverrideコンストラクターを呼び出します。キャッシュされた読み取り専用のGetCultureInfo オブジェクトを返すCultureInfo メソッドを呼び出します。
現在のカルチャと UWP アプリ
ユニバーサル Windows プラットフォーム (UWP) アプリでは、.NET Framework アプリと .NET Core アプリの場合と同様に、 CurrentCulture プロパティは読み取り/書き込み可能です。現在のカルチャの取得と設定の両方に使用できます。 ただし、UWP アプリでは、現在のカルチャと現在の UI カルチャは区別されません。 CurrentCultureプロパティとCurrentUICulture プロパティは、Windows.ApplicationModel.Resources.Core.ResourceManager.DefaultContext.Languages コレクションの最初の値にマップされます。
.NET Framework アプリと .NET Core アプリでは、現在のカルチャはスレッドごとの設定であり、 CurrentCulture プロパティには現在のスレッドのカルチャのみが反映されます。 UWP アプリでは、現在のカルチャは、グローバル設定である Windows.ApplicationModel.Resources.Core.ResourceManager.DefaultContext.Languages プロパティにマップされます。 CurrentCulture プロパティを設定すると、アプリ全体のカルチャが変更されます。スレッドごとにカルチャを設定することはできません。
.NET