User Interface Localization

User Interface Localization

The System.Globalization namespace provides most of the .NET support for localization in your applications. Here are the two key features in development:

  • Cultures An identifier for a specific locale

  • Resource files A storage location for culture-dependent resources such as strings and bitmaps

graphics/note_icon.gif

A culture does not simply define the language to be used, because many locales may share the same language and yet have very different localization requirements. One example would be the United Kingdom and the United States, both of which use English as the primary language but also use very different formats for currency and dates.


Cultures

Cultures are designated by a full culture code made up of a neutral culture code, with one or more possible subculture codes, as in the following examples:

  • de The German culture. This is a neutral culture alone, without a specified subculture code. Neutral cultures generally do not provide sufficient information to localize an application.

  • en-GB The English (United Kingdom) culture. This is a specific culture, including enough information to localize an applicationin this case, for English speakers in Great Britain.

  • az-AZ-Cyrl A specific culture with two subculture codes (in this case, the Azeri language in Azerbaijan, written using the Cyrillic alphabet).

One more culture that you should know about is the invariant culture . This is a special culture that doesn't have an abbreviation. The invariant culture has two purposes:

  • Interacting with other software, such as system services, in which no user is directly involved

  • Storing data in a culture-independent format that won't be displayed directly to end users

There are two ways to create a CultureInfo object that represents the invariant culture:

 Dim ciInv As CultureInfo = New CultureInfo("") 

and

 Dim ciInv As CultureInfo = CultureInfo.InvariantCulture 
The CultureInfo Class

The System.Globalization.CultureInfo class provides various items of information about a particular culture and may be accessed by performing the following steps:

  1. Open Visual Studio .NET and create a new Visual Basic .NET Windows application with a form.

  2. Place a Button control named btnGetInfo , a TextBox control named txtCulture , and a ListBox control named lbInfo on the form.

  3. Add a reference to the System.Globalization namespace at the top of the form's code module:

     Imports System.Globalization 
  4. Enter code to handle the button's Click event:

     Private Sub btnGetInfo_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnGetInfo.Click     ' Create a CultureInfo object for the specified culture     Dim ci As CultureInfo = New CultureInfo(txtCulture.Text)     ' Dump information about the culture     With lbInfo.Items         .Clear()         .Add("Display Name: " & ci.DisplayName)         .Add("English Name: " & ci.EnglishName)         .Add("Native Name: " & ci.NativeName)         ' Get day names         .Add("Day Names:")         Dim strDayNames() As String = ci.DateTimeFormat.DayNames         Dim strDay As String         For Each strDay In strDayNames             .Add("  " & strDay)         Next         ' Get the current year         .Add("Current year: " & ci.Calendar.GetYear(DateTime.Today))         ' And the currency symbol         .Add("Currency symbol: " & ci.NumberFormat.CurrencySymbol)     End With End Sub 
  5. Set the form as the startup object for the project and then run the project. Enter the name of a culture (such as en-US or fi-FI) in the text box and click the Button control to display some of the information available to that culture (see Figure 8.1).

    Figure 8.1. The sample form displaying information on the fi-FI (Finnish) culture.

    graphics/08fig01.jpg

graphics/tip_icon.gif

If you need to get a list of all the available cultures, call the CultureInfo.GetCultures method. This method returns an array of all supported CultureInfo objects.


Culture Threading

The .NET framework allows for localization on a thread-by-thread basis. In a normal Windows application, only a single thread of execution is usedrepresented by the Thread.CurrentThread object within the System.Threading namespace.

Each thread has two properties that are used for determining the culture to use:

  • CurrentUICulture Tells the Common Language Runtime which culture to use when selecting resources for the user interface (resource files are covered in the next section of this chapter).

  • CurrentCulture Dictates the format used for dates, times, currency, numbers , and string comparison and casing rules.

graphics/alert_icon.gif

Although localization settings can change currency display settings, the code does not perform any type of currency conversion process. This only affects the manner in which the data is displayed in the user interface.


The Invariant Culture

A special culture may be used in order to interact directly with system services or components that will not display data directly to the user interface. This culture-independent setting is managed by using the invariant culture , which may be instantiated in two ways:

  • Dimensioning a new CultureInfo object with a null input string:

     Dim ciInv As CultureInfo = New CultureInfo("") 
  • Dimensioning a new object using CultureInfo.InvariantCulture :

     Dim ciInv As CultureInfo = CultureInfo.InvariantCulture 
Displaying Localized Information

