Accessing Cultural Information in Code


Open the code module for the frmEmployeeEdit form and go to the frmEmployeeEdit_Load method. Add the following declaration to this method:

 Dim locRes As New ResourceManager("NorthwindTraders", _ Me.GetType().Assembly) 

The ResourceManager class is responsible for accessing all culture resources in an application. It can access either satellite assemblies or the .resources file directly (if you choose to deploy these files, but that is not recommended). This line creates a new instance of the resource manager and tells it to look for resource files for the NorthwindTraders application. Next, you add the code in Listing 13-1 to the frmEmployeeEdit_Load method just before the Try..Catch block.

Listing 13-1: Loading the Resource Strings

start example
 Me.lblCourtesy.Text = locRes.GetString("COURTESY") Me.lblFirstName.Text = locRes.GetString("FIRST_NAME") Me.lblLastName.Text = locRes.GetString("LAST_NAME") Me.lblTitle.Text = locRes.GetString("TITLE") Me.lblReportsTo.Text = locRes.GetString("REPORTS_TO") Me.lblBirthDate.Text = locRes.GetString("BIRTH_DATE") Me.lblHireDate.Text = locRes.GetString("HIRE_DATE") Me.lblHomePhone.Text = locRes.GetString("HOME_PHONE") Me.lblExtension.Text = locRes.GetString("EXTENSION") Me.lblAddress.Text = locRes.GetString("ADDRESS") Me.lblCity.Text = locRes.GetString("CITY") Me.lblRegion.Text = locRes.GetString("REGION") Me.lblPostalCode.Text = locRes.GetString("POSTAL_CODE") Me.lblCountry.Text = locRes.GetString("COUNTRY") Me.lblPhoto.Text = locRes.GetString("PHOTO") Me.lblNotes.Text = locRes.GetString("NOTES") Me.lblAssigned.Text = locRes.GetString("SELECTED_TER") Me.lblAvailable.Text = locRes.GetString("AVAILABLE_TER") Me.btnOK.Text = locRes.GetString("OK") Me.btnCancel.Text = locRes.GetString("CANCEL") 
end example

Note

If you develop like I do, you will probably have those moments of laziness where you do not want to give a label a proper name. Even when I teach classes, I teach that you generally do not ever need to give a label a specific name—except in one circumstance. Whenever you are going to access a label from code, you must give it a proper name. Initially this adds some development time because you have to make the necessary adjustments even though it is a tedious process. If you have not given these labels proper names, now is the time to do so. I have used a fairly logical scheme to name these.

You can use two methods to access resources: GetString() and GetObject(). You use GetObject for images and audio, so you will be using the GetString method in this example.

At this point, if you run the application and open the Employee Edit form, you should see all of your labels in English, exactly as you would expect. But, you want to test what it will look like in Spanish and French. To do this, you set the CurrentUICulture property of the CurrentThread using the CultureInfo class. This sounds complicated but it is as simple as this next line, which you should add to the frmEmployeeEdit constructor:

 Thread.CurrentThread.CurrentUICulture = New CultureInfo("fr-FR") 

After adding this line of code, run the application again. You should get a screen that looks something like Figure 13-4.

click to expand
Figure 13-4: The Employee Edit form, localized in French

Pretty cool, huh? The only problem you will note is that some of the labels are not large enough to contain the text. Remember the rules: This is an easy fix at this point, but you would not want to have to go back and retrofit the entire application!




Building Client/Server Applications with VB. NET(c) An Example-Driven Approach
Building Client/Server Applications Under VB .NET: An Example-Driven Approach
ISBN: 1590590708
EAN: 2147483647
Year: 2005
Pages: 148
Authors: Jeff Levinson

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