This project has moved. For the latest updates, please go here.

[Info] Localization: CurrentCulture vs. CurrentUICulture

Coordinator
Jul 2, 2014 at 7:33 PM
Edited May 14, 2016 at 8:29 PM

This article has been moved to GitHub!


The .NET Framework provides powerful tools to localize an application into various languages. The static code analysis of Visual Studio supports us in writing “world-ready” applications that can be localized without any code changes.

Note: You might need to change the rule set of the code analysis to one that includes the Microsoft Globalization Rules.

The decision which language or culture specific adaption should be used is defined in two different properties:
  • CultureInfo.CurrentCulture
  • CultureInfo.CurrentUICulture
The difference between CurrentCulture and CurrentUICulture is not that obvious. This separation comes from Windows and can be found in the Control Panel.


CurrentUICulture
The CurrentUICulture defines the display language of the application. It is used to get the localized texts from the ResX files. If you use the strongly-typed resource classes that are created by Visual Studio then they use this property setting to get the localized resources.

In the Control Panel this setting is the primary language. In Windows 7 and older Windows versions this looks different but provides the same functionality.

Image
// The default value for CurrentUICulture comes from Windows.
Console.WriteLine(CultureInfo.CurrentUICulture);
Console.WriteLine(LocalizationTest.Properties.Resources.HelloWorld);

// Change the CurrentUICulture for all threads.
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("de-AT");
Console.WriteLine(CultureInfo.CurrentUICulture);
Console.WriteLine(LocalizationTest.Properties.Resources.HelloWorld);
> en-US
> Hello World

> de-AT
> Hallo Welt
CurrentCulture
The CurrentCulture setting is responsible for formatting and parsing of values. Additionally, it is used to control culture-aware implementations of sorting or comparison algorithms.

Windows allows to set this culture to a different value. By default the CurrentUICulture and CurrentCulture are the same but the user can change this.

Image
// The default value for CurrentCulture comes from Windows.
Console.WriteLine(CultureInfo.CurrentCulture);
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, "{0:G}", new DateTime(2014, 7, 5, 13, 12, 11)));
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, "{0:C}", 123456));

// Change the CurrentCulture for all threads.
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("de-AT");
Console.WriteLine(CultureInfo.CurrentCulture);
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, "{0:G}", new DateTime(2014, 7, 5, 13, 12, 11)));
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, "{0:C}", 123456));
> en-US
> 7/5/2014 1:12:11 PM
> $123,456.00

> de-AT
> 05.07.2014 13:12:11
> € 123.456,00
When to pass CurrentUICulture and when CurrentCulture for a .NET Framework method?
In most situations the CurrentCulture is the right object that needs to be provided to culture-aware .NET methods.
string.Format(CultureInfo.CurrentCulture, "{0:C}", 123456);
The CurrentUICulture is only needed for resource lookup by the ResourceManager. However, if you use the ResourceManager’s methods that do not require a CultureInfo object then the ResourceManager takes the CurrentUICulture property by default.