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) 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?

@ThingsExpo Stories
Nordstrom is transforming the way that they do business and the cloud is the key to enabling speed and hyper personalized customer experiences. In his session at 21st Cloud Expo, Ken Schow, VP of Engineering at Nordstrom, discussed some of the key learnings and common pitfalls of large enterprises moving to the cloud. This includes strategies around choosing a cloud provider(s), architecture, and lessons learned. In addition, he covered some of the best practices for structured team migration an...
Recently, REAN Cloud built a digital concierge for a North Carolina hospital that had observed that most patient call button questions were repetitive. In addition, the paper-based process used to measure patient health metrics was laborious, not in real-time and sometimes error-prone. In their session at 21st Cloud Expo, Sean Finnerty, Executive Director, Practice Lead, Health Care & Life Science at REAN Cloud, and Dr. S.P.T. Krishnan, Principal Architect at REAN Cloud, discussed how they built...
In his session at 21st Cloud Expo, Raju Shreewastava, founder of Big Data Trunk, provided a fun and simple way to introduce Machine Leaning to anyone and everyone. He solved a machine learning problem and demonstrated an easy way to be able to do machine learning without even coding. Raju Shreewastava is the founder of Big Data Trunk (www.BigDataTrunk.com), a Big Data Training and consulting firm with offices in the United States. He previously led the data warehouse/business intelligence and B...
In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, led attendees through the exciting evolution of the cloud. He looked at this major disruption from the perspective of technology, business models, and what this means for enterprises of all sizes. John Considine is General Manager of Cloud Infrastructure Services at IBM. In that role he is responsible for leading IBM’s public cloud infrastructure including strategy, development, and offering m...
With tough new regulations coming to Europe on data privacy in May 2018, Calligo will explain why in reality the effect is global and transforms how you consider critical data. EU GDPR fundamentally rewrites the rules for cloud, Big Data and IoT. In his session at 21st Cloud Expo, Adam Ryan, Vice President and General Manager EMEA at Calligo, examined the regulations and provided insight on how it affects technology, challenges the established rules and will usher in new levels of diligence arou...
The 22nd International Cloud Expo | 1st DXWorld Expo has announced that its Call for Papers is open. Cloud Expo | DXWorld Expo, to be held June 5-7, 2018, at the Javits Center in New York, NY, brings together Cloud Computing, Digital Transformation, Big Data, Internet of Things, DevOps, Machine Learning and WebRTC to one location. With cloud computing driving a higher percentage of enterprise IT budgets every year, it becomes increasingly important to plant your flag in this fast-expanding busin...
Smart cities have the potential to change our lives at so many levels for citizens: less pollution, reduced parking obstacles, better health, education and more energy savings. Real-time data streaming and the Internet of Things (IoT) possess the power to turn this vision into a reality. However, most organizations today are building their data infrastructure to focus solely on addressing immediate business needs vs. a platform capable of quickly adapting emerging technologies to address future ...
No hype cycles or predictions of a gazillion things here. IoT is here. You get it. You know your business and have great ideas for a business transformation strategy. What comes next? Time to make it happen. In his session at @ThingsExpo, Jay Mason, an Associate Partner of Analytics, IoT & Cybersecurity at M&S Consulting, presented a step-by-step plan to develop your technology implementation strategy. He also discussed the evaluation of communication standards and IoT messaging protocols, data...
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
DevOps at Cloud Expo – being held June 5-7, 2018, at the Javits Center in New York, NY – announces that its Call for Papers is open. Born out of proven success in agile development, cloud computing, and process automation, DevOps is a macro trend you cannot afford to miss. From showcase success stories from early adopters and web-scale businesses, DevOps is expanding to organizations of all sizes, including the world's largest enterprises – and delivering real results. Among the proven benefits,...
@DevOpsSummit at Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, is co-located with 22nd Cloud Expo | 1st DXWorld Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait...
Cloud Expo | DXWorld Expo have announced the conference tracks for Cloud Expo 2018. Cloud Expo will be held June 5-7, 2018, at the Javits Center in New York City, and November 6-8, 2018, at the Santa Clara Convention Center, Santa Clara, CA. Digital Transformation (DX) is a major focus with the introduction of DX Expo within the program. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive ov...
SYS-CON Events announced today that T-Mobile exhibited at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. As America's Un-carrier, T-Mobile US, Inc., is redefining the way consumers and businesses buy wireless services through leading product and service innovation. The Company's advanced nationwide 4G LTE network delivers outstanding wireless experiences to 67.4 million customers who are unwilling to compromise on qua...
SYS-CON Events announced today that Cedexis will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Cedexis is the leader in data-driven enterprise global traffic management. Whether optimizing traffic through datacenters, clouds, CDNs, or any combination, Cedexis solutions drive quality and cost-effectiveness. For more information, please visit https://www.cedexis.com.
SYS-CON Events announced today that Google Cloud has been named “Keynote Sponsor” of SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Companies come to Google Cloud to transform their businesses. Google Cloud’s comprehensive portfolio – from infrastructure to apps to devices – helps enterprises innovate faster, scale smarter, stay secure, and do more with data than ever before.
SYS-CON Events announced today that Vivint to exhibit at SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California. As a leading smart home technology provider, Vivint offers home security, energy management, home automation, local cloud storage, and high-speed Internet solutions to more than one million customers throughout the United States and Canada. The end result is a smart home solution that sav...
SYS-CON Events announced today that Opsani will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Opsani is the leading provider of deployment automation systems for running and scaling traditional enterprise applications on container infrastructure.
SYS-CON Events announced today that Nirmata will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Nirmata provides a comprehensive platform, for deploying, operating, and optimizing containerized applications across clouds, powered by Kubernetes. Nirmata empowers enterprise DevOps teams by fully automating the complex operations and management of application containers and its underlying ...
SYS-CON Events announced today that Opsani to exhibit at SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California. Opsani is creating the next generation of automated continuous deployment tools designed specifically for containers. How is continuous deployment different from continuous integration and continuous delivery? CI/CD tools provide build and test. Continuous Deployment is the means by which...