CultureInfo.DefaultThreadCurrentUICulture プロパティ

定義

現在のアプリケーション ドメイン内のスレッドの既定 UI カルチャを取得または設定します。

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

プロパティ値

現在のアプリケーション ドメイン内のスレッドの既定 UI カルチャ、または現在のシステム UI カルチャがアプリケーション ドメイン内の既定スレッド UI カルチャである場合は null

例外

設定操作で、Name プロパティ値が正しくありません。

次の例は、新しいスレッドの現在のカルチャを定義する際の.NET Frameworkの既定の動作を示しています。 英語とロシア語のリソースを使用します。 GreetingStrings.txt という名前の次のテキスト ファイルには、英語リソースが含まれています。

greeting =Hello again!
newGreeting=Hello!

これは、次のコマンドで Resource File Generator を使用して GreetingStrings.resources という名前のバイナリ .resources ファイルにコンパイルされます。

resgen greetingstrings.txt

GreetingStrings.ru-RU.txt という名前の次のテキスト ファイルには、ロシア語のリソースが含まれています。

greeting=Еще раз привет!
newGreeting=Привет!

次のコマンドで リソース ファイル ジェネレーター を使用して、GreetingStrings.ru-RU.resources という名前のバイナリ .resources ファイルにコンパイルされます。

resgen greetingstrings.ru-RU.txt

次に示すアプリケーション コードは、Example1.vb または Example1.cs という名前のファイルに存在します。 これは、Visual Basic コンパイラに対して次のコマンドを使用して実行可能ファイルにコンパイルされます。

vbc Example1.vb /resource:GreetingStrings.resources

C# コンパイラの場合、コマンドは次のようになります。

csc /resource:GreetingStrings.resources Example1.cs

これにより、例の実行可能コードとそのフォールバック カルチャのリソースを含むアセンブリが作成されます。 アセンブリ リンカーを使用して、次のコマンドを使用してロシア語 (ロシア) カルチャのリソース ファイルを作成することもできます。

>al /embed:greetingstrings.ru-RU.resources /c:ru-RU /template:example1.exe /out:ru-RU\Example1.resources.dll

起動時に、既定のシステム カルチャが既にロシア語 (ロシア) であるシステムを除くすべてのシステムで、現在のカルチャと現在の UI カルチャをロシア語 (ロシア) に設定します。 既定のシステム カルチャが既にロシア語 (ロシア) の場合、コードは現在のカルチャと現在の UI カルチャを英語 (米国) に設定します。 次に、 ルーチンを ShowGreeting 呼び出します。これにより、最初に呼び出されるときに単純な文字列が表示され、後続のメソッド呼び出しでは少し異なる文字列が表示されます。 次に、ルーチンも実行する新しいスレッドを作成します ShowGreeting

using System;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Threading;

[assembly:NeutralResourcesLanguageAttribute("en-US")]

public class Example
{
   private static int nGreetings = 0;
   private static ResourceManager rm;

   public static void Main()
   {
      AppDomain domain = AppDomain.CurrentDomain;
      rm = new ResourceManager("GreetingStrings",
                               typeof(Example).Assembly);

      CultureInfo culture = null;
      if (Thread.CurrentThread.CurrentUICulture.Name == "ru-RU")
         culture = CultureInfo.CreateSpecificCulture("en-US");
      else
         culture = CultureInfo.CreateSpecificCulture("ru-RU");

      Thread.CurrentThread.CurrentCulture = culture;
      Thread.CurrentThread.CurrentUICulture = culture;

      ShowGreeting();
      Thread.Sleep(1000);

      Thread workerThread = new Thread(Example.ShowGreeting);
      workerThread.Start();
   }

   private static void ShowGreeting()
   {
      string greeting = nGreetings == 0 ? rm.GetString("newGreeting") :
                                          rm.GetString("greeting");
      nGreetings++;
      Console.WriteLine("{0}", greeting);
   }
}
// The example displays the following output:
//       Привет!
//       Hello again!
Imports System.Globalization
Imports System.Reflection
Imports System.Resources
Imports System.Threading

