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
Dion Hinchcliffe is an internationally recognized digital expert, bestselling book author, frequent keynote speaker, analyst, futurist, and transformation expert based in Washington, DC. He is currently Chief Strategy Officer at the industry-leading digital strategy and online community solutions firm, 7Summits.
Digital Transformation is much more than a buzzword. The radical shift to digital mechanisms for almost every process is evident across all industries and verticals. This is often especially true in financial services, where the legacy environment is many times unable to keep up with the rapidly shifting demands of the consumer. The constant pressure to provide complete, omnichannel delivery of customer-facing solutions to meet both regulatory and customer demands is putting enormous pressure on...
IoT is rapidly becoming mainstream as more and more investments are made into the platforms and technology. As this movement continues to expand and gain momentum it creates a massive wall of noise that can be difficult to sift through. Unfortunately, this inevitably makes IoT less approachable for people to get started with and can hamper efforts to integrate this key technology into your own portfolio. There are so many connected products already in place today with many hundreds more on the h...
The standardization of container runtimes and images has sparked the creation of an almost overwhelming number of new open source projects that build on and otherwise work with these specifications. Of course, there's Kubernetes, which orchestrates and manages collections of containers. It was one of the first and best-known examples of projects that make containers truly useful for production use. However, more recently, the container ecosystem has truly exploded. A service mesh like Istio addr...
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...
Charles Araujo is an industry analyst, internationally recognized authority on the Digital Enterprise and author of The Quantum Age of IT: Why Everything You Know About IT is About to Change. As Principal Analyst with Intellyx, he writes, speaks and advises organizations on how to navigate through this time of disruption. He is also the founder of The Institute for Digital Transformation and a sought after keynote speaker. He has been a regular contributor to both InformationWeek and CIO Insight...
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...
To Really Work for Enterprises, MultiCloud Adoption Requires Far Better and Inclusive Cloud Monitoring and Cost Management … But How? Overwhelmingly, even as enterprises have adopted cloud computing and are expanding to multi-cloud computing, IT leaders remain concerned about how to monitor, manage and control costs across hybrid and multi-cloud deployments. It’s clear that traditional IT monitoring and management approaches, designed after all for on-premises data centers, are falling short in ...
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, discussed how Dice leverages data insights and tools to help both tech professionals and recruiters better understand how skills relate to each other and which skills are in high demand using interactive visualizations and salary indicator tools to maximize earning potential. Manish Dixit is VP of Product and Engineering at Dice. As the leader of the Product, Engineering and Data Sciences team at D...
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...