Localizability in .NET 1.1


Making an ASP.NET application localizable in the .NET Framework 1.1 is all your own work. Visual Studio 2003 offers no facilities to help you with this process, and the .NET Framework 1.1 offers no facilities beyond what we have seen so far. This means that your ASP.NET application handles this problem "manually" at every stage using the basic functionality that we saw in Chapter 3, "An Introduction to Internationalization." In this section, we localize an ASP.NET application. In subsequent sections, we add processing to handle the user's language preference.

To show how to localize an ASP.NET 1.1 application, we work through an example. Create a new ASP.NET Web Application and add a table, two labels (User Name and Password), two TextBoxes, and a Button (Login) so that the resulting page looks like Figure 5.1.

Figure 5.1. Example ASP.NET 1.1 Web Form before Localization


Add a new resource file to hold the page's resources. (In Solution Explorer, right-click the project; select Add, Add New Item...; select Assembly Resource File.) Call the resource file WebForm1Resources.resx. For each property that should be localized, add an entry to the resource file. Figure 5.2 shows the Resource Editor with the Text properties for the labels and button.

Figure 5.2. Resource Editor with Entries for the Web Form's Controls


Add a private field to the page to hold a ResourceManager:

 private ResourceManager resourceManager; 


Add the following assignment to the page's Load event:

 resourceManager = new ResourceManager(     "WebApplication1.WebForm1Resources",     System.Reflection.Assembly.GetExecutingAssembly()); 


If your application is not called WebApplication1, you should change the name-space used in the ResourceManager constructor's first parameter. Now we need to load each of the resources. Add the following code to the page's Load event after the previous line:

 Label1.Text  = resourceManager.GetString("Label1.Text"); Label2.Text  = resourceManager.GetString("Label2.Text"); Button1.Text = resourceManager.GetString("Button1.Text"); 


Note that for this solution to be effective, all controls that have localizable content must have an ID and have runat="server".


At this stage, the application is localizable, but it has not yet been localized. Copy the WebForm1Resources.resx file to WebForm1Resources.fr.resx and translate each of the items into French. The application has now been localized, but so far users are unable to specify that they want to see the French version. The application's culture will be determined by the default values for CurrentCulture and Current UICulture. In other words, they will be determined by the language version of Windows on which the application is running and the culture set in the Regional and Language Options dialog for the user account on which ASP.NET is running. These settings are likely to be of use to your user only by coincidence.

Automating Resource Assignment

The previous example shows the mechanism of retrieving resources and applying them to their respective properties of their respective controls. It is easy to understand and is useful for demonstrating how the process works. However, as far as production code goes, it is lacking. The problem is that it is fragile. It requires the developer to remember to add an assignment for every property that should be localizable. On a form with many controls, the maintenance of this block of code is time-consuming and error prone. A better solution is a solution that does not require the code to be modified. This section explains this solution.

Remove the three property assignments and replace them with the following single call:

 ApplyResources(resourceManager, this); 


ApplyResources is a protected static method that you would add to a base page class. It is included in the downloadable source code for this book and is shown shortly. The essential strategy is that it iterates through all of the resources for the CurrentUICulture looking for controls that have the same name as the controls in the resources, and properties of those controls that have the same name as the properties in the resources, and assigns the resource value to those properties. ApplyResources is implemented like this:

 protected static void ApplyResources(     ResourceManager resourceManager, Page page) {     HtmlForm htmlForm = GetHtmlForm(page);     if (htmlForm != null)     {         ResourceSet resourceSet = resourceManager.GetResourceSet(             CultureInfo.CurrentUICulture, true, true);         IDictionaryEnumerator enumerator =             resourceSet.GetEnumerator();         while (enumerator.MoveNext())         {             ApplyResources(htmlForm,                 enumerator.Key.ToString(), enumerator.Value);         }     } } 


ApplyResources gets the HtmlForm from the page using the following method:

 private static HtmlForm GetHtmlForm(Page page) {     foreach(Control control in page.Controls)     {         if (control is HtmlForm)             return (HtmlForm) control;     }     return null; } 


ApplyResources gets the ResourceSet (the set of resource entries) for the CurrentUICulture. It iterates through each of the entries, calling an overloaded ApplyResources method to apply the resource entry to the form. The overloaded ApplyResources is this:

 protected static void ApplyResources(     HtmlForm htmlForm, string key, object value) {     int periodIndex = key.IndexOf(".");     if (periodIndex > -1)     {         string controlID = key.Substring(0, periodIndex);         Control control = GetControl(htmlForm, controlID);         if (control != null)         {             string propertyName = key.Substring(periodIndex + 1);             PropertyInfo propertyInfo =                 control.GetType().GetProperty(propertyName);             if (propertyInfo != null)                 propertyInfo.SetValue(                     control, value, new object[] {});         }     } } 


The overloaded ApplyResources method splits the key (e.g., Label1.Text) into its control ID (i.e., Label1) and its property name (i.e., Text). It calls GetControl to get a control that corresponds to the control ID:

 protected static Control GetControl(     HtmlForm htmlForm, string controlID) {     foreach(Control control in htmlForm.Controls)     {         if (String.Compare(control.ID, controlID, true,             CultureInfo.InvariantCulture) == 0)             return control;     }     return null; } 


The overloaded ApplyResources method uses reflection to get a PropertyInfo for the named property and calls its SetValue method to assign the value from the resource to the control's property.




.NET Internationalization(c) The Developer's Guide to Building Global Windows and Web Applications
.NET Internationalization: The Developers Guide to Building Global Windows and Web Applications
ISBN: 0321341384
EAN: 2147483647
Year: 2006
Pages: 213

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