Browse DevX
Sign up for e-mail newsletters from DevX


Support Eastern Languages in Your Struts Web Applications : Page 2

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Conversion of Arabic Characters to Unicode (native2ascii.exe)
Struts stores Locale-specific messages in properties files. The sample application uses the following two files:
  1. ApplicationResources_ar_JO.properties for Arabic messages
  2. ApplicationResources_en_US.properties for English messages

Click to enlarge
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 <JDK_HOME>\bin folder of the Java SDK. It converts the characters written in Arabic to Unicode.

To run 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

Click to enlarge
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.

Tareq Shaheen is an architect and senior consultant with Ejada, an IT solutions and services company.
Thanks for your registration, follow us on our social networks to keep up-to-date