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 Solution
When the challenges were completely understood, it was decided that a custom Locale and a custom Resource bundle should be used while providing a solution. The custom Resource bundle will be more like a property resource bundle, in which case a key will be used to retrieve a corresponding value based on the custom Locale. Since this custom resource bundle was expected to be CMA database-centric, essentially the key will be an index into the content in the CMA database supplemented by a custom locale.

The foremost thing we ensured was that the underlying CMA database schema supported retrieval of content based on a content key and a custom locale. The existing CMA database schema did not fully support what we were looking for in terms of a content key. We had to come up with extensions without disturbing other parts of the schema so that localized content could be retrieved by using a unique content key. Based on these extensions, the CMA UI interface was also modified to provide the ability to create content with a unique content key in multiple different languages and the ability to target the content to a particular locale. Figure 6 is a simplified view of the extensions made to the schema with the existing design constraints in place.

The custom implementation of the database-centric Resource bundle was pretty straightforward. The custom Resource bundle class "DynaResourceBundle" was created by extending java.util.ResourceBundle and by overriding the "handleGetObject" method. The "handleGetObject" method is called internally when either the "getString" or the "getObject" method of the Resource bundle is invoked. An instance of this class will be created by passing the custom locale and a String array containing resource names. A constant resource name "CMAX" was used to indicate to the bundle to pull content from the underlying CMA database. All other resource names were considered as non-CMA resources. For all non-CMA resources, the usual ResourceBundle.getBundle method was first used to load the bundle using the resource name and the current locale, and then the content value was retrieved using the key. The resources are tried out in the order they are found in the array while retrieving the content. If the current resource in the array returns null, then the next resource is used to retrieve the content. This is a simple failover approach in case content is not found in one resource. Listing 1 shows the concrete implementation of this method.

Please note that "Label" is a data object representing a label entry in the CMA database. The "handleGetObject" method internally uses a "ContentBO" instance while retrieving content from CMA. The "ContentBO" is a content retrieval business helper object that actually interacts with the data access objects to retrieve content from CMA. The "ContentBO" provides a "getLabel" method that looks up a "DAOFactory" class to get a reference to the DAO implementation for labels and invokes the "getContent" method on the DAO by passing the content key and the locale. The "ContentBO" also has a static method - "getFallBackLocales" - that implements the business requirement for the content look-up fallback logic approach as described earlier. It turns out that this requirement translates into the fallback of custom locales in our design and fits well with the approach we took. So ultimately our choice of the custom locale with "site name" as a variant paid rich dividends. Listing 2 is a code snippet for the "getLabel" method in the ContentBO object.

The "LabelDAO" class is extended out of an abstract DAO class and provides the concrete implementation for retrieving the content from the database. This class implements the "getContent" method; the content key and the custom locale are the arguments to this method.

class LabelDAO extends AbstractDAO {
public CMAObject getContent( String contentKey, Locale locale ) throws SQLException{
     .......
}
}

Note that this class has package-level access in the package "com.ual.i18n.content.dao" since an instance of this class is actually created by the DAOFactory that is part of the same package. The method "getContent" simply retrieves the language, country, and site name from the locale object, builds the SQL using the content key, and executes the SQL to retrieve the label from the CMA database. Listing 3 shows a code snippet from the method "getContent."

Once the text of the label is retrieved from the database, this method invokes the inherited method "getEncodedText" by passing the retrieved text and "UTF-8" as the character set encoding. The following code snippet is for the "getEncodedText" method defined in the "AbstractDAO" class:


public String getEncodedText
( String text, String encoding ) throws UnsupportedEncodingException {
if( text == null ){
return null;
}

String encodedText = new String( text.getBytes( "8859_1" ), encoding );

return encodedText;
}
The "getEncodedText" method converts the text String retrieved from the database to a String encoded with UTF-8 charset. This conversion is required because by default a String is constructed from a sequence of bytes by using the underlying platform's default character encoding. The specification of "8859_1" as a transit encoding to retrieve a byte array from the text String is only a precautionary measure, because a String object's default "getBytes()" method encodes text into a sequence of bytes using the platform's default charset. The behavior of this method when the String cannot be encoded in the default charset is unspecified.

Figure 7 summarizes the method calls required to retrieve a localized label from the "DynaResourceBundle" class while retrieving the content from a CMA database.

