Weblogic Authors: Yeshim Deniz, Elizabeth White, Michael Meiner, Michael Bushong, Avi Rosenthal

Related Topics: Weblogic

Weblogic: Article

Cover Story: A Practical Solution to Internationalization of a J2EE Web App

Making Web Applications Multilingual

The "getMessage" method with two arguments is the method of interest. This is the method that binds our custom resource bundle with the Struts framework. When invoked, this method first looks up the "DynaResourceBundle" instance for the specified locale and calls the "getString" method on the bundle to retrieve the message for the specified key. This method uses a private method - "getBundle" - to look up an instance of the resource bundle for the specified locale. The private "getBundle" method manages a HashMap to store a locale-specific instance of "DynaResourceBundle" and creates a new instance of the bundle, if necessary. In this way, locale-specific instances of "DynaResourceBundle" are cached for the life of the application. The sequence diagram shown in Figure 8 summarizes the main interactions.

In order to incorporate a custom "MessageResources" implementation in Struts, a custom "MessageResourcesFactory" should be created by extending the "org.apache.struts.util.MessageResourcesFactory" class. "MessageResourcesFactory" is an abstract class that declares an abstract method called "createResources." In the current application, a factory, "DynaMessageResourcesFactory," was created by providing implementation for the "createResources" method. The following is the code snippet for this class:

public class DynaMessageResourcesFactory extends MessageResourcesFactory{
public DynaMessageResourcesFactory(){

public MessageResources createResources( String config ){
return new DynaMessageResources(this, config, this.returnNull);
The custom factory is incorporated in the Struts framework by specifying the factory class in the Struts configuration file as follows:

<message-resources factory="com.ual.i18n.ui.core.DynaMessageResourcesFactory"
parameter="CMAX, MessageResources" null="false"/>

Please note that the values specified for the attribute "parameter" are passed to the "createResources" method as config parameters. The Boolean value for the attribute "null" is set in the factory when the "setReturnNull" method is invoked on the factory at the time of its creation. The "createResources" method is where an instance of "DynaMessageResources" is created and glued into the Struts framework.

The major consumers of the messages retrieved from the "MessageResources" instance are the locale-sensitive JSP tags in the Struts framework. Table 4 shows some of the attributes of the most commonly used Struts locale-sensitive tags that retrieve messages internally from the underlying "MessageResources."

Listing 5 is from the JSP page that uses some of these tags while retrieving content using content keys from the underlying "DynaMessageResources."

The most commonly used tag for writing out localized content is the Struts bean:message tag in the bean tag library. Please note that Struts by default sets up the user locale as an attribute in the user session using "Globals.LOCALE_KEY" as the key. Subsequently, the user locale can be retrieved at any point using the key. Figure 9 shows how this tag pulls the content from the underlying "DynaMessageResources" instance in the current example.

Localizing Other Struts Components
There are other components in Struts that participate in displaying localized messages while interacting with the underlying message resources instance. The important ones are the "ActionErrors" and the "ActionMessages" form validators and tiles configuration.

Typically, application-specific validations are performed within the Struts actions, and in many cases there will be a need to display correct error messages to the user. Instances of "ActionErrors" are used to relay the error messages to the JSP page. The following is a code snippet showing how an "ActionErrors" instance is constructed.

ActionErrors errors = new ActionErrors();
ActionError error = new ActionError( "ui.i18n.error.from.field.incorrect" );
errors.add( ActionErrors.GLOBAL_ERROR, error );
this.saveErrors( request, errors );

A content key such as "ui.i18n.error.from.field.incorrect" is used to represent the error message to be displayed to the user. On the JSP page, an "html:errors" tag is used to display all of the errors relayed to the page as shown in the following code snippet:

<html:errors id="error" >
     <bean:write name="error" /><BR>

The important point to note is that internally, the html:errors tag uses the content key specified in the constructor of the "ActionError" and the current user locale to retrieve the localized error message from the underlying "MessageResources" instance.

The "ActionMessages" are similarly used to relay any localized messages to the JSP page. The html:messages tag is used to loop through each action message and retrieve the localized message from the underlying "MessageResources" instance.

One important feature of Struts is its inbuilt validation framework. The validation framework provides a declarative way of specifying validation settings for HTML form validations. The user input can be validated using settings specified in a XML configuration file. Each HTML form is represented as a <form> element and uses a name attribute corresponding to the form-bean name specified in the Struts configuration file. The validations for each HTML field can be declared under the <form> element using a <field> element. The validation settings include labels and message keys that will be used to display the messages in case the field validation fails. Listing 6 is an example declaration.

An interesting point is that the <form> element itself can be specified per locale in case validation settings differ per locale as follows:

<form name="tripTemplateForm" locale="pt" country="BR" >

The configuration of tiles can be localized if the structure of the page layouts differs per locale. The localization of tiles configuration can be accomplished by following a naming convention similar to the one used for properties files. For example, if the Web application needs a separate tiles definition for a Brazilian Portuguese locale, then the naming of the tiles configuration should be "tiles_pt_BR.xml."

Identification of the User Locale
Because our application was using a custom locale and not the browser locale, it was necessary to design a strategy to identify a user locale and use it for the life of the user session, unless a user explicitly selects a new locale by changing any of the custom locale attributes such as language, POS country, or booking site. It was decided that the first-time entry URL will be used to determine the initial user locale. Specifically, the user locale will be determined using the Servlet path of the URI as shown in the following example:


In this example, the custom locale is determined from the Servlet path "/core/BR/pt," indicating that the current request is for a core booking site with Brazil as POS country and Portuguese as the language in which to display content. This information should be retrieved at the entry point into the application in such a way that custom locale and resource bundle are populated even before any application processing occurs. The natural place to do this would be in the control Servlet. Because the current application is driven by Struts, the decision was made to create a control Servlet extending Struts's ActionServlet class. The major responsibility of this class is to tap the user request and check for user locales before the request is mapped to a Struts action class. Listing 7 shows the class and its implementation of the "service" method.

Please note that, as a standard practice, a "SessionHelper" object is used by providing a standard-typed API to work with session attributes so that attribute names are not scattered across the application code, but rather centralized in a single object. The "SessionHelper" method that sets locales in a user session is the "setLocale" method. This method will be discussed in the next section.

More Stories By Murali Kashaboina

Murali Kashaboina leads Enterprise Architecture at United Airlines, Inc. He has 15+ years of enterprise software development experience utilizing a broad range of technologies, including JEE, CORBA, Tuxedo, and Web services. Murali previously published articles in WLDJ and SilverStream Developer Center. He has master’s degree in mechanical engineering from the University of Dayton, Ohio.

More Stories By Bin Liu

Bin Liu is a lead software engineer at United Airlines. Bin has more than seven years of experience developing distributed applications using J2EE technologies, WebLogic, Tuxedo, C++, and Web services. Bin has previously published articles in WLDJ.

Comments (2) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

Most Recent Comments
Raj Kumar Kundu 06/04/08 10:52:33 PM EDT

This content is very useful for all those people who are thinking about internationalization of J2EE/ Web Based applications. It explains and points out the areas which should be rather can be considered for this activity. This can help people start thinking in right direction.
But this can be made extremely useful by providing some example files (Resource Bundle related AppResource files and the java files which are using those property files) or snaps of the java codes.

Henry 10/23/07 08:34:21 PM EDT

Is database-centric internationalization with JSF similar with this article?

IoT & Smart Cities Stories
Early Bird Registration Discount Expires on August 31, 2018 Conference Registration Link ▸ HERE. Pick from all 200 sessions in all 10 tracks, plus 22 Keynotes & General Sessions! Lunch is served two days. EXPIRES AUGUST 31, 2018. Ticket prices: ($1,295-Aug 31) ($1,495-Oct 31) ($1,995-Nov 12) ($2,500-Walk-in)
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
Nicolas Fierro is CEO of MIMIR Blockchain Solutions. He is a programmer, technologist, and operations dev who has worked with Ethereum and blockchain since 2014. His knowledge in blockchain dates to when he performed dev ops services to the Ethereum Foundation as one the privileged few developers to work with the original core team in Switzerland.
René Bostic is the Technical VP of the IBM Cloud Unit in North America. Enjoying her career with IBM during the modern millennial technological era, she is an expert in cloud computing, DevOps and emerging cloud technologies such as Blockchain. Her strengths and core competencies include a proven record of accomplishments in consensus building at all levels to assess, plan, and implement enterprise and cloud computing solutions. René is a member of the Society of Women Engineers (SWE) and a m...
Digital Transformation and Disruption, Amazon Style - What You Can Learn. Chris Kocher is a co-founder of Grey Heron, a management and strategic marketing consulting firm. He has 25+ years in both strategic and hands-on operating experience helping executives and investors build revenues and shareholder value. He has consulted with over 130 companies on innovating with new business models, product strategies and monetization. Chris has held management positions at HP and Symantec in addition to ...
The challenges of aggregating data from consumer-oriented devices, such as wearable technologies and smart thermostats, are fairly well-understood. However, there are a new set of challenges for IoT devices that generate megabytes or gigabytes of data per second. Certainly, the infrastructure will have to change, as those volumes of data will likely overwhelm the available bandwidth for aggregating the data into a central repository. Ochandarena discusses a whole new way to think about your next...
CloudEXPO | DevOpsSUMMIT | DXWorldEXPO are the world's most influential, independent events where Cloud Computing was coined and where technology buyers and vendors meet to experience and discuss the big picture of Digital Transformation and all of the strategies, tactics, and tools they need to realize their goals. Sponsors of DXWorldEXPO | CloudEXPO benefit from unmatched branding, profile building and lead generation opportunities.
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...
All in Mobile is a place where we continually maximize their impact by fostering understanding, empathy, insights, creativity and joy. They believe that a truly useful and desirable mobile app doesn't need the brightest idea or the most advanced technology. A great product begins with understanding people. It's easy to think that customers will love your app, but can you justify it? They make sure your final app is something that users truly want and need. The only way to do this is by ...
DXWorldEXPO LLC announced today that Big Data Federation to Exhibit at the 22nd International CloudEXPO, colocated with DevOpsSUMMIT and DXWorldEXPO, November 12-13, 2018 in New York City. Big Data Federation, Inc. develops and applies artificial intelligence to predict financial and economic events that matter. The company uncovers patterns and precise drivers of performance and outcomes with the aid of machine-learning algorithms, big data, and fundamental analysis. Their products are deployed...