How to localize a WinForms application at runtime

Localization is the process of customizing your application to a particular language, culture or locale. Visual Studio provides support for localizing Windows Forms. In this article, we will see how to localize windows forms and give the user the ability to change to his preferred language at runtime.

Sample App

I have created a simple application that includes a simple “hello world” form and supports “en”, “fr-FR” and “de-De” locales.

All the source code can be downloaded here

How does it work?

When you run a localized application, the appearance is determined by two culture values:

UICulture value determines what resource file needs to load. That drives all the content of the form.
Culture value determines the results of culture-dependent functions, such as the date, number, and currency formatting, and so on.

Basically, Culture doesn’t affect the display language (only some “formating”) and UICulture does.

Let us see the steps required to create a localized form

    1. Create a new Windows Forms Application and add new form
    2. In the Properties window, set the form’s Localizable property to true.
      Note: The Language property is already set to (Default).
    3. Drag a Label control from the Toolbox and set its Text property to Hello world.
    4. Set the form’s Language property to French (France).
      Set the label’s Text property to Bonjour le Monde.
    5. Set the form’s Language property to German (Germany).
      Set the label’s Text property to Hallo Welt.

Note: As you observe resorce files appear underneath ExampleForm.cs. ExampleForm.resx is the resource file for the default culture, ExampleForm.fr-FR.resx is the resource file for French (France) and ExampleForm.de-DE.resx is the resource file for Germany (German).

  • Now set the form’s Language property back to (Default). Drag a Combobox from the Toolbox to the form and add in constructor following code:
    CB_Languages.Items.Add(new CultureInfo("en"));
    CB_Languages.Items.Add(new CultureInfo("fr-FR"));
    CB_Languages.Items.Add(new CultureInfo("de-De"));
    
  • Now add a SelectedIndexChanged event handler to the ComboBox. Write the following code:
    private void CB_Languages_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (CB_Languages.SelectedIndex == -1)
            return;
    
        CultureInfo ci = (CultureInfo)CB_Languages.SelectedItem;
        Thread.CurrentThread.CurrentUICulture = ci;
        ComponentResourceManager resources = new ComponentResourceManager(this.GetType());
        ApplyResourceToControl(resources, this);
    }
    
    private static void ApplyResourceToControl(ComponentResourceManager res, Control control)
    {
        foreach (Control c in control.Controls)
            ApplyResourceToControl(res, c);
    
        var text = res.GetString(String.Format("{0}.Text", control.Name));
        control.Text = text ?? control.Text;
    }
    

 

Is there any easier and more professional solution?

As you probably noticed the localization in Visual Studio is problematic for larger projects. Therefore, I suggest to use the best solution on the market. LSACreator requires only compiled program (.dll/.exe files). This approach separates source code from language project and allows to create translation even by end-user without any knowledge about program structure.

Please visit our website for more details: tbs-apps.com/lsacreator
If you have any questions, please feel free to discuss. тиц пр