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

Internationalization with JSTL
JSP standard tag libraries (JSTL) are built with inherent support for internationalization. The JSTL internationalization tags can be classified into the following two categories:

1.  Tags that support display of localized messages:

  • <fmt:setLocale>
  • <fmt:setBundle>
  • <fmt:bundle>
  • <fmt:message>
2.  Tags that support locale-specific formatting
  • <fmt:formatNumber>
  • <fmt:formatDate>
  • <fmt:parseNumber>
  • <fmt:parseDate>
These internationalization tags work directly from the locale and current resource bundle settings in the JSTL configuration. JSTL provides the "javax.servlet.jsp.jstl.core.Config" class that governs these settings. Specifically, the current user locale is stored in the configuration setting using the "Config.FMT_LOCALE" key. The resource bundle to be used for the current locale is stored in the configuration setting using an instance of helper bean "javax.servlet.jsp.jstl.fmt.LocalizationContext." This instance is configured using the "Config.FMT_LOCALIZATION_CONTEXT" key.

The <fmt:setLocale> tag enables explicit setting of the locale. It uses the "Config.FMT_LOCALE" key internally to store the locale object in the configuration so that other internationalization tags can use it. Optionally, the locale can be set in a particular scope using the "scope" attribute. The following is an example use of this tag:

<fmt:setLocale value="zh-CN" variant="discount" />

The <fmt:setBundle> tag enables explicit setting of a resource bundle for the current locale so that localized messages can be retrieved from that bundle. The mandatory "basename" attribute of this tag determines the resource bundle to load. Internally, this tag looks up the current locale using the "Config.FMT_LOCALE" key. It creates an instance of "LocalizationContext" to store the loaded bundle using the current locale, and stores that instance in the specified scope or by default in the page scope. The following is a typical use of this tag:

<fmt:setBundle basename="messages" scope="session" />

The <fmt:bundle> is a convenient tag to override the current localization context in such a way that a different resource bundle can be used within the nested scope of this tag. This tag uses the mandatory "basename" attribute to load the resource bundle using the current locale configuration setting. It then creates an instance of "LocalizationContext" to store the resource bundle and the current locale so that any other internationalization tags nested inside its body can avail themselves of that information. The following is the typical use of this tag:

<fmt:bundle basename="messages" />

The <fmt:message> tag is the major consumer of the underlying resource bundle and is the primary way of displaying localized messages in a JSP page. A stand-alone usage of this tag (shown in the following snippet) uses the underlying "LocalizationContext" instance to look up the resource bundle and retrieve messages using the value specified for the key attribute:

<fmt:message key="ui.i18n.label.shopForFlights" />

This tag has an optional "bundle" attribute that can be used to directly point to an instance of "LocalizationContext." If this attribute is specified, this tag will not look up for the "LocalizationContext" in the JSTL configuration but will instead directly access the specified instance to retrieve the resource bundle. In general, the following is the look-up order used by the <fmt:message> tag to look up the resource bundle for localized messages:

  1. The "LocalizationContext" instance specified by the "bundle" attribute
  2. The "LocalizationContext" instance specified by <fmt:bundle> tag, if nested within the <fmt:bundle> tag
  3. The "LocalizationContext" specified in the JSTL configuration either by <fmt:setBundle> or explicitly by custom Java code. The order of look up in this case is as follows:
    a. Page scope
    b. Request scope
    c. Session scope
    d. Application scope
Note that the key used to retrieve the message from the underlying resource bundle can be specified in the body of the tag. This helps in generating the key dynamically during run time, as shown in the following snippet:

<fmt:message>
<c:out value="ui.i18n.label.month.${month.month}" />
</fmt:message>

It is worth mentioning here that the default resource bundle to be used by the JSTL configuration can be specified in the "web.xml" file as follows so that there is no need to explicitly set the bundle either by using <fmt:setBundle> or by using custom Java code.


