How to Localize Windows Forms and Change the Language 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 with much ease. 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.
When you run a localized application, the appearance is determined by two culture values. The UICulture property is used to specify which resource files will be loaded for the form. The Culture property, on the other hand, determines how strings such as dates, numerals, and currency amounts are formatted.
Let us see the steps required to create a localized form. You can then expand this example and adopt the same approach for the rest of the forms in your project.
Step 1: Open Visual Studio > File > New > Project. In the Project Types pane, choose the language of your choice (Visual C# or Visual Basic). In the Templates pane, choose Windows Application. Choose a name and location for the project and click OK.
Step 2: With the form opened, set its ‘Localizable’ property to True. Drag a Label control from the Toolbox, and set its Text property to ‘Hello’.
Note: By default, the Language property of the form is set to ‘(Default)’.
Step 3: Now set the form's ‘Language’ property to French (France). Set the label's Text property to ‘bonjour’.
Once again, set the form's Language property, but this time to Spanish (Spain). Set the button's Text property to holà.
Build the Solution. Expand the node in Form1.vb/Form1.cs. As you observe, the resource files appear underneath Form1.vb/Form1.cs. Form1.resx is the resource file for the default culture, Form1.fr-FR.resx is the resource file for French (France) and Form1.es-ES.resx is the resource file for Spanish (Spain). If you cannot see these files for some reason, click the ‘Show All Files’ button in Solution Explorer.
Step 4: Now set the form's ‘Language’ property back to (Default). Drag a ‘ComboBox’ from the Toolbox to the form.
In the Form_Load() add the following code :
C#
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("English");
comboBox1.Items.Add("Spanish");
comboBox1.Items.Add("French");
comboBox1.SelectedIndex = 0;
}
VB.NET
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
comboBox1.Items.Add("English")
comboBox1.Items.Add("Spanish")
comboBox1.Items.Add("French")
comboBox1.SelectedIndex = 0
End Sub
Also add references to the namespaces:
C#
using System.Globalization;
using System.Threading;
VB.NET
Imports System.Globalization
Imports System.Threading
Step 5: Now add a SelectedIndexChanged event handler to the ComboBox. Write the following code:
C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "English")
{
ChangeLanguage("en");
}
else if (comboBox1.SelectedItem.ToString() == "Spanish")
{
ChangeLanguage("es-ES");
}
else
{
ChangeLanguage("fr-FR");
}
}
private void ChangeLanguage(string lang)
{
foreach (Control c in this.Controls)
{
ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
resources.ApplyResources(c, c.Name, new CultureInfo(lang));
}
}
VB.NET
Private Sub comboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
If comboBox1.SelectedItem.ToString() = "English" Then
ChangeLanguage("en")
ElseIf comboBox1.SelectedItem.ToString() = "Spanish" Then
ChangeLanguage("es-ES")
Else
ChangeLanguage("fr-FR")
End If
End Sub
Private Sub ChangeLanguage(ByVal lang As String)
For Each c As Control In Me.Controls
Dim resources As ComponentResourceManager = New ComponentResourceManager(GetType(Form1))
resources.ApplyResources(c, c.Name, New CultureInfo(lang))
Next c
End Sub
As you observe, every time the user selects a new language, the ChooseLanguage() method is called. In this method, we loop through each control on the form and use the ComponentResourceManager to enumerate through the resources for an object. We then use the ApplyResources method to apply a resource's value to the corresponding property of the object. One of the overloads of the ApplyResources accepts 3 parameters: the control that contains the property value to be applied; a string that contains the name of the object to look up in the resources and a culture for which to apply resources.
Note: Notice that each culture name(es-ES) consists of two parts. The first part represents the language code and the second part represents the country/region code. If you specify a culture name and do not provide a country/region code—for example, "en" —then that represents a 'neutral culture'. If you provide both a language code and a country/region code—for example, "es-ES", then that represents a 'specific culture'.
Well that’s it. Run the application and choose your preferred language from the combobox. You will see that the label changes values as set by you while creating the resources. I hope you liked the article and I thank you for viewing it.