<Assembly:NeutralResourcesLanguageAttribute("en-US")>

Public Class Example
   Private Shared nGreetings As Integer = 0
   Private Shared rm As ResourceManager

   Public Shared Sub Main()
      Dim domain As AppDomain = AppDomain.CurrentDomain
      rm = New ResourceManager("GreetingStrings", 
                               GetType(Example).Assembly)
                  
      Dim culture As CultureInfo = Nothing
      If Thread.CurrentThread.CurrentUICulture.Name = "ru-RU" Then
         culture = CultureInfo.CreateSpecificCulture("en-US")
      Else
         culture = CultureInfo.CreateSpecificCulture("ru-RU")
      End If   
      Thread.CurrentThread.CurrentCulture = culture
      Thread.CurrentThread.CurrentUICulture = culture

      ShowGreeting()
      Thread.Sleep(1000)

      Dim workerThread As New Thread(AddressOf Example.ShowGreeting)
      workerThread.Start()
   End Sub
   
   Private Shared Sub ShowGreeting()
      Dim greeting As String = CStr(IIf(nGreetings = 0, 
                                        rm.GetString("newGreeting"),
                                        rm.GetString("greeting")))
      nGreetings += 1
      Console.WriteLine("{0}", greeting)   
   End Sub
End Class
' The example displays the following output:
'       Привет!
'       Hello again!

この例の出力に示すように、システム カルチャが英語 (米国) のコンピューターで例を実行すると、メイン スレッドは最初の文字列をロシア語で表示します。 ただし、ワーカー スレッドのカルチャは、アプリケーションの現在のカルチャではなく、現在の Windows システム カルチャから派生しているため、ワーカー スレッドはその文字列を英語で表示します。

次の例では、 DefaultThreadCurrentCulture プロパティと DefaultThreadCurrentUICulture プロパティを使用して、新しいアプリケーション スレッドの現在のカルチャと現在の UI カルチャを定義します。

この例では、前の例と同じリソース ファイルを使用します。 ローカライズされたロシア語リソースをサテライト アセンブリにコンパイルして埋め込むコマンドも同じですが、実行可能アセンブリの名前が変更される点が除きます。

起動時に、既定のシステム カルチャが既にロシア語 (ロシア) であるシステムを除くすべてのシステムで、現在のカルチャと現在の UI カルチャをロシア語 (ロシア) に設定します。 既定のシステム カルチャが既にロシア語 (ロシア) の場合は、現在のカルチャと現在の UI カルチャを英語 (米国) に設定します。 次に、 ルーチンを ShowGreeting 呼び出します。これにより、最初に呼び出されるときに単純な文字列が表示され、後続のメソッド呼び出しでは少し異なる文字列が表示されます。 次に、ルーチンも実行する新しいスレッドを作成します ShowGreeting

using System;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Threading;

[assembly:NeutralResourcesLanguageAttribute("en-US")]

public class Example
{
   private static int nGreetings = 0;
   private static ResourceManager rm;

   public static void Main()
   {
      AppDomain domain = AppDomain.CurrentDomain;
      rm = new ResourceManager("GreetingStrings",
                               typeof(Example).Assembly);

      CultureInfo culture = null;
      if (Thread.CurrentThread.CurrentUICulture.Name == "ru-RU")
         culture = CultureInfo.CreateSpecificCulture("en-US");
      else
         culture = CultureInfo.CreateSpecificCulture("ru-RU");

      CultureInfo.DefaultThreadCurrentCulture = culture;
      CultureInfo.DefaultThreadCurrentUICulture = culture;

      Thread.CurrentThread.CurrentCulture = culture;
      Thread.CurrentThread.CurrentUICulture = culture;

      ShowGreeting();
      Thread.Sleep(1000);

      Thread workerThread = new Thread(Example.ShowGreeting);
      workerThread.Start();
   }

   private static void ShowGreeting()
   {
      string greeting = nGreetings == 0 ? rm.GetString("newGreeting") :
                                          rm.GetString("greeting");
      nGreetings++;
      Console.WriteLine("{0}", greeting);
   }
}
// The example displays the following output:
//       Привет!
//       Еще раз привет!
Imports System.Globalization
Imports System.Reflection
Imports System.Resources
Imports System.Threading

<Assembly:NeutralResourcesLanguageAttribute("en-US")>

Public Class Example
   Private Shared nGreetings As Integer = 0
   Private Shared rm As ResourceManager

   Public Shared Sub Main()
      Dim domain As AppDomain = AppDomain.CurrentDomain
      rm = New ResourceManager("GreetingStrings", 
                               GetType(Example).Assembly)
                  
      Dim culture As CultureInfo = Nothing
      If Thread.CurrentThread.CurrentUICulture.Name = "ru-RU" Then
         culture = CultureInfo.CreateSpecificCulture("en-US")
      Else
         culture = CultureInfo.CreateSpecificCulture("ru-RU")
      End If   
      CultureInfo.DefaultThreadCurrentCulture = culture
      CultureInfo.DefaultThreadCurrentUICulture = culture
       
      Thread.CurrentThread.CurrentCulture = culture
      Thread.CurrentThread.CurrentUICulture = culture

      ShowGreeting()
      Thread.Sleep(1000)

      Dim workerThread As New Thread(AddressOf Example.ShowGreeting)
      workerThread.Start()
   End Sub
   
   Private Shared Sub ShowGreeting()
      Dim greeting As String = CStr(IIf(nGreetings = 0, 
                                        rm.GetString("newGreeting"),
                                        rm.GetString("greeting")))
      nGreetings += 1
      Console.WriteLine("{0}", greeting)   
   End Sub
End Class
' The example displays the following output:
'       Привет!
'       Еще раз привет!

この例の出力に示すように、システム カルチャが英語 (米国) のコンピューターで例を実行すると、メイン スレッドとワーカー スレッドの両方に、ロシア語で文字列が表示されます。

注釈

.NET Framework 4 以前のバージョンでは、既定では、すべてのスレッドの UI カルチャが Windows システム カルチャに設定されています。 現在の UI カルチャが既定のシステム カルチャと異なるアプリケーションの場合、多くの場合、この動作は望ましくありません。 .NET Framework 4.5 では、 プロパティをDefaultThreadCurrentUICulture使用して、アプリケーション ドメイン内のすべてのスレッドの既定の UI カルチャを定義できます。

重要

アプリケーション ドメインで実行されている既存のスレッドの UI カルチャを明示的に設定していない場合は、 プロパティを DefaultThreadCurrentUICulture 設定すると、これらのスレッドのカルチャも変更されます。 ただし、これらのスレッドが別のアプリケーション ドメインで実行される場合、そのカルチャは、そのアプリケーション ドメインの プロパティによって DefaultThreadCurrentUICulture 定義されます。既定値が定義されていない場合は、既定のシステム カルチャによって定義されます。 このため、メイン アプリケーション スレッドのカルチャを常に明示的に設定し、 プロパティを DefaultThreadCurrentUICulture 使用してメイン アプリケーション スレッドのカルチャを定義しないことをお勧めします。

明示的に設定されていない限り、 プロパティの DefaultThreadCurrentUICulture 値は であり null、明示的なカルチャが割り当てられていないアプリケーション ドメイン内のすべてのスレッドの現在のカルチャは、既定の Windows システム カルチャによって定義されます。

カルチャ、スレッド、およびアプリケーション ドメインの詳細については、リファレンス ページの「カルチャとスレッド」および「カルチャとアプリケーション ドメイン」セクションを CultureInfo 参照してください。

適用対象

こちらもご覧ください