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.
|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 HTMLtag 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_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" );
|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:
Next is selecting the appropriate page direction based on the attribute stored in the session:
Finally, a Struts tag is used to display a welcome message in the appropriate language:
Conversion of Arabic Characters to Unicode (native2ascii.exe)
Struts stores Locale-specific messages in properties files. The sample application uses the following two files:
ApplicationResources_ar_JO.propertiesfor Arabic messages
ApplicationResources_en_US.propertiesfor English messages
|Figure 3. Improperly Displayed Arabic Text|
Intuitively, you would place Arabic text in the properties file and expect the application to display its home page in Arabic when a user selects Arabic. However, just putting the Arabic text in the properties files would result in the Arabic text displaying as question marks (see Figure 3) because text in the Properties file is not loaded in Unicode format.
You have to convert all Arabic characters to Unicode and write them in Unicode hex format in the properties files. Doing this manually is tedious. It’s better to use
native2ascii.exe, a utility located in the
native2ascii, write Arabic messages in a text file (call it 1.txt) and then run the following code:
native2ascii -encoding utf-8 c:1.txt c:2.txt
|Figure 4. Properly Displayed Arabic Text|
You can then copy the converted text from the output file (2.txt) to the Arabic properties file. (Find more information about
native2ascii at java.sun.com.)
After doing this, you will see the Arabic version as shown in Figure 4.
Nothing Lost in Translation
As simply as that, you can enable your Web applications to handle Eastern languages just by using the basic Struts support for i18n and some additional Java/JSP code. You also can easily enhance the functionality presented in the sample application. For example, some applications require users to be able to change the locale in any page, not just at login. You can enable the sample application to do this by adding an Action class that is both responsible for changing language and capable of being called anywhere in the application.