Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleLocalize control resources at run time in VB .NET
DescriptionThis example shows how to localize control resources at run time in VB .NET.
Keywordslocale, internationalization, globalization, CultureInfo, runtime
CategoriesVB.NET, Controls
 
First, build the form as you normally would. Then set the form's Localizable property to True and set its Language property to another language that you want to support. Change the controls' properties for the new language. For example, translate the text into the new language.

When you do this, Visual Basic stores information for the different languages in different resource files associated with the form. If you click the Solution Explorer's Show All Files button, you can see these files below the form.

Normally Visual Basic examines the computer's regional settings to decide which resources to use but you can change this for testing purposes at run time.

Subroutine ApplyLocale applies a new locale. It creates a CultureInfo object to represent the locale and a ComponentResourceManager to load resources for the locale. It sets the thread's culture properties to the locale.

The code uses the ComponentResourceManager to load the form's properties. It then loops through the form's controls and calls subroutine ApplyLocaleToControl for each.

That takes care of basic properties but there are some things it doesn't handle so the program must handle them itself. The program makes a ResourceManager to load resources from the correct vesion of the form's resource file.

Tooltips are provided by a ToolTip extender provider so they are not true properties and are not loaded by the ComponentResourceManager. The program shows how to set a tooltip, using the ResourceManager to get the appropriate text.

ComboBoxes and ListBoxes contain lists of items. Because they are items, the ComponentResourceManager cannot know how to load their values so the program does it. It uses the ResourceManager to fetch the appropriate values from the resource files. Note that the strings you put in these controls at design time are named Items, Items1, Items2, etc.

Similarly the program loads a ToolStripComboBox.

 
' Apply a locale to the form and its controls.
Private Sub ApplyLocale(ByVal locale_name As String)
    ' Make a CultureInfo and ComponentResourceManager.
    Dim culture_info As New CultureInfo(locale_name)
    Dim component_resource_manager As New _
        ComponentResourceManager(Me.GetType)

    ' Make the thread use this locale. This doesn't change
    ' existing controls but will apply to those loaded later
    ' and to messages we get for Help About (see below).
    Thread.CurrentThread.CurrentUICulture = culture_info
    Thread.CurrentThread.CurrentCulture = culture_info

    ' Apply the locale to the form itself.
    ' Debug.WriteLine("$this")
    component_resource_manager.ApplyResources(Me, "$this", _
        culture_info)

    ' Apply the locale to the form's controls.
    For Each ctl As Control In Me.Controls
        ApplyLocaleToControl(ctl, _
            component_resource_manager, culture_info)
    Next ctl

    ' Perform manual localizations.
    ' These resources are stored in the Form1 resource
    ' files.
    Dim resource_manager As New _
        ResourceManager("Localized.Form1", _
        Me.GetType.Assembly)

    ' Tooltips.
    ToolTip1.SetToolTip(picFlag, _
        resource_manager.GetString("picFlag.ToolTip"))

    ' ComboBox items.
    ComboBox1.Items.Clear()
    ComboBox1.Items.Add(resource_manager.GetString("ComboBox1.Items"))
    ComboBox1.Items.Add(resource_manager.GetString("ComboBox1.Items1"))
    ComboBox1.Items.Add(resource_manager.GetString("ComboBox1.Items2"))

    ' ListBox items.
    ListBox1.Items.Clear()
    ListBox1.Items.Add(resource_manager.GetString("ListBox1.Items"))
    ListBox1.Items.Add(resource_manager.GetString("ListBox1.Items1"))
    ListBox1.Items.Add(resource_manager.GetString("ListBox1.Items2"))

    ' ToolStripComboBox items.
    ToolStripComboBox1.Items.Clear()
    ToolStripComboBox1.Items.Add(resource_manager.GetString("ToolStripComboBox1.Items"))
    ToolStripComboBox1.Items.Add(resource_manager.GetString("ToolStripComboBox1.Items1"))
    ToolStripComboBox1.Items.Add(resource_manager.GetString("ToolStripComboBox1.Items2"))
End Sub
 
Subroutine ApplyLocaleToControl applies the resources for a control.

If the control is a MenuStrip, the code loops through the control's Items collection and calls ApplyLocaleToToolStripItem for each of the menu strip's items.

If the control is not a MenuStrip, the code loops through the control's Controls collection and calls ApplyLocaleToControl for each of the controls this control contains.

 
' Apply the locale to a control.
Private Sub ApplyLocaleToControl(ByVal ctl As Control, _
    ByVal component_resource_manager As _
    ComponentResourceManager, ByVal culture_info As _
    CultureInfo)
    ' Debug.WriteLine(ctl.Name)
    component_resource_manager.ApplyResources(ctl, _
        ctl.Name, culture_info)

    ' See what kind of control this is.
    If TypeOf ctl Is MenuStrip Then
        ' Apply the new locale to the MenuStrip's items.
        Dim menu_strip As MenuStrip = DirectCast(ctl, _
            MenuStrip)
        For Each child As ToolStripMenuItem In _
            menu_strip.Items
            ApplyLocaleToToolStripItem(child, _
                component_resource_manager, culture_info)
        Next child
    Else
        ' Apply the new locale to the control's children.
        For Each child As Control In ctl.Controls
            ApplyLocaleToControl(child, _
                component_resource_manager, culture_info)
        Next child
    End If
End Sub
 
Subroutine ApplyLocaleToControl applies the resources for a control.

Subroutine ApplyLocaleToToolStripItem applies the resources for a ToolStripItem. If the item is a ToolStripMenuItem, then it may contain submenus. The routine loops through item's DropDownItems collection and calls itself for any dropdown menus.

 
' Recursively apply the locale to a ToolStripItem.
Private Sub ApplyLocaleToToolStripItem(ByVal item As _
    ToolStripItem, ByVal component_resource_manager As _
    ComponentResourceManager, ByVal culture_info As _
    CultureInfo)
    ' Debug.WriteLine(menu_item.Name)
    component_resource_manager.ApplyResources(item, _
        item.Name, culture_info)

    ' Apply the new locale to items contained in it.
    If TypeOf item Is ToolStripMenuItem Then
        Dim menu_item As ToolStripMenuItem = _
            DirectCast(item, ToolStripMenuItem)
        For Each child As ToolStripItem In _
            menu_item.DropDownItems
            ApplyLocaleToToolStripItem(child, _
                component_resource_manager, culture_info)
        Next child
    End If
End Sub
 
The following code shows how the program displays a locale-aware message. This example stores strings in resource files names RuntimeStrings.resx and RuntimeStrings.de-DE.resx so Visual Basic makes a My.Resources.RuntimeStrings namespace for them. The program displays the string named Help_About. The resource manager automatically gets the correct version.
 
    ' Display Help About.
    Private Sub AboutToolStripMenuItem_Click(ByVal sender _
        As System.Object, ByVal e As System.EventArgs) _
        Handles mnuHelpAbout.Click
#If True Then
        ' The easy way
        MessageBox.Show(My.Resources.RuntimeStrings.Help_About)
#Else
        ' This vesion explicitly uses a ResourceManager.
        ' The base name is <ProjectName>.<FileName>.
        Dim resource_manager As New _
            ResourceManager("Localized.RuntimeStrings", _
            Me.GetType.Assembly)

        ' Display the message.
        MessageBox.Show(resource_manager.GetString("Help_About"))
#End If
    End Sub
 
This example was written in Visual Basic 2005 but I think it should work in earlier vesions of Visual Basic .NET.

See also:

 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated