Setting the CurrentUICulture


Now that we have made our form localizable and have localized it, our user needs a way to see the French form. Recall from Chapter 3, "An Introduction to Internationalization," that the user interface of our application is determined by the resources loaded by the ResourceManager, and that the GetString and GetObject methods default the culture to CultureInfo.CurrentUICulture, and that CurrentUICulture gets its default from the Win32 GetUserDefaultUILanguage function, which is normally the language version of Windows installed on the machine (except in Windows MUI, where it can be set by the user). So the only time we would see the French version of our application is when it is run on a French version of Windows. This is standard Windows behavior, but in some situations, you might need a more flexible solution.

In Chapter 3, you also learned that CurrentUICulture has a sister property, CurrentCulture, which determines the defaults for the .NET Framework's globalization classes. The user does have direct control over this value by setting the culture in the Regional and Language Options control panel applet. Though Microsoft recommends keeping these settings distinct, a common approach is to use the Regional and Language Options dialog to set both the CurrentCulture and the CurrentUICulture. To do this, add the following line as early in the application's startup process as possible (in our simple example, this would be in the application's main form's constructor, before the call to InitializeComponent).

 Thread.CurrentThread.CurrentUICulture =     Thread.CurrentThread.CurrentCulture; 


(This assumes that there is a suitable using System.Threading in the file.) This solves the problem but might be inefficient in your scenario. If you offer only neutral cultures in your application (e.g., French, German, Spanish) and do not offer specific cultures (e.g., French (Canada), German (Germany), Spanish (Mexico)), the previous line will result in unnecessary checking for the missing specific resource. A better approach would be to use the specific culture's parent:

 Thread.CurrentThread.CurrentUICulture =     Thread.CurrentThread.CurrentCulture.Parent; 


Notice that we don't need to check whether CurrentCulture has a parent because CurrentCulture is always culture specific.

Of course, if the user is using Windows MUI, the user already has control over this problem because he can set the user interface in Regional and Language Options. Despite this, the user might be confused or annoyed by having to specify the culture twice in the Regional and Language Options (once for the CurrentCulture and once for the CurrentUICulture), so you might like to make one setting the dominant setting and use it for both. If this is the case, you should still use the globalization setting instead of the language setting because CurrentCulture must include a region, whereas the language setting in Regional and Language Options does not require this.

If using Regional and Language Options doesn't suit your needs, there are alternatives. You could accept a command-line switch. You could load the setting from a configuration file or database. You could let the user specify the language of choice in a menu option (see the section "Changing the Culture During Execution").

If you are using Visual Studio 2005, the simplest solution is to maintain the user's setting in the application's settings file. In Solution Explorer, right-click the Windows Forms project, select Properties, and select the Settings tab. Enter a name for the setting (e.g., UICulture) and a value (e.g., "fr-FR"). The settings are stored in an XML file called Settings.settings. Visual Studio 2005 automatically maintains a strongly typed Settings class for this file in Settings.Designer.cs. The values can be accessed using the Settings class in the application's Properties name-space. The previous example for setting the CurrentUICulture now looks like this:

 AppSettingsReader reader = new AppSettingsReader(); Thread.CurrentThread.CurrentUICulture =     new CultureInfo(Properties.Settings.Default.UICulture); 





.NET Internationalization(c) The Developer's Guide to Building Global Windows and Web Applications
.NET Internationalization: The Developers Guide to Building Global Windows and Web Applications
ISBN: 0321341384
EAN: 2147483647
Year: 2006
Pages: 213

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net