11.3 Programmatically Changing the Current Culture

 <  Day Day Up  >  

You want to change an application's current culture during runtime.


Technique

The CurrentCulture property defined within the CultureInfo class allows you to retrieve the currently set culture but doesn't allow you to set it. To specify a new culture for a running application, use the CurrentUICulture property defined in the Thread.CurrentThread object. Create a CultureInfo object by using the new keyword and passing the ISO identifier of the culture and optional country/region identifier or by passing the LCID of the culture:

 
 CultureInfo ci1 = new CultureInfo("en-US"); CultureInfo ci2 = new CultureInfo("de"); CultureInfo ci3 = new CultureInfo( 0x0409 ); CultureInfo ci4 = new CultureInfo( 0x0407 ); 

Change the current culture by setting the CurrentUICulture property defined in the currently executing thread object:

 
 CultureInfo newci = new CultureInfo("de"); Thread.CurrentThread.CurrentUICulture = newci; 

Comments

We wish we could tell you that simply changing the current culture of an application during runtime automatically updates the user interface with the new localized strings for that culture's language. Unfortunately, it doesn't, but there is a way to add that functionality to your application as was done for the application that accompanies this recipe, shown in Figure 11.2.

Figure 11.2. You can change the language of the user interface at runtime by setting the CurrentUICulture property and retrieving control text using the ResourceManager class.

graphics/11fig02.gif

As you are well aware, the InitializeComponent method that the Integrated Development Environment (IDE) generates automatically for a Windows Form application is hidden from view by using a code region. When you set the Localizable property to true within the forms designer, the code for the InitializeComponent method changes drastically. Rather than explicitly setting control properties, the method utilizes the ResourceManager class to look up data within resource files to set the various properties for a Windows Form and its associated controls. Using that method as a guide, you can therefore create the code necessary to reload the user interface for the new culture by creating a ResourceManager object and setting the necessary properties using that object. For instance, when a user clicks on the German radio button on the application shown in Figure 11.2, a new ResourceManager class is created, and the TextBox objects' Text properties are set by looking up the corresponding resource string:

 
 private void rbGerman_CheckedChanged(object sender, System.EventArgs e) {     Thread.CurrentThread.CurrentUICulture = new CultureInfo("de");     ReloadControlText(); } private void ReloadControlText() {     System.Resources.ResourceManager resources =         new System.Resources.ResourceManager(typeof(Form1));     label1.Text = resources.GetString( "label1.Text" );     label2.Text =  resources.GetString("label2.Text" );     label3.Text = resources.GetString( "label3.Text" ); } 
 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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