RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Support Eastern Languages in Your Struts Web Applications

Learn how to enable your Struts Web applications to handle Eastern languages such as Arabic just by using the basic Struts support for i18n and some additional Java/JSP code.

nternationalizing (i18n) Web applications has become a prerequisite for most Web development projects. Whether a Web site has a worldwide audience who speak different languages or a local audience in a country where the population commonly speaks more than one language, development teams need to have the proper application infrastructure to support internationalization.

Many Web development frameworks provide i18n support for this purpose. The Struts framework, one of the most commonly used, is no exception. Struts provides a set of classes and JSP tags that make it easier to work with i18n. This infrastructure enables Struts developers to produce multi-language applications with minimal extra effort and time.

Despite Struts' excellent i18n support, however, Struts applications that need to use Eastern languages such as Arabic require special attention and extra work. This article demonstrates how to build a Struts Web application that runs in English and Arabic. You will see how to set the user locale in Struts Action classes, set the appropriate HTML page direction, select the appropriate Cascade Style Sheet (CSS) file, and convert properties files so that Arabic characters are displayed correctly.

The Sample Application: Providing Language Choice

The sample application consists of two JSP pages: a login page where the user enters a login name/password and selects a language (either English or Arabic). Based on the user's selection, the application's home page displays in the appropriate language. It uses a Struts Action class to handle the login procedure. (Although the sample application was developed in JDeveloper 10g, you can open the project in any Java/J2EE editor because it is in a standard J2EE Web application format.)

Please note that in order to view Arabic characters, your operating system must be able to display them. For Microsoft Windows, you can check this from the Regional Settings under the Control Panel.

Click to enlarge
Figure 1. The Login Page for the Sample Application

The following sections describes in detail the sample application's various components.

The Login Page (Login.jsp)
Figure 1 shows the login page (login.jsp). As previously stated, in addition to the custom user name/password, the user can select a language (Arabic or English). You can enter any values for the user name/password, as the sample application does not perform real authentication. The important step is selecting the language.

The Login Action (LoginAction.java)
The LoginAction class is responsible for authenticating users. You can use any authentication mechanism. Listing 1 shows the execute method of the LoginAction class. Based on the language selected, the application creates and stores a java.util.Locale object in the session as follows:

request.getSession().setAttribute(org.apache.struts.Globals.LOCALE_KEY, new java.util.Locale("en","US") );

It stores the Locale object in the HTTP session object (javax.servlet.http. HttpSession) using the attribute name org.apache.struts.Globals.LOCALE_KEY, which the Struts framework uses to determine the correct language to use when loading text messages from properties files.

Eastern languages are written from right to left, so text direction is an important feature in this application--pages should align accordingly. To handle this, an attribute added to the session represents the appropriate direction as follows:

request.getSession().setAttribute( JspConstants.PAGE_DIR , "LTR" );

This attribute sets the HTML <body> tag with the correct value for the DIR attribute: LTR (Left To Right) or RTL (Right To Left).

In addition to displaying labels, the pages should use fonts and colors that suit the intended locale. This is why a separate CSS file is necessary for each locale. The sample application uses two CSS files: demo_en_US.css and demo_ar_JO.css for English and Arabic, respectively. The LoginAction class selects the appropriate CSS file based on the selected language as follows:

            request.getSession().setAttribute( JspConstants.PAGE_CSS , "demo_en_US.css" );

Click to enlarge
Figure 2. The Welcome Page in English

The Home Page (Home.jsp)
The home page displays a welcome message in the selected language. Figure 2 shows the English version of the page.

To display the page in Arabic, UTF-8 encoding must be specified in the JSP page as follows:

<%@ page contentType="text/html;charset=utf-8"%>

The proper CSS file is selected as follows:

<link href="<%= request.getContextPath()%>/css/<%= session.getAttribute( JspConstants.PAGE_CSS  ) %>" _
rel="stylesheet" type="text/css">

Next is selecting the appropriate page direction based on the attribute stored in the session:

<body dir="<%= session.getAttribute( JspConstants.PAGE_DIR  ) %>" >

Finally, a Struts tag is used to display a welcome message in the appropriate language:

  <bean:message key="welcomeMsg" />

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date