I would like to test how my app would work under different cultures.
So for testing purposes, under Windows 7, I tried to change CurrentUICulture
in system settings.
This seems to be the right option: Language for non-Unicode programs
as suggested here, but it does not work, i.e. app's locale is still English.
I also tried this in Region and Language
dialog:
The question is what should I set in Windows 7 for it to affect:
Thread.CurrentThread.CurrentUICulture
instead of having to write this:
Thread.CurrentThread.CurrentUICulture = New CultureInfo("fr")
Ultimately, this code should pick the correct culture, get the correctly suffixed resource file and display it on the screen (which it does with the above line in place):
Label1.Text = My.Resources.Form1Resource.TestString
A similar question has been asked on StackOverflow, but none of the answers addressed this issue.
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.
CurrentCulture is the . NET representation of the default user locale of the system. This controls default number and date formatting and the like e.g. whether or not a number uses , or . as a group seperator (e.g. 1,99 or 1.99) as well as the associated currency symbol.
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.
The CultureInfo. InvariantCulture property is used if you are formatting or parsing a string that should be parseable by a piece of software independent of the user's local settings. The default value is CultureInfo. InstalledUICulture so the default CultureInfo is depending on the executing OS's settings.
The knob is on the "Keyboard and Languages" tab of the "Region and Language" control panel. Click on the "Install/uninstall languages…" button to get started. If you only have one UI language installed, you will need to install another one. The wizard should walk you through this. You will also have to log off and log back on in order to see the effect.
In most cases, the CurrentUICulture
property is going to return the first language in the list of user preferred UI languages, so setting this should be sufficient. The other languages are used as fallback languages in case necessary resources are not available in the preferred language.
But the actual algorithm that CurrentUICulture
uses to determine the UI culture is a bit more complicated:
DefaultThreadCurrentUICulture
property. If that is not null
, it returns whatever UI culture has been set as the default for all threads in the current application domain.DefaultThreadCurrentUICulture
is null
, it calls the GetUserDefaultUILanguage
function.
Of course, this method of testing might be a little overkill because it's changing global settings (at least, for your entire user account). Because the current UI culture is maintained per-thread, you can modify it just for your application's thread. To do this, set the Thread.CurrentUICulture
property. If your application is multi-threaded, you might want to set the DefaultThreadCurrentUICulture
property to ensure that additional threads pick up the correct culture. The question says that you already found this, but I'm not clear on why you don't want to use it.
Also, be careful about confusing the UI language with the locale; they are not the same. CurrentCulture
is the locale, which sets things like date/number/time formats and the sort order. CurrentUICulture
is the UI language, which deals with loading the correctly localized resources. They might be the same, and I suppose they often are, but they do not have to be. There are cases where a user might want them to be different; for example, if they speak English and prefer the localized English version, but want to see things like dates and times formatted according to their custom.
You can set UI culture System.Globalization.CultureInfo.CurrentUICulture
on windows machine in: Control Panel\Clock, Language, and Region\Language
:
System.Globalization.CultureInfo.CurrentUICulture
And in case if you want to set System.Globalization.CultureInfo.CurrentCulture
in your windows machine go to Control Panel\Clock, Language, and Region
,and select Region:
System.Globalization.CultureInfo.CurrentCulture
In your code you can just use this to get those values:
Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.CurrentUICulture;
Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CurrentCulture;
Source: CurrentCulture vs. CurrentUICulture
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With