CultureInfo.CurrentUICulture Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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 property System::Globalization::CultureInfo ^ CurrentUICulture { System::Globalization::CultureInfo ^ get(); };
public static System.Globalization.CultureInfo CurrentUICulture { get; set; }
public static System.Globalization.CultureInfo CurrentUICulture { get; }
static member CurrentUICulture : System.Globalization.CultureInfo with get, set
static member CurrentUICulture : System.Globalization.CultureInfo
Public Shared Property CurrentUICulture As CultureInfo
Public Shared ReadOnly 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 null.
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 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.
Remarks
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's executing a task-based asynchronous operation, 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 {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 {Thread.CurrentThread.CurrentUICulture.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($"UI Culture of task {Task.CurrentId} on thread {Thread.CurrentThread.ManagedThreadId} is {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-BRImports 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 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 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 UI culture is en-US ' Current UI 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-BRFor more information, see Culture and task-based asynchronous operations.
By calling the Windows
GetUserDefaultUILanguagefunction.
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 {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.CurrentUICulture
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
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: {CultureInfo.CurrentUICulture.Name}");
CultureInfo.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR");
Console.WriteLine($"The current UI culture: {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
GetUserDefaultUILanguagefunction to set the current UI culture.GetUserDefaultUILanguagereturns 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.