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
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...