Support Eastern Languages in Your Struts Web Applications

Support Eastern Languages in Your Struts Web Applications

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 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:

            rel="stylesheet" type="text/css">

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:

  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 in 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.

devx-admin

devx-admin

Share the Post:
Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India,

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

Iran Drone Expansion

Iran’s Jet-Propelled Drone Reshapes Power Balance

Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional

Solar Geoengineering

Did the Overshoot Commission Shoot Down Geoengineering?

The Overshoot Commission has recently released a comprehensive report that discusses the controversial topic of Solar Geoengineering, also known as Solar Radiation Modification (SRM). The Commission’s primary objective is to

Remote Learning

Revolutionizing Remote Learning for Success

School districts are preparing to reveal a substantial technological upgrade designed to significantly improve remote learning experiences for both educators and students amid the ongoing pandemic. This major investment, which

Revolutionary SABERS Transforming

SABERS Batteries Transforming Industries

Scientists John Connell and Yi Lin from NASA’s Solid-state Architecture Batteries for Enhanced Rechargeability and Safety (SABERS) project are working on experimental solid-state battery packs that could dramatically change the

Build a Website

How Much Does It Cost to Build a Website?

Are you wondering how much it costs to build a website? The approximated cost is based on several factors, including which add-ons and platforms you choose. For example, a self-hosted

Battery Investments

Battery Startups Attract Billion-Dollar Investments

In recent times, battery startups have experienced a significant boost in investments, with three businesses obtaining over $1 billion in funding within the last month. French company Verkor amassed $2.1