Internationalization with Struts
As expected, Struts internationalization support is based on use of Locale class and Resource bundles. However, Struts slightly differs in terms of handling resource bundles. Struts uses the "MessageResources" class and its sub class "PropertyMessageResources" in the "org.apache.struts.util" package to represent resource bundles. Even though Struts web application developers normally do not directly work with an instance of "MessageResources," it provides an API to retrieve localized messages. Struts uses this API internally to provide appropriate messages to the framework clients. One of the reasons why Struts uses message resources to represent resource bundles is because Java's resource bundles are not serializable and Struts wanted to create its own serializable representations. Struts requires that any object it manages internally be serializable because a Struts-based Web application can be deployed in a clustered server environment where session data replication could be enabled.

The big question at that point was how to make our own custom resource bundle "DynaResourceBundle" work with Struts in a way that is transparent to the rest of the application. After much research, we could not find a direct way of incorporating "DynaResourceBundle" in Struts. We had to look into other options. Typically, Struts looks up the following configuration in the struts-config.xml to load an instance of the "MessageResources" class representing an "application.properties" file and retains it in memory for the life of the application.

<message-resources parameter="application" null="false" />

The promise of Struts has always been to enable the incorporation of custom "MessageResources" implementations that retrieve data from other sources such as a database. With this promise in mind, we moved forward with the idea of extending the "MessageResources" class in such a way that we could glue our custom resource bundle to the Struts framework. We created a custom class - "DynaMessageResources" - by extending "MessageResources" as shown in Listing 4.

The main methods that are overridden are the "getMessage" methods. The method that is internally called by the Struts framework is the overloaded "getMessage" method that takes locale, key, and an array of objects. The array of objects is typically passed to this method to replace indexed parameters in the actual messages. The indexed parameters ({0}, {1}) are placeholders in a message that can be replaced at run time with certain values.

The MessageResources class internally uses the "java.text.MessageFormat" class to format messages with run-time values. The MessageResources class provides a default implementation for the "getMessage" method with three arguments; however, one word of caution about this method: it caches the message format objects for each message in a local HashMap for the life of the application. This may prove to be undesirable if the custom message resources class is not supposed to cache because either there is no requirement to cache any messages or caching could be done outside of this class with much more control to refresh the cache on demand. This was the main reason why we had to override that method by eliminating the need to cache the message format objects in a local HashMap. However, the core function of the method was not taken away in the sense that it still performed formatting of the messages using the MessageFormat objects but without caching them. Note that this method internally calls the other overloaded method "getMessage" that takes two arguments: locale and the message key.

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
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...
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...
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...
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.
Whenever a new technology hits the high points of hype, everyone starts talking about it like it will solve all their business problems. Blockchain is one of those technologies. According to Gartner's latest report on the hype cycle of emerging technologies, blockchain has just passed the peak of their hype cycle curve. If you read the news articles about it, one would think it has taken over the technology world. No disruptive technology is without its challenges and potential impediments t...
If a machine can invent, does this mean the end of the patent system as we know it? The patent system, both in the US and Europe, allows companies to protect their inventions and helps foster innovation. However, Artificial Intelligence (AI) could be set to disrupt the patent system as we know it. This talk will examine how AI may change the patent landscape in the years to come. Furthermore, ways in which companies can best protect their AI related inventions will be examined from both a US and...
Bill Schmarzo, Tech Chair of "Big Data | Analytics" of upcoming CloudEXPO | DXWorldEXPO New York (November 12-13, 2018, New York City) today announced the outline and schedule of the track. "The track has been designed in experience/degree order," said Schmarzo. "So, that folks who attend the entire track can leave the conference with some of the skills necessary to get their work done when they get back to their offices. It actually ties back to some work that I'm doing at the University of San...
When talking IoT we often focus on the devices, the sensors, the hardware itself. The new smart appliances, the new smart or self-driving cars (which are amalgamations of many ‘things'). When we are looking at the world of IoT, we should take a step back, look at the big picture. What value are these devices providing. IoT is not about the devices, its about the data consumed and generated. The devices are tools, mechanisms, conduits. This paper discusses the considerations when dealing with the...
Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: Driving Business Strategies with Data Science," is responsible for setting the strategy and defining the Big Data service offerings and capabilities for EMC Global Services Big Data Practice. As the CTO for the Big Data Practice, he is responsible for working with organizations to help them identify where and how to start their big data journeys. He's written several white papers, is an avid blogge...