Localizing ASP .Net Pages

If you plan on distributing your application to an international audience, there are a number of things you’ll need to keep in mind during the design and development phases. Even if you don’t have such plans, a small effort up front can make things considerably easier should your plans change in future versions of your application. Services built into the .NET Framework make it easy to develop a single application that can adapt to different locales using managed development with Visual Basic or Visual C#.

We will now discuss a method that can help developers add localization code to their web forms without much effort .When dealing with the issue of localizing web pages , we often face the problem of writing too much code to get the right labels and captions for the user interface elements displayed .In this demonstration we will use classes from the following name spaces

using System.Threading ;
using System.Resources ; 
using System.Globalization ;

so make sure to include them in your page

1)The first step is to create a resource file that will hold our strings we do so by adding a resource assembly using the project menu

2)Add a label and a button to the web form , name the button mybtn ,name the label mylabel

3)in each entry of the resource file type the ID of the control you which to be international ,so we will have 2 entries mybtn and mylabel , then write the caption for it in English save this file as myapp.en-US.resx add another resource file and add the same entries , but add the captions for them in French save this file as myapp.fr-FR.resx To use our resource file in .NET application , we must convert from a .resx file to a .resources file . This is done using the Resgen.exe utility it is used as follows

resgen myapp.en-EN.resx myapp.en-EN.resources
resgen myapp.fr-FR.resx myapp.fr-FR.resources

After doing that we have now 2 .resources file. To use this file from our application use the following code :

CultureInfo("en-EN"); ResourceManager ;
rm =ResourceManager.CreateFileBasedResourceManager("myapp",Server.MapPath("."),null);

In the first line we set the current cultureUIculture of the web page to French. In the second line we passed “myapp” to the first argument cause we named our files with a base “myapp” , the second argument should be the path where .net will look for the resource files ,the third argument could be passed as null in this case .

Now do display the correct the captions of the user interface elements we use the following code

mybtn.Text= rm.GetString("mybtn") ;

mylabel.Text= rm.GetString("mylabel") ;

This will be enough to display the right strings on the controls. If you modify the CultureInfo(“en-EN”) to CultureInfo(“fr-FR”) and recompile you will get the French text on your controls . do not forget to call rm.ReleaseAllResources() to release all resources used by our resource manager.

Now the problem if we have 10s of controls on our web page , are we going to repeat the code for localizing for each control ? Fortunately there is a solution . Since we named each entry in the resource file with the ID of the control , we can loop on them with code like this

foreach(Control c in this.controls)
  c.Text = rm.getString(c.ID) ;

This code has 2 problems . First the page doesn’t have to be the direct parent of the control , it could be containing a panel having child controls , or a repeater hosting panels hosting child controls so we must modify our function to do a recursive call on each control and its child controls .The second problem is that we don’t guarantee that all controls have a text property also , and we don’t want to alter the textbox entry fields. So we modify the code like the following function.

private void Localize(Control parent, ResourceManager myres )
if( parent.GetType().Name=="Label" )
	((Label)parent).Text =myres.GetString(parent.ID);
else if ( parent.GetType().Name=="Button" )
	((Button)parent).Text =myres.GetString(parent.ID);

	foreach( Control child in parent.Controls ) Localize(child ,myres ) ;
else return ;

Note that at first we check if the control is a button or a label , and retrieve the corresponding string according to its ID . We can add more tests to affect more control types , we just used the label and button here for simplification .After that we check if the control has child controls , if it does we repeat the process on each of its child controls .After we have finished the implementation of this function , we may call it in our page like that :

Localize( this ,rm ) ; passing to it the web page (which is also a control ) , and a resource manager that we have already opened in the second argument .This will localize all controls with just a single line , avoiding hours if you have a 100 controls on your web page . You could even declare this function in a class and call it among your web pages ,instead of including it in each page .