Loading...
 
Multi-Language Add-In for Visual Studio

Multi-Language Add-In for Visual Studio


Selecting the language from a ComboBox

Germany

This information is in reply to a question on Visual Studio Marketplace.

The following is a rough description of how to add a combobox to a WinForms application to select the language at run time.

In order to switch the language at runtime, you will have to select the option "Support for Runtime Language Switching". This command is on the runtime support menu, which is the 3rd button on Multi-Language window's toolbar.

Image

This adds a function ml_UpdateControls() to every Form and UserControl to update the UI.

It also adds a new Project to the solution called MLRuntime. The only purpose of this project is to broadcast an event when the language is changed. If your application contains multiple class libraries, then this will work across the component boundaries. If your application is a single project, then you can move the class MLRuntime into your project.

I added a combobox named cboLanguage to my main form.
I set the property DisplayMember to "DisplayName".

Image

I am going to add CultureInfo items to the combobox. I want the combobox to display the property "DisplayName" of the CultureInfo class.

In the code behind, I added a definition of the supported languages:

private static string[] SupportedCultures = { "en", "de" } ;

By the way, if you use the ml_string() function, Multi-Language generates that definition in the MLString module. I might extent that in future to generate it in a different module.

In the function InitializeComponent, I added the code

foreach (String IetfTag in SupportedCultures)
{
  try
  {
    CultureInfo Cult = new CultureInfo(IetfTag);
    cboLanguage.Items.Add ( Cult ) ;

    if ( Cult.Equals ( Thread.CurrentThread.CurrentUICulture ) )
      cboLanguage.SelectedItem = Cult ;
  }
  catch{}
}

// If we didn't already select the current language, 
// then select the first language in the list.
if ( cboLanguage.SelectedItem == null )
{
  cboLanguage.SelectedItem = cboLanguage.Items[0] ;
}


The first part initialises the combobox items with the CultureInfo objects. If one of them is the current culture, then is makes it the selected item.

If none of the items was selected, then the second part just selects the first item in the list.

I placed this code after the code inserted by Multi-Language to hook into the LanguageChanged event. That way it should immediately be able to handle language changed events.

Lastly, I added a handler for the SelectedValueChanged event, as follows:

private void cboLanguage_SelectedValueChanged(object sender, EventArgs e)
{
  var SelectedCulture = cboLanguage.SelectedItem as CultureInfo ;
  Thread.CurrentThread.CurrentUICulture = SelectedCulture ;
  MLRuntime.MLRuntime.BroadcastLanguageChanged() ;
}


On selecting an item in the list, this:

  • sets the CurrentUICulture, which is effective for any new modal forms
  • broadcasts the language changed event, which is effective for the current UI, including any UserControls which have attached to this event.


I'm sure that you can make much more elegant code, but that is just one example of how it might work.