<web-app>
.......
<context-param>
<param-name>
javax.servlet.jsp.jstl.fmt.localizationContext
</param-name>

<param-value>
applicationMessages
</param-value>
</context-param>
.......
</web-app>
Tags that support locale-specific formatting use the current locale specified in the JSTL configuration through the "Config.FMT_LOCALE" key. The primary purpose of these tags is to format the data in a manner that is appropriate for the current locale. The <fmt:formatNumber> and <fmt:formatDate> are the most frequently used tags to display data in a locale-sensitive manner.

The <fmt:formatNumber> tag in its simplest form is used to display numbers formatted based on current locale. Optionally the "type" attribute can be specified to instruct the tag to print the numeric value as a regular number, currency, or percentage.

<fmt:formatNumber value="1000000.05"/>

Table 5 shows the formatting done by the above tag in different locales. The <fmt:formatDate> tag is used to format a date based on the current locale. The "value" attribute is used to specify the variable of java.util.Date to be formatted. The following code snippet shows an example:

<jsp:useBean id="today" class="java.util.Date"/>
<font class="generalSmallLabel" >Today: </font>
<br/>
<font class="generalLabel">
<fmt:formatDate value="${today}"/></font>

Table 6 shows the date formatting for different locales.

Incorporating "DynaResourceBundle" in JSTL
It was very easy to incorporate "DynaResourceBundle" into JSTL after understanding the internals of its internationalization support. Once the custom user locale was identified by the application based on the current user request, an instance of "DynaResourceBundle" was created and set explicitly in the JSTL configuration using the "setLocale" method in the "SessionHelper" object as shown in Listing 8.

Figure 10 shows how the <fmt:message> tag interacts with "DynaResourceBundle" to retrieve the localized messages.

Setting the Page Encoding Scheme
Setting the character set encoding is a final but critical step in making sure that the content is displayed correctly. For all JSP pages, the character set encoding was set using the "pageEncoding" attribute of the "page" directive. As discussed before, "UTF-8" was set as the character encoding scheme:

<%@ page pageEncoding="UTF-8" %>

If there were many JSP pages that use a common template JSP page, it would be easy to specify the page encoding in one single location in the common template JSP.

Encoding Scheme for the Database
If the localized content is stored in a database such as Oracle, it is important to make sure that the correct character set encoding scheme is specified for the database. Oracle gives options to specify the character set encoding to be used at the time of database creation. The character set encoding used by Oracle database can be verified by executing the following SQL:

select * from NLS_DATABASE_PARAMETERS

Among other things, the above SQL gives information about the character set property. The parameters "NLS_CHARACTERSET" and "NLS_NCHAR_CHARACTERSET" will indicate the character set encoding scheme used by the database. Table 7 shows an example.

Summary
The design for internationalization should be an up-front task and not an afterthought. The design and implementation of internationalization should be carried out after careful evaluation of the exact application internationalization requirements. The choice of locale plays an important role in the implementation strategy. The internationalization requirements in general will also help in determination of the implementation approach: a single set of JSPS for all locales or separate sets of JSPS per locale. Internationalization in a J2EE Web application can be accomplished by using many different methods and technologies. This article showed an example implementation using Struts framework and JSTL custom tags by extending the support provided by both in a manner suitable to the custom needs of the application. In practice, it is easy to store and manage huge amounts of localized content in a database instead of storing it in application properties files. The right choice of character set encoding is a must for internationalizing a Web application.

