CultureInfo.CurrentUICulture Property

 

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.

Namespace:   System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

public static CultureInfo CurrentUICulture { get; set; }

Property Value

Type: System.Globalization.CultureInfo

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

Exception Condition
ArgumentNullException

The property is set to null.

ArgumentException

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.

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 void 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);
                                } ));                     
    
          Task.WaitAll(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
    

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

  • By calling the Windows GetUserDefaultUILanguage function.

System_CAPS_noteNote

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.

System_CAPS_noteNote

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

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]

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

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 iCultureInfo.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

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.

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.

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

System_CAPS_cautionCaution

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.

In Windows apps, the CurrentUICulture property is read-only. You can set the current UI culture by using the Windows ResourceContext.Languages property. The following example uses it to change the application's current UI culture either to English (United States) or, if the current culture is already English (United States), to French (France). The value of the CurrentCulture and CurrentUICulture properties are then displayed to a TextBlock control named block. As the output from the example shows, the current UI culture changes when the ResourceContext.Languages property changes.


ResourceContext context = ResourceManager.Current.DefaultContext;
var list = new List<String>();
if (CultureInfo.CurrentCulture.Name == "en-US")
   list.Add("fr-FR");
else
   list.Add("en-US");

context.Languages = list;
block.Text += String.Format("\nThe current culture is {0}\n",
                            CultureInfo.CurrentCulture);
block.Text += String.Format("The current culture UI is {0}\n",
                            CultureInfo.CurrentUICulture);
// The example displays output like the following if run on a system
// whose current culture is English (United States):
// The current culture is fr-FR
// The current UI culture is fr-FR   


Dim context As ResourceContext = ResourceManager.Current.DefaultContext
Dim list As New List(Of String)()
If CultureInfo.CurrentCulture.Name = "en-US" Then
   list.Add("fr-FR")
Else
   list.Add("en-US")
End If
context.Languages = list
block.Text += String.Format("{1}The current culture is {0}{1}",
                            CultureInfo.CurrentCulture, vbCrLf)
block.Text += String.Format("The current culture UI is {0}\n",
                            CultureInfo.CurrentUICulture)
' The example displays output Like the following if run on a system
' whose current culture Is English (United States):
'    The current culture Is fr-FR
'    The current UI culture Is fr-FR   

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

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.

SecurityPermission

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: