Localization in VB.NET

Introduction

Localization allows you to customize your application according to a certain culture. Today I will show you how to Localize your application to three different cultures.

It is always good to learn by example, so let's do a project.

Our Project

Our little project is a Windows Forms project with two controls: a Combobox and a label. It looks like Figure 1.

Our design
Figure 1 - Our design

Now, adding localization capabilities isn't that difficult at all. It is actually pretty easy as VB takes care of everything for you. Now, isn't that cool?

In design view, click on the form and open the Properties window.

Select the Form's Language property and you will notice that it is set to Default. The trick here is that when we set it to something else, it will automatically localize our application!

Set the Form's Localizable Property to True. This enables Localization.

Change the form's Language property to English and then change the Label's Text, as shown in the next image:

English Label
Figure 2 - English Label

Change the Form's Language property to French, and change the Label's text to:

French Label
Figure 3 - French Label

You might have noticed that with the very first text you had to supply, that it is a strange language as well. That is Afrikaans, my mother-tongue. We now have three languages at play here: Afrikaans, English, and French.

When a user's Locale has been set to any of those, the associated text will be displayed.

We can take it a step further! Add the next code:

   Imports System.Globalization
		Imports System.ComponentModel  
		
		Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'Add items to combobox
        ComboBox1.Items.Add("English")
        ComboBox1.Items.Add("Afrikaans")
        ComboBox1.Items.Add("French")

    End Sub

    Private Sub comboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

        If ComboBox1.SelectedIndex = 0 Then

            ChangeLanguage("en")

        ElseIf ComboBox1.SelectedIndex = 1 Then

            ChangeLanguage("af-AF")

        Else

            ChangeLanguage("fr-FR")

        End If

    End Sub

This loads items into the combobox, and determines which item has been selected.

Add the final piece of the puzzle:

    Private Sub ChangeLanguage(ByVal Language As String)

        For Each c As Control In Me.Controls

            Dim crmLang As ComponentResourceManager = New ComponentResourceManager(GetType(Form1))

            crmLang.ApplyResources(c, c.Name, New CultureInfo(Language)) 'Set desired language

        Next c

    End Sub

This sets the locale based on the resources. Now where do the resources come from?

Build your project. Once built, expand all the items inside the Solution Explorer. It should resemble Figure 4. You will see that the resource files for Afrikaans as well as French have been created.

Solution Explorer with new resources
Figure 4 - Solution Explorer with new resources

Conclusion

I hope you have enjoyed today's article. This is me signing off!



Related Articles

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • On-demand Event Event Date: July 22, 2014 In this WhatWorks analysis, John Pescatore examines a use case where end users had local administrative rights on their PCs and it had gotten out of hand for this Fortune 500 Energy and Utilities company. The compelling event that prompted the company to reexamine this situation was the migration to Windows 7. In Windows XP, a custom tool that allowed users one of three levels of administrative rights to their workstations would need to be replaced during the Windows …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds