Writing the Culture Info Browser Application

The Culture Info Browser application uses the methods and properties in the CultureInfo class to give you specific information about the formatting of numbers, dates, and strings for each culture on your computer.

To get started, create a new Windows Forms application named Globalization_vb or Globalization_cs, depending on the language you're using.

When the default form1 comes up in the designer, follow these steps to complete the user interface:

1.
Change the Text property of the form to Culture Info Browser.

2.
Change the Font property of the form to 12pt Tahoma.

3.
Drag a ListBox control to the form. Change the Name property to lb, and set the Dock property to Left.

4.
Drag a Splitter control to the form. It should automatically dock against the ListBox.

5.
Drag a ListView control to the form. Change the Name property to lv, and set the Dock property to Fill.

The user interface is now complete. You have a form that resizes its child controls correctly, and uses a splitter bar to enable resizing between the controls on the form. Your completed form should look like Figure 15.4.

Figure 15.4. The Culture Info Browser application at design time.


The next step is to add some code. Double-click the form to get the Form_Load event. Before you add any code, you must add the System.Globalization namespace to this class. So, scroll to the top of the form1 class file, and add the Imports or using statement for the System.Globalization namespace. Listing 15.1 is what the top of your class file should look like.

Listing 15.1. Aliasing the System.Globalization Namespace
Imports System.Globalization

Public Class Form1
   Inherits System.Windows.Forms.Form


using System.Globalization;

