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.
CurrentThread.CurrentCulture
Console.WriteLine(culture.DisplayName)
'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")
System.Threading.Thread.CurrentThread.
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:
1. | Drag a Button from the toolbox onto the new form surface, and set its Text property as Localized button.
|
2. | Drag a Label from the toolbox onto the new form surface, and set its Text property as Localized label.
|
3. | Select the form, and in the Properties window set its Localizable property as True; then set its Language property as Italian.
|
4. | Select the Button and set its Text property as Pulsante localizzato (in Italian).
|
5. | Select the Label and set its Text property as Etichetta localizzata.
|
6. | 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 Form1.it-IT.resx 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.
InitializeComponent()
' 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.
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.