Arabization: Localization/Globalization in ASP.Net 2.0

There are many tutorials which explain you about localization / globalization concept. In this article I just tryed to explain every single point from the developer/programmer point of view.

Let me brief you about Resource files.

  1. "aspx.resx" (resource) files: Resource files are xml files, which stores string, file paths (image), which are required to translate in to other languages.
  2. "App_GlobalResources": You can keep files which need to be accessed throughout the application. So in our application I have kept "TextDirect" & "EmailFormat" which required to access in maximum pages. In short common strings.
  3. "App_LocalResources": Local resources are associated with the single web pages, In this folder I have kept lables and strings which are specific to particualt aspx page it self.

Note: Since I am showing this in english and arabic. So every aspx page will have two different resource files.

For Example:

I have "Registration.aspx" page, for this page I had made files as follows:

  1. Registration.aspx.ar.resx (for arabic content to be stored).
  2. Registration.aspx.resx (for english content to be stored).

The above files are local, now letus consider files which can be accessed globally as.

  1. MulResource.resx
  2. MulResource.ar.resx

These files are used for storing "EmailFormat" & "TextDirection" which is common for most of the pages.

To create a resource file manually:

Make sure that your Web site has a folder in which to store the resource file by doing one of the following:

If you are creating a global resource file, you must have a folder named App_GlobalResources. To create the folder, in Solution Explorer, right-click the name of your Web site, click Add Folder, and then click App_GlobalResources Folder. There can only be one of these folders in an application, and it must be located at the root of the application.

If you are creating a local resource file, you must have a folder named App_LocalResources. To create the folder, in Solution Explorer, right-click the name of your Web site, click Add Folder, and then click App_LocalResources Folder. There can be many of these folders in an application, and they can be located at any level in the application.

To create a resource file, right-click the App_GlobalResources or App_LocalResources folder, and then click Add New Item.

Global resource files must be in the App_GlobalResources folder. If you try to create a .resx file outside of this folder, Visual Web Developer prompts you to create it in the folder.

Glbalization:

It is process by which you can develope application / program so that it can be used by the various regions/cultures. Let us consider that you have made a product called as "Shopping Cart" and you want to sell it to different regions, since it is online say arabic people should able to read the info in Arabic & English people should read it in English. To achive this we have to use something called as localization

Localization:

It is process of using specific regional/cultural info so that your program uses local language.

Culture:

Every region has different language and language is depend on different geographical location.

For example: "ar-EG" ar is the code for arabic language & EG means this language is spoken in Egypt country/region so "ar-EG" sets for Arabic-Egyption. Similarly "en-US" stands for English-United State.

Note: In IE the user can change the culture by going to Internet Options->General->Languages. For this to work, we need to set both the Culture and the UICulture to auto and enableClientBasedCulture = true

Setting Page Culture:

You can set the page culture by two ways:

  1. <globalization enableClientBasedCulture="true" culture="ar-EG" uiCulture="ar-EG"/>

  2. Setting culture by programmatically.

protected override void InitializeCulture()
{
    //string culture = Request.Form["ddSelLanguage"];
    string culture = Session["Language"].ToString();
    if (string.IsNullOrEmpty(culture))
        culture = "Auto";
    //Use this
    UICulture = culture;
    Culture = culture;
    //OR This
    if (culture != "Auto")
    {
        System.Globalization.CultureInfo MyCltr = new System.Globalization.CultureInfo(culture);
        System.Threading.Thread.CurrentThread.CurrentCulture = MyCltr;
        System.Threading.Thread.CurrentThread.CurrentUICulture = MyCltr;
    }
    base.InitializeCulture();
}
 

Code:

In "Default.aspx" file I have a drop-down by which user can select specific language, i have Arabic and English languages and by default "Auto" is set. I have used Auto-postback property of the drop-down & even you can make use of submit button for the same.

Once you got the selected language you can hold that value in the session as explained in the above code.

Session["Language"] = Request.Form["ddSelLanguage"];

While overriding the InitializeCulture() method you can assign this to the culture variable.

In this application i have tryed to explain with two different forms:

  1. User registration form.
  2. Send Email (In which I have also explained that how can we make use of built-in asp.net validation controls).

And on both the pages i have shown as we can switch on different pages by making use of <asp:HyperLink>.

Now let us have a look at "Registration.aspx" page code.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Registration.aspx.cs" Inherits="Registration" meta:resourcekey="RegResource" %>

This code is preety much similar with the code which get generated normally for any aspx page, only difference is that:

meta
:resourcekey="RegResource"

Here I want to explain in little depth, the concept of storing values like key-value pair and accessing values in the aspx page. Forexample if you refer to "Registration.aspx.resx" & "Registration.aspx.ar.resx" pages you will notice that
A resourceKey "RegResource.Title" has value "Registration" similarly in the other language. "RegResource.Title" has  value, which is directly accessed as shown above.

<
html id="Html1" runat="server" dir='<%$ Resources:MulResource, TextDirection %>' xmlns="http://www.w3.org/1999/xhtml">

In the above <html> tag it should have runat="server" attribute or it will give you error use literal runat="server" .

As i have already explained you about the global & local resources, in the above <html> we have used dir='<%$ Resources:MulResource, TextDirection %>'  attribute, now let us go little in dept for this, many of you must be aware of "DIR" attribute, it is nothing but the direction or the text in the page by default it is LTR (left to write) to have arabic type of appearance of the page we have to use it as "RTL". In short this act as common for all the pages either LTR or RTL so i have two global resource files as:

  1. MulResource.resx
  2. MulResource.ar.resx

In which you will find "TextDirection" ResourceKey and it's value associated with it, which is used in the above code.

I think so far so clear?

Now let us see how we can access the value of different labels in our project.

For example: On the registratio page i have label called as "lblFirstName" and it's values are stored in the corrosponding resource files viz.

  1. Registration.aspx.resx
  2. Registration.aspx.ar.resx

Here is code to make use of the same.

<asp:Label ID="lblFirstName" runat="server" meta:resourcekey="lblFirstName"></asp:Label>

Note: None of the labels have assigned any default value, label get values assigned at runtime depend on the selected language on the default page.