namespace Globalization_cs
{

Next, you fill the ListBox with all the available cultures on your machine. The CultureInfo class has a CultureType enumeration, which returns an array to the CultureInfo type that contains information about each culture you specify in the filter. The filter can be any CultureType listed in Table 15.4.

Table 15.4. CultureType Members
Member NameDescription
AllCulturesRefers to all cultures.
InstalledWin32CulturesRefers to all cultures that are installed in the system.
NeutralCulturesRefers to cultures that are associated with a language but aren't specific to a country/region. The names of these cultures consist of the lowercase two-letter code derived from ISO 639-1. For example: "en" (English) is a neutral culture. The invariant culture is included in the array of cultures returned by CultureInfo.GetCultures with this value.
SpecificCulturesRefers to cultures that are specific to a country/region. The names of these cultures follow the RFC 1766 standard in the format "<languagecode2>-<country/regioncode2>", where <languagecode2> is a lowercase two-letter code derived from ISO 639-1 and <country/regioncode2> is an uppercase two-letter code derived from ISO 3166. For example, "en-US" (English - United States) is a specific culture.

To test this, add the code in Listing 15.2 to the Form_Load event of your form.

Listing 15.2. Getting the CultureInfo in the Form_Load Event
Private Sub Form1_Load(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles MyBase.Load

   ' Create a CultureInfo Variable
   Dim cu As CultureInfo

   ' Loop through the CultureTypes Enumeration
   ' and list the Culture Name in the Listbox
   For Each cu In CultureInfo.GetCultures _
     (CultureTypes.InstalledWin32Cultures)
      lb1.Items.Add(cu.Name)
   Next
End Sub


private void Form1_Load(object sender, System.EventArgs e)
{
   //   Create a CultureInfo Variable
   //   Loop through the CultureTypes Enumeration
   //   and list the Culture Name in the Listbox
   foreach (CultureInfo cu in
      CultureInfo.GetCultures
      (CultureTypes.InstalledWin32Cultures))
   {
      lb.Items.Add(cu.Name);
   }
}

The code in Listing 15.2 loops though the enumeration to retrieve the names in the InstalledWin32Cultures enumeration, and adds them to the list box.

To view the properties of the culture that you click on in the list box, add the code in Listing 15.3, which uses the properties of the CultureInfo class to retrieve the values for the selected region code.

Listing 15.3. Retrieving the CultureInfo Properties for the Selected Culture
Private Sub lb1_SelectedIndexChanged(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles lb1.SelectedIndexChanged

   ' Get the Culture listed in the Listbox control
   Dim cu As String = CType(lb1.SelectedItem, String)

   ' Declare an instance of the CurrentCulture class,
   ' passing the selected listbox item
   Dim ci As New CultureInfo(cu)

   ' Set the cursor to an hourglass
   Me.Cursor = Cursors.WaitCursor

   ' Set the View property, and add 2 columns
   lv.View = View.Details
   lv.Columns.Add("Property", 300, HorizontalAlignment.Left)
   lv.Columns.Add("Value", 300, HorizontalAlignment.Left)

   ' Create a string array to hold the DayNames
   Dim Days() As String
   Days = ci.DateTimeFormat.DayNames

   ' Create a string array to hold the MonthNames
   Dim Months() As String
   Months = ci.DateTimeFormat.MonthNames

   ' Looping variable
    Dim intX As Integer

   With lv.Items
      ' Clear the ListView of any existing items
      .Clear()

      ' Add items to the list base on the
      ' ci CultureInfo class variable
      .Add("Name").SubItems.Add(ci.Name)
      .Add("Display Name").SubItems.Add(ci.DisplayName)
      .Add("English Name").SubItems.Add(ci.EnglishName)
      .Add("LCID").SubItems.Add(ci.LCID)
      .Add("Long Date Format").SubItems.Add _
          (ci.DateTimeFormat.LongDatePattern.ToString)
      .Add("Native Name").SubItems.Add(ci.NativeName)
      .Add("Text Info").SubItems.Add(ci.TextInfo.ToString)
      .Add("3 Letter ISO Lang Name").SubItems.Add _
          (ci.ThreeLetterISOLanguageName)
      .Add("3 Letter Windows Name").SubItems.Add _
          (ci.ThreeLetterWindowsLanguageName)
      .Add("2 Letter ISO Name").SubItems.Add(ci.TwoLetterISOLanguageName)

      ' Add the Days of the week
      For intX = 0 To Days.Length - 1
         .Add("Day " & intX + 1).SubItems.Add(Days(intX))
      Next

      ' Add the Months of the year
      For intX = 0 To Months.Length - 1
         .Add("Month " & intX + 1).SubItems.Add(Months(intX))
      Next
   End With

   ' Set the mouse cursor back to normal
   Me.Cursor = Cursors.Default

End Sub


private void lb_SelectedIndexChanged(object sender, System.EventArgs e)
{
   // Get the Culture listed in the Listbox control

   string cu = lb.Text;

   // Declare an instance of the CurrentCulture class,
   // passing the selected listbox item
   CultureInfo ci = new CultureInfo(cu);

   // Set the cursor to an hourglass
   this.Cursor = Cursors.WaitCursor;

   // Set the View property, and add 2 columns
   lv.View = View.Details;
   lv.Columns.Add("Property", 300, HorizontalAlignment.Left);
   lv.Columns.Add("Value", 300, HorizontalAlignment.Left);

   // Create a string array to hold the DayNames
   string[] days;
   days = ci.DateTimeFormat.DayNames;

   // Create a string array to hold the MonthNames
   string[] months;
   months = ci.DateTimeFormat.MonthNames;

   // Clear the ListView of any existing items
   lv.Items.Clear();

   // Add items to the list base on the
   // ci CultureInfo class variable
   lv.Items.Add("Name").SubItems.Add(ci.Name);
   lv.Items.Add("Display Name").SubItems.Add(ci.DisplayName);
   lv.Items.Add("English Name").SubItems.Add(ci.EnglishName);
   lv.Items.Add("LCID").SubItems.Add(ci.LCID.ToString());
   lv.Items.Add("Long Date Format").SubItems.Add
        (ci.DateTimeFormat.LongDatePattern.ToString());
   lv.Items.Add("Native Name").SubItems.Add(ci.NativeName);
   lv.Items.Add("Text Info").SubItems.Add(ci.TextInfo.ToString());
   lv.Items.Add("3 Letter ISO Lang Name").SubItems.Add
        (ci.ThreeLetterISOLanguageName);
   lv.Items.Add("3 Letter Windows Name").SubItems.Add
        (ci.ThreeLetterWindowsLanguageName);
   lv.Items.Add("2 Letter ISO Name").SubItems.Add
        (ci.TwoLetterISOLanguageName);

   // Add the Days of the week
   for(int x = 0; x < days.Length -1; x++)
   {
      lv.Items.Add("Day " + Convert.ToString(x+1)).SubItems.Add(days[x]);
   }

   // Add the Months of the year
   for(int x = 0; x < months.Length -1; x++)
   {
      lv.Items.Add("Month " +
          Convert.ToString(x+1)).SubItems.Add(months[x]);
   }

   // Set the mouse cursor back to normal
   this.Cursor = Cursors.Default;
}
					

Press the F5 key to run the application. Click around the list box in the left pane of the form, and you'll notice the different culture-specific formatting available.

Figure 15.5 demonstrates the CultureInfo properties for the Malay region.

Figure 15.5. Running the Culture Info Browser application.


After you create the instance of the CultureInfo class, the ci variable has full access to the properties and methods of the CultureInfo class. Notice the arrays you returned: a days array and a months array, which contain the correct day and month spelling for the selected culture, respectively. You saw earlier how Outlook handles a different locale setting, so now you understand how it knows where to get the information.

There are many more properties that you didn't display. As you saw when you were typing the code, the auto-complete feature in Visual Studio .NET comes in very handy again when learning about a new class. Figure 15.6 shows the auto-list members in action for the ci variable, which is an instance of the CultureInfo class.

Figure 15.6. Visual Studio .NET auto-list members in action with the CultureInfo class.


I joke about guessing when you aren't sure about what properties and methods to use, but guessing is a sure way to learn what a class offers. Go ahead and try some of the other properties that you didn't list in the ListView control. It's an excellent way to learn.

I mentioned the Outlook Calendar again, and it seems to make sense that when you change a locale in the Control Panel, the entire operating system should reflect those changes. When you write code in .NET, the .NET Framework looks at the CurrentCulture of the current thread that's running your application to determine what locale information to use.

So, by changing the CurrentCulture of the CurrentThread, you can actually see the differences in what your code would look like running in a different locale. This is why writing language- and locale-neutral code is so important. Consider the following Visual Basic .NET code:

Dim i As Integer = 100

Console.WriteLine(i.ToString("c", _
 System.Threading.Thread.CurrentThread.CurrentUICulture.NumberFormat))

Console.WriteLine(i.ToString(Date.Now, _
 System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat))

Console.WriteLine(FormatCurrency(i))

When I run this on my normal English (United States) locale, the numbers are displayed as I expect. When I run it after changing the locale to Tatar (Russia), the numbers also look as I'd expect. There are different formats for the numbers and dates, as the following output shows:

100,00 ?.
10011,02002 13:14:18
100,00 ?.

When dealing with applications that use the CultureInfo class, the data is based on the CurrentCulture of the running thread. So, you can modify the running thread's current culture to actually see how your data would look running on another locale.

To make this work, you must modify the CurrentUICulture property of the CurrentThread. On a Windows Form, this can be done before the InitializeComponent method call by using the code listed in Listing 15.4.

Listing 15.4. Changing the CurrentUICulture in the Class Constructor
System.Threading.Thread.CurrentThread.CurrentUICulture = _
  New System.Globalization.CultureInfo("tt-RU")


System.Threading.Thread.CurrentThread.CurrentUICulture =
 new System.Globalization.CultureInfo("tt-RU");

By modifying the running thread's CurrentUICulture to ìtt-RUî, the form correctly formats the data based on the ìtt-RUî localization properties.

That's why the Culture Info Browser is so important. It's one thing to see how things look in a nice demo, but you can actually implement the correct culture formatting by altering the CurrentUICulture on any running thread in any .NET application.

Note

Because the System.ReadMyMind namespace isn't in the .NET FCL yet, you're normally going to write applications that enable the end user to set a preference on what locale she wants to use. One of my customers has offices in Monaco, Greece, Australia, and the United States, so there are four different locales I need to worry about. But even though a person using my application might be in Monaco, it doesn't mean he necessarily wants the European date and currency formats. So, I need to let the users choose what they want. By letting them do so, I can guarantee that each screen they look at is user-friendly to their preference because I can modify the culture information for all the formatting in the application.


The next step in understanding globalization is the use of Visual Studio .NET's built-in tools that handle localization resources.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.189.251