CultureInfo.CurrentUICulture CultureInfo.CurrentUICulture CultureInfo.CurrentUICulture CultureInfo.CurrentUICulture Property

Definition

Gets or sets the CultureInfo object that represents the current user interface culture used by the Resource Manager to look up culture-specific resources at run time.

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

Property Value

The culture used by the Resource Manager to look up culture-specific resources at run time.

Exceptions

The property is set to a culture name that cannot be used to locate a resource file. Resource filenames can include only letters, numbers, hyphens, or underscores.

Examples

The following code example demonstrates how to change the CurrentCulture and CurrentUICulture of the current thread.

using namespace System;
using namespace System::Globalization;
using namespace System::Threading;

int main()
{
   // Display the name of the current thread culture.
   Console::WriteLine("CurrentCulture is {0}.", CultureInfo::CurrentCulture->Name);
   
   // Change the current culture to th-TH.
   CultureInfo::CurrentCulture = gcnew CultureInfo("th-TH",false);
   Console::WriteLine("CurrentCulture is now {0}.", CultureInfo::CurrentCulture->Name);
   
   // Displays the name of the CurrentUICulture of the current thread.
   Console::WriteLine("CurrentUICulture is {0}.", CultureInfo::CurrentCulture->Name);
   
   // Changes the CurrentUICulture of the current thread to ja-JP.
   CultureInfo::CurrentUICulture = gcnew CultureInfo("ja-JP",false);
   Console::WriteLine("CurrentUICulture is now {0}.", CultureInfo::CurrentCulture->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.
using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()  
   {
      // Display the name of the current thread 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 Example
   Public Sub Main()

      ' Display the name of the current thread 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.

Remarks

The current UI culture is a per-thread property. That is, each thread has its own current UI culture. This property is equivalent to retrieving or, starting with the .NET Framework 4.6, setting the CultureInfo object assigned to the System.Threading.Thread.CurrentThread.CurrentUICulture property. When a thread is started, its UI culture is initially determined as follows:

  • By retrieving the culture that is specified by the DefaultThreadCurrentUICulture property in the application domain in which the thread is executing, if the property value is not null.

  • If the thread is a thread pool thread that is executing a task-based asynchronous operation and the app targets the .NET Framework 4.6 or a later version of the .NET Framework, its UI culture is determined by the UI culture of the calling thread. The following example changes the current UI culture to Portuguese (Brazil) and launches six tasks, each of which displays its thread ID, its task ID, and its current UI culture. Each of the tasks (and the threads) has inherited the UI culture of the calling thread.

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Runtime.Versioning;
    using System.Threading;
    using System.Threading.Tasks;
    
    [assembly:TargetFramework(".NETFramework,Version=v4.6")]
    
    public class Example
    {
       public static async Task Main()
       {
          var tasks = new List<Task>();
          Console.WriteLine("The current UI culture is {0}", 
                            Thread.CurrentThread.CurrentUICulture.Name);
          Thread.CurrentThread.CurrentUICulture = new CultureInfo("pt-BR");
          // Change the current UI culture to Portuguese (Brazil).
          Console.WriteLine("Current UI culture changed to {0}",
                            Thread.CurrentThread.CurrentUICulture.Name);
          Console.WriteLine("Application thread is thread {0}",
                            Thread.CurrentThread.ManagedThreadId);
          // Launch six tasks and display their current culture.
          for (int ctr = 0; ctr <= 5; ctr++)
             tasks.Add(Task.Run( () => {
                                   Console.WriteLine("UI Culture of task {0} on thread {1} is {2}",
                                                     Task.CurrentId, 
                                                     Thread.CurrentThread.ManagedThreadId,
                                                     Thread.CurrentThread.CurrentUICulture.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.Collections.Generic
    Imports System.Globalization
    Imports System.Runtime.Versioning
    Imports System.Threading
    Imports System.Threading.Tasks
    
    <assembly:TargetFramework(".NETFramework,Version=v4.6")>
    
    Module Example
       Public Sub Main()
          Dim tasks As New List(Of Task)
          Console.WriteLine("The current UI culture is {0}", 
                            Thread.CurrentThread.CurrentUICulture.Name)
          Thread.CurrentThread.CurrentUICulture = New CultureInfo("pt-BR")
          ' Change the current UI culture to Portuguese (Brazil).
          Console.WriteLine("Current culture changed to {0}",
                            Thread.CurrentThread.CurrentUICulture.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.CurrentUICulture.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
    

    For more information, see the "Culture and task-based asynchronous operations" section in the CultureInfo topic.

  • By calling the Windows GetUserDefaultUILanguage function.

Note

In the .NET Compact Framework, the CurrentUICulture property is read-only. The current UI culture is determined by the system's regional settings and cannot be changed programmatically.

Starting with the .NET Framework 4.6, to change the user interface culture used by a thread, set the Thread.CurrentUICulture property to the new culture. If you explicitly change a thread's UI culture in this way, that change persists if the thread crosses application domain boundaries.

Note

In the .NET Framework 4.5.2 and earlier versions, the CurrentUICulture property is read-only; that is, you can retrieve the property value, but you cannot set it. To change the current UI culture, you assign the CultureInfo object that represents the new UI culture to the Thread.CurrentThread.CurrentUICulture property. Starting with the .NET Framework 4.6, the CultureInfo.CurrentUICulture property is read-write; you can both set and retrieve the property's value. If you do set the property value to a CultureInfo object that represents a new culture, the value of the Thread.CurrentThread.CurrentCulture property also changes.

In this section:

Getting the Current UI Culture
Explicitly Setting the Current UI Culture
Implicitly Setting the Current UI Culture
Security Considerations
The Current UI Culture and Windows apps

Getting the Current UI Culture

The CultureInfo.CurrentUICulture property is a per-thread setting; that is, each thread can have its own UI culture. You get the UI culture of the current thread by retrieving the value of the CultureInfo.CurrentUICulture property, as the following example illustrates.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CultureInfo culture = CultureInfo.CurrentUICulture;
      Console.WriteLine("The current UI culture is {0} [{1}]",
                        culture.NativeName, culture.Name);
   }
}
// The example displays output like the following:
//       The current UI culture is English (United States) [en-US]
Imports System.Globalization

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

You can also retrieve the value of the current thread's UI culture from the Thread.CurrentUICulture property.

Explicitly Setting the Current UI Culture

Starting with the .NET Framework 4.6, you can change the current UI culture by assigning a CultureInfo object that represents the new culture to the CultureInfo.CurrentUICulture property. The current UI culture can be set to either a specific culture (such as en-US or de-DE) or to a neutral culture (such as en or de). The following example sets the current UI culture to fr-FR or French (France).

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("The current UI culture: {0}", 
                        CultureInfo.CurrentUICulture.Name);
      
      CultureInfo.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR");
      Console.WriteLine("The current UI culture: {0}", 
                        CultureInfo.CurrentUICulture.Name);
   }
}
// The example displays output like the following:
//       The current UI culture: en-US
//       The current UI culture: fr-FR
Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Main()
      Console.WriteLine("The current UI culture: {0}", 
                        CultureInfo.CurrentUICulture.Name)
      
      CultureInfo.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR")
      Console.WriteLine("The current UI culture: {0}", 
                        CultureInfo.CurrentUICulture.Name)
   End Sub
End Module
' The example displays output like the following:
'       The current UI culture: en-US
'       The current UI culture: fr-FR

In a multithreaded application, you can explicitly set the UI culture of any thread by assigning a CultureInfo object that represents that culture to the thread's Thread.CurrentUICulture property. If the thread whose culture you want to set is the current thread, you can assign the new culture to the CultureInfo.CurrentUICulture property. When the UI culture of a thread is set explicitly, that thread retains the same culture even if it crosses application domain boundaries and executes code in another application domain.

Implicitly Setting the Current UI Culture

When a thread, including the main application thread, is first created, by default its current UI culture is set as follows:

  • By using the culture defined by the DefaultThreadCurrentUICulture property for the current application domain if the property value is not null.

  • By using the system's default culture. On systems that use the Windows operating system, the common language runtime calls the Windows GetUserDefaultUILanguage function to set the current UI culture. GetUserDefaultUILanguage returns the default UI culture set by the user. If the user has not set a default UI language, it returns the culture originally installed on the system.

If the thread crosses application boundaries and executes code in another application domain, its culture is determined in the same way as that of a newly created thread.

Note that if you set a specific UI culture that is different from the system-installed UI culture or the user's preferred UI culture, and your application starts multiple threads, the current UI culture of those threads will be the culture returned by the GetUserDefaultUILanguage function, unless you assign a culture to the DefaultThreadCurrentUICulture property in the application domain in which the thread is executing.

Security Considerations

Changing the culture of the current thread requires a SecurityPermission permission with the ControlThread value set.

Caution

Manipulating threads is dangerous because of the security state associated with threads. Therefore, this permission should be given only to trustworthy code, and then only as necessary. You cannot change thread culture in semi-trusted code.

The current UI culture and UWP apps

In Universal Windows Platform (UWP) apps, the CurrentUICulture property is read-write, just as it is in .NET Framework and .NET Core apps; you can use it both to get and to set the current culture. However, UWP apps do not distinguish between the current culture and the current UI culture. The CurrentCulture and CurrentUICulture properties map to the first value in the Windows.ApplicationModel.Resources.Core.ResourceManager.DefaultContext.Languages collection.

In .NET Framework and .NET Core apps, the current UI culture is a per-thread setting, and the CurrentUICulture property reflects the UI culture of the current thread only. In UWP apps, the current culture maps to the Windows.ApplicationModel.Resources.Core.ResourceManager.DefaultContext.Languages property, which is a global setting. Setting the CurrentCulture property changes the culture of the entire app; culture cannot be set on a per-thread basis.

Security

SecurityPermission
for setting the current UI culture. Associated enumeration: ControlThread

Applies to

See also