Create new account I forgot my password    

How to Localize Windows Forms and Change the Language at Runtime
Rating: 20 user(s) have rated this article Average rating: 4.4
Posted by: Suprotim Agarwal, on 7/15/2008, in category "Windows Forms 2.0"
Views: this article has been read 30798 times
Abstract: 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.

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.
If you liked the article,  Subscribe to my RSS Feed.









Page copy protected against web site content infringement by Copyscape


How would you rate this article?

User Feedback
Comment posted by Alain Henriot on Tuesday, July 22, 2008 4:33 PM
Very well. Very clearly. But how do you make the obfuscation of this program with dotfuscator CE in Visual Studio ?
Thank
Comment posted by Deepak Dagar on Wednesday, July 23, 2008 6:49 AM
Very good article & Thanks for sharing with all.
I have a question, suppose we have 100 controls(labels/buttons etc.) on the form & we have to localize it in three languages.
Then I feel this approach will be very difficult.
Could you suggest solution for this problem?
Thanks again for good work.
Comment posted by Chathura on Wednesday, July 30, 2008 3:42 AM
Excellent and thanks a lot for sharing knowledge.
Comment posted by lasa on Monday, August 18, 2008 10:44 AM
cool, the idea helped me allot in my C++.net program

private: System::Void LanguageSettings_Load(System::Object^  sender, System::EventArgs^  e) {
          comboBox1->Items->Add("English");
          comboBox1->Items->Add("French");
          comboBox1->Items->Add("Spanish");
       }

   private: System::Void comboBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
             if(comboBox1->SelectedItem::get()=="English"){
                ChangeLanguage("en");
             }else if(comboBox1->SelectedItem::get()=="French"){
               ChangeLanguage("fr-FR");
             }else if(comboBox1->SelectedItem::get()=="Spanish"){
               ChangeLanguage("es-ES");
             }
          }

      private: void ChangeLanguage(String^ lang)
        {
            for each (Control ^c in this->Controls){
                ComponentResourceManager^ resources = gcnew ComponentResourceManager(this->GetType());
                resources->ApplyResources(c, c->Name, gcnew System::Globalization::CultureInfo(lang));
         }
        }
Comment posted by Suprotim Agarwal on Monday, August 18, 2008 9:10 PM
lasa: Thanks for sharing your C++ solution!!
Comment posted by ChrissDeGrece on Monday, September 22, 2008 1:08 PM
Thanks for sharing this nice information.

Unfortunately it changes only buttons and texts on the form, the toolbar buttons text is not changed. Any suggestions on that?

Thanks

Comment posted by abdoureda on Wednesday, October 08, 2008 12:10 PM
But How Can I return back to (default) language not a specific language like "es-es"
Comment posted by Suprotim Agarwal on Thursday, October 09, 2008 6:04 AM
Isn't the combo box working for you? Selecting Spanish in the combobox should change the culture.
Comment posted by .NET Dev on Monday, November 03, 2008 2:12 AM
This is also good article:
http://urenjoy.blogspot.com/2008/11/windows-form-localization-in-net.html
Comment posted by tagtog on Monday, December 29, 2008 12:46 AM
OK, it's good. but how can i deal with positioning in right to left languages?
Comment posted by samehsenosi on Sunday, January 03, 2010 4:52 AM
you can switch to right to left by the following sample code

'to switch Form Layout:
Me.RightToLeft = Windows.Forms.RightToLeft.Yes
Me.RightToLeftLayout = True
'to switch controls:
            For Each c As Control In Me.Controls
                c.RightToLeft = Windows.Forms.RightToLeft.Yes
            Next
Comment posted by marko on Thursday, March 25, 2010 6:52 AM
Hi, i have a problem with localization. I have about 300 forms in my project, and i cannot set localizable property to true (i have to do this to generate resource files for 2 additional languages) for all of them manualy, it would take forever. I want to do this from code. Is it even possible?

Thanks
Comment posted by marko on Thursday, March 25, 2010 7:08 AM
Hi, i have a problem with localization. I have about 300 forms in my project, and i cannot set localizable property to true (i have to do this to generate resource files for 2 additional languages) for all of them manualy, it would take forever. I want to do this from code. Is it even possible?

Thanks
Comment posted by faraz on Friday, May 28, 2010 8:17 AM
Very good article & Thanks for sharing with all.
I have problem with type of ComponentResourceManager in VB 2010 .
" Error   1   Type 'ComponentResourceManager' is not defined "
code :
  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")
        ElseIf ComboBox1.SelectedItem.ToString() = "French" Then
            ChangeLanguage("fr-FR")
        Else
            ChangeLanguage("ar")
        End If
    End Sub

    Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ComboBox1.Items.Add("English")
        ComboBox1.Items.Add("Spanish")
        ComboBox1.Items.Add("French")
        ComboBox1.Items.Add("Arabic")
        ComboBox1.SelectedIndex = 0
    End Sub

    Private Sub ChangeLanguage(ByVal lang As String)
        For Each c As Control In Me.Controls
            Dim resources As ComponentResourceManager = New ComponentResourceManager(GetType(Login))
            resources.ApplyResources(c, c.Name, New CultureInfo(lang))
        Next c
    End Sub
Comment posted by Chris on Monday, July 05, 2010 2:12 PM
Thank you very much for this very clear explanation!

@ Faraz, you need to put on the top : Imports System.ComponentModel
Comment posted by KommradHomer on Monday, July 26, 2010 11:53 AM
im doing all these and the applyresource method is being called too as i check with breaks on debug.but nothing changes at all. some people suggest to initializeComponent method but its doubling all the items so it doesnt work either. whats the problem ?

Post your comment
Name:
E-mail: (Will not be displayed)
Comment:
Insert Cancel

NEWSLETTER