With Globalization, the world has become a small village, information and trade is negotiated at world level. Companies have to develop and support applications in languages other than their language if they have to remain relevant into this global competition. What is even more challenging is that some of the major languages of the world like Arabic, Hindi and Chinese are not even written in Roman Script, so special features have to be provided to deal with these languages.

In this article, we will see how to localize windows forms to create a multilingual application to give the user the ability to change to his/her preferred language at run-time using Microsoft Visual C#.

Localization is the process of customizing your application to a particular language, culture or locale. In our demonstration project, we have supported 3 languages, English, French, and Spanish. The user can switch between any of these 3 languages at run time and the application will remember the last selected language when started. This same method can be used to support any other required language of the world.

Start a new C# Windows application, ours we are going to name it Multilingual.  Select the form and in the Properties, change the Text Properties to “Multilingual Application” as shown on figure 1 below.

If you don’t know how to create a new project using Microsoft Visual C#, please read this article before continuing: Creating a new project in C#

Multilingual Windows FormFigure 1: Multilingual Windows Form

Drag and drop the MenuStrip from the ToolBox to the form. Type File, in the Sub-Menu, type Language, under the language, type “English“, “Français” and “Español“. To switch the display language to French, one has to click the File Menu, then under Language select Français. The same applies to switching to Spanish, one has to select Español.

Inserting the File Menu

Figure 2: Inserting the File Menu

Drag and drop two TexBoxes, three Labels and one Button a shown on figure 3 below. We are going to build a small interface to calculate the total price of items. The number of items will be interred in the first TextBox and the price per item in the second TextBox. The Calculate Button will calculate the total price of Items and display the results in the LabelDisplay. If wrong values such as non-numerical values are entered into the two TexBoxes, a message Box will notify the user. This will be our default language Interface.

Multilingual English User Interface Figure 3: Multilingual English User Interface

Let us now see how we can create additional language files for this English Interface.

Step 1:

In the Properties window, set the form’s Localizable property to true. You will see the Language property is already set to Default.

Form's Localizable property to trueFigure 3: Form’s Localizable property to true

Step 2:

Set the form’s Language property to French (France). Change the Text property of all the controls and elements on the Form to their French equivalent. Click the Form and change its Text Property from “Multilingual Application” to “Application Multilingue”. Select each control and Change its Text Property, the “File” to “Fichier”, “Language” to “Langue”, “Exit” to “Quitter”, “Number of Items” to “Nombre d’articles”, “Price per Item” to “Prix par article” and “Calculate” to “Calculer”. If need to, resize the control to contain the new text.

Save all, a new French language resource file will be generated (Form1.fr-FR.resx) in the Solution Explorer. Figure 4 below shows the French Interface.

French User Interface

Figure 4: French User Interface

Step 3:

Repeat step 2 to generate the Spanish language resource file. Form1.es-ES.resx resource file will be generated in the Solution Explorer. Figure 5 below shows the Spanish Interface.

Spanish User Interface

Figure 5: Spanish User Interface

Press the F5 key or choose Start Debugging from the Debug menu. Our Application will load in English, French or Spanish depending on the UI language of your operating system.

Note: The UI language used in Windows is a function of the CurrentUICulture setting. If your copy of Windows has a Multilingual User Interface Pack (MUI) installed, you can change UI language in Control Panel. If you have no MUI installed, you can change the current UI culture programmatically, as we are going to explain it next

Step 4:

Create a Settings Variable to save the user selected language settings. Even if  we close our application, the next time it will be reopened, The application will load the preferred language selected by the user. We are going to create a String variable “Language” of User Scope to save our language settings. The default language will be American English “en-US“. Right Click Multilingual Project in the Solution Explorer, then click Properties to open the Project Properties. Click the Settings tab and enter Language in the Name section, select string in the Type and write en-US in the Value as shown on figure 6 below. For a list of Language Values, refer to the Table of Language Culture Names, Codes, and ISO Values

SettingsFigure 6: Settings

In the Code Editor, add the System.Globalization, System.Threading and System.Resources namespace codes below the auto generated namespace codes and the code to Launch the Form with the default/selected language as shown below:

In the Button Calculate Click Event, write the following code to calculate the total price:

Press the F5 key or choose Start Debugging from the Debug menu. The project should start with the default language (en-US which is American English). Now let us code our language Menu to be able to change the language at run time.

Step 5:

In code View, write the following Function code that will be used to change the Language.

In Design View, double Click the English Sub-Menu Item and write the following code:

Press the F5 key or choose Start Debugging from the Debug menu. Use the Menu and change the language to your desired language, you will notice that the language doesn’t change immediately, but if you restart your application, it will change. Let us create a customized  message Box to inform the user to restart the application for the language change to take effect. The other important thing also is to validate the input data before calculating the total price. If a user enters a non-numerical data like a string for example instead of a number, our application will not run, it will crash. Let us create two dialogue boxes to notify the user.

Step 6:

Create Two additional forms, on the Project menu, click Add New Item or Right Click our project in the Solution Explorer and Select Add New Item. In the Templates box, select the Windows Form File template. Type the file name “dlgChangeLanguage.cs” in the Name box.

The file is added to your project in Solution Explorer and automatically opens. Add some few controls, one PictureBox to display a warning image, a Label to display the warning message and two Buttons.  Localize the form as we have learned in step one, translate the different control Text strings in French and Spanish.

Select the Form, change the following Properties:

  • Change the AcceptButton to btnYes (the name of our Yes Button)
  • Change the CancelButton to btnNo (the name of our No Button)
  • Change the ControlBox to False
  • Change the FormBorderStyle to FixedDialog
  • Change the Localizable to True
  • Change the StartPosition to CentreScreen

Repeat step 6 to create another Form to display an error message if the user enters an invalid character. Name this new Form “dlgErrorMessage.cs”. Write the following code in the txtnoItems and txtPriceItem TextBoxes KeyPress Event:

Call the dlgChangeLanguage before changing a language. If a user accept to restart the application, then Restart the application. The code below shows how the English language can be selected, do the same for other languages.

Start the form , if you want to change a language, you will get the warning message as shown on figure 7 below or if you enter an invalid value into the textBox, you will get an error message as shown below on figure 8.

Change of language warning message

Figure 7: Change of language warning message

Invalid value error message displayed in French Figure 8: Invalid value error message displayed in French

You can download the full project files below here. All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).  

Download Multilingual