You may display localized information by performing the following steps:

  1. Open Visual Studio .NET and create a new Visual Basic .NET Windows application with a form.

  2. Place a Label control, a ComboBox control named cboSelectCulture , and four TextBox controls named txtCulture , txtDate , txtCurrency , and txtNumber on the form.

  3. Add the reference to the top of the form's code module:

     Imports System.Globalization Imports System.Threading 
  4. Enter code to handle events within the form's module:

     Private Sub SampleForm_Load(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles MyBase.Load     ' Stock the combo box     Dim ci As CultureInfo     For Each ci In CultureInfo.GetCultures( _      CultureTypes.SpecificCultures)         cboSelectCulture.Items.Add(ci.Name)     Next     ' Display the name of the default culture     txtCulture.Text = Thread.CurrentThread. _      CurrentCulture.EnglishName     ' Display some data     DisplayData() End Sub Private Sub cboSelectCulture_SelectedIndexChanged( _  ByVal sender As System.Object, ByVal e As System.EventArgs) _  Handles cboSelectCulture.SelectedIndexChanged     ' Create an appropriate CultureInfo object for the thread     Thread.CurrentThread.CurrentCulture = _      New CultureInfo(cboSelectCulture.Text)     ' Display the name of the culture     txtCulture.Text = Thread.CurrentThread.CurrentCulture.EnglishName     ' Refresh the display of the data     DisplayData() End Sub Private Sub DisplayData()     Dim dtNow As Date = DateTime.Now     Dim dblcurrency As Double = 13472.85     Dim dblnumber As Double = 1409872.3502     txtDate.Text = dtNow.ToLongDateString()     txtCurrency.Text = dblcurrency.ToString("c")     txtNumber.Text = dblnumber.ToString("n") End Sub 
  5. Set the form as the startup object for the project and then run the project. Select the name of a culture in the combo box to display localized information using that culture (see Figure 8.2). Note that no special code is required to format the data provided by the ToLongDateString and ToString methods .

    Figure 8.2. The sample form displaying localized information using the Uzbekistan (Latin) culture.

    graphics/08fig02.jpg

This example shows how to programmatically select a culture other than the current culture of the operating system, which is the default. Setting the Thread.CurrentThread.CurrentCulture property to a particular CultureInfo object will select that CultureInfo object's culture for the application.

Resource Files

Localizing the text displayed on the user interface is an important aspect of multicultural application development. As discussed in Chapter 4, "Components and .NET Assemblies," localized data for user interfaces may be configured as external resource files, satellite assemblies, or assembly resource files. Assembly resource files are often the easiest to work with because they are simply XML files that contain formatted text.

The following subsections discuss how to localize the user interface using assembly resource files and how to localize resources at runtime.

Localizing the User Interface Using Assembly Resource Files

You may create a form that provides a localized user interface by first creating the user interface form. Follow these steps:

  1. Open Visual Studio .NET and create a new Visual Basic .NET Windows application with a form, naming the form SampleForm1.vb .

  2. Place a Label control, three RadioButton controls, and a Button control on the form, as shown in Figure 8.3.

    Figure 8.3. A sample form displaying the placed controls.

    graphics/08fig03.jpg

  3. Set the Localizable property of the form to True and set the Language property to French (France).

  4. Change the text of each of the controls to the French translations and expand the button to accommodate the French translation, as shown in Figure 8.4.

    Figure 8.4. The sample form now displaying French (France) localized settings.

    graphics/08fig04.jpg

After you have created the user interface form, you can add the localization control form to your project by following these steps:

  1. Add a new form to your project, naming the form SampleForm2.vb .

  2. Place a ComboBox control named cboCulture and a Button control named btnOpenForm on the form.

  3. Add the references at the top of the form's code module:

     Imports System.Globalization Imports System.Threading 
  4. Add code to handle events within the form's module:

     Private Sub SampleForm2_Load(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles MyBase.Load     ' Put language choices in the combo box     cboCulture.Items.Add("English")     cboCulture.Items.Add("French") End Sub Private Sub btnOpenForm_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnOpenForm.Click     Dim f As New SampleForm1()     f.Show() End Sub Private Sub cboCulture_SelectedIndexChanged( _  ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles cboCulture.SelectedIndexChanged     ' When the user selects a language, change the UI culture     Select Case cboCulture.Text         Case "English"             Thread.CurrentThread.CurrentUICulture = _              New CultureInfo("en-US")         Case "French"             Thread.CurrentThread.CurrentUICulture = _              New CultureInfo("fr-FR")     End Select End Sub 
  5. Set the SampleForm2.vb form as the startup object for the project and then run the project. Select a culture and open the form using the button to display that culture's settings, as shown in Figure 8.5. Note that all settings, including the button-width change, are maintained .

    Figure 8.5. Examples of the same form created by selecting English or French culture localization for the form to be opened.

    graphics/08fig05.gif

graphics/note_icon.gif

The localized settings for each culture are stored in a resource file ( .resx ) and a satellite assembly ( .dll ), created automatically by the Visual Studio interface.


Localizing Resources at Runtime

Runtime user interface resources are loaded by an instance of the System.Resources.ResourceManager class. After you have initialized a ResourceManager object by calling one of the class's constructors, two methods may be used to retrieve localized settings:

  • Using the GetObject method to return an object from the resource file

  • Using the GetString method to return a string from the resource file

If you plan to move or resize controls at runtime, a satellite assembly configuration like the one in the previous example is more convenient . Using Visual Studio .NET to build satellite assemblies allows you to move and resize controls directly within the Windows Forms Designer. If you attempt this using assembly resource files, you would also have to store position and size data and then apply this data to your controls at runtime.

Assembly resource files are preferred when performing localization of information that is not directly displayed on the user interface, such as text that is used to populate a message box by the application. It is also possible to create hybrid solutions using both satellite assemblies and assembly resource files within the same project. For example, you might use Visual Studio .NET to build satellite assemblies to localize the controls on a form while using an assembly resource file to hold localized error messages.



Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 188

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net