Acknowledgements
We would like to thank our director Virgil Bistriceanu for his continuous support and inspiration to write this article. He has monitored the design from day one and provided crucial feedback that helped us make necessary changes in the design. Because he is a hardcore technologist himself, he made sure that our design strategy aligned with the enterprise architectural direction. We also would like to thank our managers Helen Agulnik and Ken Kunz and all other team members who supported and participated in the implementation.

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
Announcing Poland #DigitalTransformation Pavilion
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...
CloudEXPO | 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.
DXWorldEXPO LLC announced today that All in Mobile, a mobile app development company from Poland, will exhibit at the 22nd International CloudEXPO | DXWorldEXPO. All In Mobile is a mobile app development company from Poland. Since 2014, they maintain passion for developing mobile applications for enterprises and startups worldwide.
The best way to leverage your CloudEXPO | DXWorldEXPO presence as a sponsor and exhibitor is to plan your news announcements around our events. The press covering CloudEXPO | DXWorldEXPO will have access to these releases and will amplify your news announcements. More than two dozen Cloud companies either set deals at our shows or have announced their mergers and acquisitions at CloudEXPO. Product announcements during our show provide your company with the most reach through our targeted audienc...
@DevOpsSummit at Cloud Expo, taking place November 12-13 in New York City, NY, is co-located with 22nd international CloudEXPO | first international DXWorldEXPO and will feature technical sessions from a rock star conference faculty and the leading industry players in the world.
Everything run by electricity will eventually be connected to the Internet. Get ahead of the Internet of Things revolution. In his session at @ThingsExpo, Akvelon expert and IoT industry leader Sergey Grebnov provided an educational dive into the world of managing your home, workplace and all the devices they contain with the power of machine-based AI and intelligent Bot services for a completely streamlined experience.
DXWorldEXPO | CloudEXPO 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.
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 ...
In his keynote at 19th Cloud Expo, Sheng Liang, co-founder and CEO of Rancher Labs, discussed the technological advances and new business opportunities created by the rapid adoption of containers. With the success of Amazon Web Services (AWS) and various open source technologies used to build private clouds, cloud computing has become an essential component of IT strategy. However, users continue to face challenges in implementing clouds, as older technologies evolve and newer ones like Docker c...
"MobiDev is a software development company and we do complex, custom software development for everybody from entrepreneurs to large enterprises," explained Alan Winters, U.S. Head of Business Development at MobiDev, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
JETRO showcased Japan Digital Transformation Pavilion at SYS-CON's 21st International Cloud Expo® at the Santa Clara Convention Center in Santa Clara, CA. The Japan External Trade Organization (JETRO) is a non-profit organization that provides business support services to companies expanding to Japan. With the support of JETRO's dedicated staff, clients can incorporate their business; receive visa, immigration, and HR support; find dedicated office space; identify local government subsidies; get...
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.
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...
In past @ThingsExpo presentations, Joseph di Paolantonio has explored how various Internet of Things (IoT) and data management and analytics (DMA) solution spaces will come together as sensor analytics ecosystems. This year, in his session at @ThingsExpo, Joseph di Paolantonio from DataArchon, added the numerous Transportation areas, from autonomous vehicles to “Uber for containers.” While IoT data in any one area of Transportation will have a huge impact in that area, combining sensor analytic...
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...
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...
Michael Maximilien, better known as max or Dr. Max, is a computer scientist with IBM. At IBM Research Triangle Park, he was a principal engineer for the worldwide industry point-of-sale standard: JavaPOS. At IBM Research, some highlights include pioneering research on semantic Web services, mashups, and cloud computing, and platform-as-a-service. He joined the IBM Cloud Labs in 2014 and works closely with Pivotal Inc., to help make the Cloud Found the best PaaS.
It is of utmost importance for the future success of WebRTC to ensure that interoperability is operational between web browsers and any WebRTC-compliant client. To be guaranteed as operational and effective, interoperability must be tested extensively by establishing WebRTC data and media connections between different web browsers running on different devices and operating systems. In his session at WebRTC Summit at @ThingsExpo, Dr. Alex Gouaillard, CEO and Founder of CoSMo Software, presented ...
I think DevOps is now a rambunctious teenager - it's starting to get a mind of its own, wanting to get its own things but it still needs some adult supervision," explained Thomas Hooker, VP of marketing at CollabNet, in this SYS-CON.tv interview at DevOps Summit at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.