Welcome!

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(){
super();
}

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>
</html:errors>

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" >
     ........
</form>

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:

http://hostname/i18n/core/BR/pt/displayInput.do

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)

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.


IoT & Smart Cities Stories
The deluge of IoT sensor data collected from connected devices and the powerful AI required to make that data actionable are giving rise to a hybrid ecosystem in which cloud, on-prem and edge processes become interweaved. Attendees will learn how emerging composable infrastructure solutions deliver the adaptive architecture needed to manage this new data reality. Machine learning algorithms can better anticipate data storms and automate resources to support surges, including fully scalable GPU-c...
Machine learning has taken residence at our cities' cores and now we can finally have "smart cities." Cities are a collection of buildings made to provide the structure and safety necessary for people to function, create and survive. Buildings are a pool of ever-changing performance data from large automated systems such as heating and cooling to the people that live and work within them. Through machine learning, buildings can optimize performance, reduce costs, and improve occupant comfort by ...
The explosion of new web/cloud/IoT-based applications and the data they generate are transforming our world right before our eyes. In this rush to adopt these new technologies, organizations are often ignoring fundamental questions concerning who owns the data and failing to ask for permission to conduct invasive surveillance of their customers. Organizations that are not transparent about how their systems gather data telemetry without offering shared data ownership risk product rejection, regu...
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...
Poor data quality and analytics drive down business value. In fact, Gartner estimated that the average financial impact of poor data quality on organizations is $9.7 million per year. But bad data is much more than a cost center. By eroding trust in information, analytics and the business decisions based on these, it is a serious impediment to digital transformation.
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As automation and artificial intelligence (AI) power solution development and delivery, many businesses need to build backend cloud capabilities. Well-poised organizations, marketing smart devices with AI and BlockChain capabilities prepare to refine compliance and regulatory capabilities in 2018. Volumes of health, financial, technical and privacy data, along with tightening compliance requirements by...
Predicting the future has never been more challenging - not because of the lack of data but because of the flood of ungoverned and risk laden information. Microsoft states that 2.5 exabytes of data are created every day. Expectations and reliance on data are being pushed to the limits, as demands around hybrid options continue to grow.
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 ...
Enterprises have taken advantage of IoT to achieve important revenue and cost advantages. What is less apparent is how incumbent enterprises operating at scale have, following success with IoT, built analytic, operations management and software development capabilities - ranging from autonomous vehicles to manageable robotics installations. They have embraced these capabilities as if they were Silicon Valley startups.
As IoT continues to increase momentum, so does the associated risk. Secure Device Lifecycle Management (DLM) is ranked as one of the most important technology areas of IoT. Driving this trend is the realization that secure support for IoT devices provides companies the ability to deliver high-quality, reliable, secure offerings faster, create new revenue streams, and reduce support costs, all while building a competitive advantage in their markets. In this session, we will use customer use cases...