Write For Us

Building Multilingual Web Sites with ASP.NET

Why globalisation and localisation?

Over the past few years, Millions of users across different countries have been added to the net surfers list. This creates a demand of multilingual websites which appeal more to native speakers of the language.


Before ASP.NET, developing multilingual applications in HTML was a daunting task. Programmers were required to develop multiple pages for supporting multiple languages. Though, PHP and ASP was introduced, there was not much relief for programmers in terms of saving their time. ASP.NET together with Visual Studio makes life easier for a programmer. Being enriched with namespaces such as System.Globalization and System.Resources.ResourceManager and in the presence of CultureInfo class, a programmer feels at ease for going beyond the scope of a particular language.

How Localization process works?


First, I would explain how localization process is carried in .NET. Resource files are XML (Extensible Markup Language) files. For different languages, you have to make different resource files. At compilation time, these files do get embedded in assemblies. The file which you have specified as default (which would represent the default language of your application) is embedded in the main assembly.

CultureInfo class and the role of resource manager


A good thing to know at this point is the use of CurrentCulture value of the current thread. The resource manager can be utilized to display items like errors or other messages in the user specified language. Just to elaborate more, see this code:

 AgeValidator.ErrorMessage = ResourceManagerObject.getString("RequiredAge");

Based on the CuurentCulture value, an appropriate error message in the user language would be displayed.

Let me give a little introduction of CultureInfo class. This class gives all necessary information about the specific culture. It can be

//A CultureInfo object for Egyptian Arabic is created 
CultureInfo culture_object = new CultureInfo ("ar-EG");

// assigns the Arabic object to the Current thread thus
// making Egyptian Arabic dominant over all other
// resources

System.Threading.Thread.CurrentThread.CurrentCulture = culture_object;

// this statement helps the Resource manager to make
// use of appropriate XML file (which includes
// the language content).

System.Threading.Thread.CurrentThread.CurrentUICulture = culture_object;

name, calendar and writing system of the culture. This class can be used like:

The scattered details discussed till now can be summarized by:

* All the language contents are placed into XML files and there is one file representing that language.

* By making use of CultureInfo class and CurrentCulture variable, the Resource manager localizes a page by producing run time version of the generalized page.

* This way, no matter what number of languages our application support, we would have to make only one web form and change the value of CurrentCulture at appropriate place

Making Resource files


If you have to support n languages, you have to develop n resource files. The naming convention is: The first part of the file name is base name. The second part specifies the culture. This is optional part. If you skip it, the resources in the resource file would be defined for default values.

Example: Your page name is webapp.aspx and you have to include three additional resource files. To include US English, Egyptian Arabic and Israel Hebrew, You have to name these resource files as webapp.aspx.en-US.resx, webapp.aspx.ar-EG.resx, webapp.aspx.he-IL.resx respectively. If you skip the second part of the name, you would be assigned resources on default.

For localizing a page, design a sample page whose controls are to be localized. By sample page, I mean a page whose controls are assigned different language specific values based on the CurrentCulture value of the Current thread, as we have shortly discussed. After designing the page, go to Tools and then select Generate Local Resource.

Generating Resource File

A resource file is created which would include the control values for all the controls, you have designed. This file can be seen in the App_LocalResources folder. Its structure is like a hash table. There is Name-Value pair in it. When you open this file, you would find all controls along with their values. An example can be:

 LabelResource1.Text = "Your Age"

To assign language specific values to the controls, copy all the generated resources and paste them giving the file a different name. The naming convention would be same as we discussed. For example, to include the same control in Arabic language, copy all the generated resources, assign the name webapp.aspx.ar.resx and translate values for the Controls. For example:

 LabelResource1.Text = "?? ?? ????"

Here are snapshots of two resource files. The first one contain localizable controls which have been translated in urdu language resource file.



Note that all controls which are generated by the Resource generator in Visual studio have to possess a [Localizable] property. In other words, to define a culture specific control, add [localizable] property to it. After generating the resource file, if you see the code for these controls, you would see an implicit localization expression along with code of every control. For example, the code for the Label Ac€~Age' before generating resource file was:

 <asp:Label ID= "Age " Text= "Your Age " runat= "server " />

And after generating the resource file, you would see something like:

 <asp: Label ID= "Age " Text= "Your Age " runat= "server " Meta: resourcekey= "LabelResource1 " />

The meta:resourceKey is Localization expression and its value (LabelResource1 in this case) points to the base resource. Similarly, to add other resource files for different languages, just copy all the items from the main resource file and translate the values in the respective resource files.

Localizing Validating Fields


Field Validators are often required to validate controls. It is better to use validators rather than authenticators in some controls. Regular expression Validator is a powerful control which lets you restrict the user to a specific pattern of input. Sometimes, localizing these Validators become an important requirement for your application to work properly. For example, the style of writing the date is different in English and Arabic scriptures. If you want to enquire an employee for writing the date at which he joined his firm, the employee belonging to US would write something like 02/06/2003 and an employee in Arab would like to write it as 2/6/2003 . Now, your job is to validate both these fields based on their culture. There are two approaches to localize Regular expression validators. The first one is Explicit Localization expression in which you write something like.

 <asp:RegularExpressionValidator ControlToValidate= "Starting_date " ID= "Date_validator " ErrorMessage= "Invalid Date " runat = "server " ValidationExpression='<%Resources: RegularExpressionValidatorResource4.Validation %>' meta:ResourceKey= "RegularExpressionValidator4 " />

Here you are writing the validation expression explicitly. In the above code, the error message is provided in the definition of RegularExpressionValidator but this text would be overcome by the text generated by the resource manager based on the CurrentCulture Value (Remember we discussed the CultureInfo class above). The other way to localize a regular expression Validator is to let the Resource manager automatically generate the validation expression based on the CurrentCulture Value



Another concern is, such validators can be required in more than one pages in your application. First, I would shortly go through the procedure for making a single resource file and sharing it across different pages of the same application. You would need to generate a resource file in App_GlobalResources folder. When you put your validation expression in this resource file, you would be saved from writing the same code again and again. To use it again, you have to change your validation expression. The code will be:

ValidationExpression='<%Resources: ReusableResources, DateFormat %>'
Where ReusableResources.resx is the resource file containing definition for the Ac€~DateFormat'.

Switching between RTL & LTR writing style


Similarly, writing the date, as we discussed shortly, requires right to left writing for the Arabic employee whereas US employees would require going from Left to Right. The RTL or LTR varies across languages. To handle this issue, you can make use of the same resource file in the App_GlobalResources folder. Since this thing can be required all across a web page, it would be better if you put it in html tag in your page. Don't forget to include the runat= "server " property. The complete syntax would be

 <html dir='<%Resources: ReusableResources,TextDirection%>' runat= "server " >

Where TextDirection is defined in ReusableResources.resx.



* Localization support is better in ASP.NET as compared to previous technologies.

* One Generalized web form is enough to accommodate as many languages provided you put resource files for all the languages. The CultureInfo class object has to be changed appropriately.

* The resource files should be named using standard conventions and the values of controls should be translated after copying them from main resource file.

* The validating fields can be localized by explicit localization expressions which require the programmer to translate the stuff and put it at the place where the expression points to.

* The expressions can be made reusable by defining a resource file and putting it in app_GlobalResources folder.

* Writing style can be changed by putting it in html tag.

This is it for now. Try using different combinations and you would experience something powerful.

Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |