Changing the Web-page direction on the fly

To create Arabic webpages, we don’t only need to translate the web-pages but we also need to change the page direction to right-to-left (rtl).

There are two ways to set the page direction on the fly, without hard coding the “dir” attribute. Below is a discussion of the two methods, including some code samples.

First method, you can depend on the CurrentUICulture , if it is RightToLeft culture, this means you need to set dir=”rtl”. Check the below code snipet:

void Page_Load(object sender, EventArgs e)

{

    if (CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft)

        html.Attributes["dir"] = "rtl";

}

However, this method could be confusing if the Arabic localized resources was not loaded.

The second method, is storing the page direction as a resource key using the resources.

You can then use it declaratively, using the resource localization method in VS 2005. This is the method that MS uses for localized web-pages. Instead of checking the CurrentUICulture, you can store an attribute in the resources, that tells you the direction. For example, in Global.resx add resource key “html_dir” … in the the Global.ar.resx , set it to “rtl” for other non-rtl cultures leave it as “ltr”. Then declaritively set “dir” attribute in your html directive. As shown below:

<html runat="server" dir="<%$ Resources: Global,html_dir %>">

Or programmatically, as shown below:

void Page_Load(object sender, EventArgs e)

{

    html.Attributes["dir"] = ((string) GetGlobalResourceObject("Global", "html_dir")).ToLower();

}