Visual Basic 2010 : Localizing Applications - Windows Forms Localization

3/5/2012 11:34:35 AM
Limiting applications’ user interfaces to just one language means limiting your business. If you want to increase the possibilities of your applications being sold worldwide, you need to consider creating user interfaces that support multiple languages and culture specifications of your users. Of course, you can give users the option to select the desired language or provide localized interfaces for a particular country, but the main concept is that localization is a common requirement in modern applications. The .NET Framework helps developers in localizing applications with several kinds of resources.

Introducing .NET Localization

The .NET Framework provides the infrastructure for application localization via the System.Globalization namespace. The most important class in this namespace is the CultureInfo class that allows getting or setting information on the current application culture or on new custom settings. Generally this class works with the System.Threading.Thread.CurrentThread class that provides access to the thread representing your executable and that exposes the CurrentCulture and CurrentUICulture properties that you can assign with a CultureInfo object. The following code demonstrates how to get information on the current thread culture and how to set a new CultureInfo:

'Requires an Imports System.Globalization directive

'Gets the current culture and shows information
Dim culture As CultureInfo = System.Threading.Thread.

'Creates an instance of the CUltureInfo class
'based on the Italian culture and sets it as
'the current culture
Dim customCulture As New CultureInfo("it-IT")
        CurrentCulture = customCulture

The CultureInfo class provides lots of properties that enable applications to adhere to the required culture specifications. For example, DisplayName shows the name of the culture as it appears on the system, DateTimeFormat specifies the appropriate format for date and time in the specified culture, and NumberFormat provides specifications on how numbers and percentage need to be formatted in the specified culture.

Windows Forms Localization

If you are an experienced Windows Forms developer, maybe you already faced the localization problem with this technology. There are different ways for localizing a Windows Forms application, but basically all of them rely on managed resources. The easiest way for localizing a Windows Forms application is to take advantage of the Visual Studio Designer so that the IDE generates the appropriate resources files for you. An example is of course the best way for providing explanations; the goal of this an example is to localize a Windows Forms application in both English and Italian. Run Visual Studio 2010, create a new Windows Forms project with Visual Basic 2010, and name it WindowsFormsLocalization. Follow these steps:

Drag a Button from the toolbox onto the new form surface, and set its Text property as Localized button.

Drag a Label from the toolbox onto the new form surface, and set its Text property as Localized label.

Select the form, and in the Properties window set its Localizable property as True; then set its Language property as Italian.

Select the Button and set its Text property as Pulsante localizzato (in Italian).

Select the Label and set its Text property as Etichetta localizzata.

Build the project and enable the Show All Files view in Solution Explorer.

You notice that Visual Studio has generated a new it-IT subfolder under Bin\Debug (or Bin\Release) containing a satellite assembly where localized resources are stored. Moreover, Visual Studio generated a new localized resources file for the current form named storing the localized information for design time. If you try to run the application, you notice that it is still localized in English. This is because you need to explicitly assign in code the new localized culture. This can be accomplished by adding the following code (which requires an Imports System.Globalization directive) at the beginning of the application startup, which is typically the constructor, before the InitializeMethod is invoked:

Public Sub New()

    With Threading.Thread.CurrentThread
        .CurrentUICulture = New CultureInfo("it-IT")
        .CurrentCulture = New CultureInfo("it-IT")
    End With
    ' This call is required by the designer.

    ' Add any initialization after the InitializeComponent() call.

End Sub

The code simply assigns to the current thread the new culture information that will be retrieved from the related subfolder and satellite assembly. Figure 1 shows how the application looks with localized controls.

Figure 1. The localized Windows Forms application in action.

Windows Forms localization is straightforward because you can take advantage of the Visual Studio Designer. Unfortunately this is not the same in WPF applications, where a number of manual steps are required, as explained in the next section.

Video tutorials
- How To Install Windows 8

- How To Install Windows Server 2012

- How To Install Windows Server 2012 On VirtualBox

- How To Disable Windows 8 Metro UI

- How To Install Windows Store Apps From Windows 8 Classic Desktop

- How To Disable Windows Update in Windows 8

- How To Disable Windows 8 Metro UI

- How To Add Widgets To Windows 8 Lock Screen

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010
programming4us programming4us