Edit

Share via


System.Globalization.CultureInfo.CurrentUICulture property

This article provides supplementary remarks to the reference documentation for this API.

The CurrentUICulture property is a per-thread property. That is, each thread has its own current UI culture. This property is equivalent to retrieving or 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;
    
    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 UI culture is en-US
    //     Current UI culture changed to pt-BR
    //     Application thread is thread 9
    //     UI Culture of task 2 on thread 11 is pt-BR
    //     UI Culture of task 1 on thread 10 is pt-BR
    //     UI Culture of task 3 on thread 11 is pt-BR
    //     UI Culture of task 5 on thread 11 is pt-BR
    //     UI Culture of task 6 on thread 11 is pt-BR
    //     UI Culture of task 4 on thread 10 is pt-BR
    
    Imports System.Globalization
    Imports System.Threading
    
    Module Example1
        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 documentation.

  • By calling the Windows GetUserDefaultUILanguage function.

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

If you set the property value to a CultureInfo object that represents a new culture, the value of the Thread.CurrentThread.CurrentCulture property also changes.

Get 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 Example2
{
    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 Example3
    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 set the current UI culture

Starting with .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 Example1
{
    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

Module Example2
    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 set 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.