8

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:

  • Formats: change Format to another culture
  • Location: set current location to another country.

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.

Community
  • 1
  • 1
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • i assume you need to change the display language – NickD Jun 11 '13 at 19:42
  • @Snoopy: I am going through this right now. Gonna grab the offline installer for a language in question, install it, and see if the change is reflected in the app. Windows Update does not let me do it for some reason. Thanks for a hint. – Victor Zakharov Jun 11 '13 at 19:49
  • hmm this looks interesting http://www.froggie.sk/ – NickD Jun 11 '13 at 20:03
  • @Snoopy: Mine is Ultimate, so does not apply, but thanks for bringing it up, others may find it useful. – Victor Zakharov Jun 11 '13 at 20:31

3 Answers3

13

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:

  • First, it checks the 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.
  • If DefaultThreadCurrentUICulture is null, it calls the GetUserDefaultUILanguage function.
    • That function checks to see if the current user has set a preferred UI language, just like I described at the beginning. If so, it returns that language.
    • If the user has not set a UI language, the function returns the UI language that is set for the system. This is done by the administrator in the "Advanced" tab of the "Region and Language" control panel and requires a reboot in order to take effect.
    • If there is no preferred language set for the system, then the system default UI language is used. This is either the language of the localized version of Windows (XP and earlier), or the language that was selected during installation (Vista and later).

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • +1 and accepting. Great answer. Thank you. I did not want to use it because I thought it appears hach-ish. What if you have a dedicated human tester? Would I need to implement an `app.config` framework to switch culture on the fly (which in turn sets CurrentUICulture and stuff) or let them change it in Windows settings? I thought that changing Windows settings would be more simple. Apparently, it is not. :) Thanks for a nice explanation of CurrentCulture vs CurrentUICulture - they are indeed confusing at times. – Victor Zakharov Jun 11 '13 at 20:28
  • @Neolisk Well sure it's kind of hackish. Normally an app shouldn't be setting these types of properties at all! But you're doing it for testing purposes, so it seems justified to me. :-) If you're testing in a dedicated VM, changing the global settings might be fine, but otherwise it seems needlessly intrusive considering the property is per-thread anyway. Your test suite would just set the UI culture programmatically before starting its batch of tests (whether your "test suite" is a person at a keyboard or an automated bank of code). – Cody Gray - on strike Jun 11 '13 at 20:31
2

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

Dan
  • 448
  • 10
  • 20
0

So I tried to install a French language pack for testing purposes, using different ways and they all failed. Both offline and online installation, as is or after restart. Got error code 800736B3 (for online) or no code at all for offline installation. Tried System Update Readiness Tool for Windows 7 (a 390MB msu package), which is a most commonly known way to fix this error code. Still got the same problems with both online and offline installers. Having spend around 4-5 hours on it in total, I'm starting to understand why @Cody recommended to keep using this instead (for testing purposes):

Thread.CurrentThread.CurrentUICulture = New CultureInfo("fr")
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151