The J2EETM Tutorial
Home
TOC
Index
PREV TOP NEXT Search
Feedback

Internationalizing Web Clients

Internationalization is the process of preparing an application to support various languages. Localization is the process of adapting an internationalized application to support a specific language or locale. Although all client user interfaces should be internationalized and localized, it is particularly important for Web clients because of the far-reaching nature of the Web. For a good overview of internationalization and localization, see

http://java.sun.com/docs/books/tutorial/i18n/index.html
 

In the simplest internationalized program, strings are read from a resource bundle that contains translations for the language in use. The resource bundle maps keys used by a program to the strings displayed to the user. Thus, instead of creating strings directly in your code, you create a resource bundle that contains translations and read the translations from that bundle using the corresponding key. A resource bundle can be backed by a text file (properties resource bundle) or a class (list resource bundle) containing the mappings.

In the following chapters on Web technology, the Duke's Bookstore example is internationalized and localized into English and Spanish. The key and value pairs are contained in list resource bundles named messages.BookMessage_*.class. To give you an idea of what the key and string pairs in a resource bundle look like, here are a few lines from the file messages.BookMessages.java.

{"TitleCashier", "Cashier"},	
{"TitleBookDescription", "Book Description"},	
{"Visitor", "You are visitor number "},	
{"What", "What We"re Reading"},	
{"Talk", " talks about how Web components can transform the way 
you develop applications for the Web. This is a must read for 
any self respecting Web developer!"},	
{"Start", "Start Shopping"},
 

To get the correct strings for a given user, a Web component retrieves the locale (set by a browser language preference) from the request, opens the resource bundle for that locale, and then saves the bundle as a session attribute (see Associating Attributes with a Session):

ResourceBundle messages = (ResourceBundle)session.	
   getAttribute("messages");	
   if (messages == null) {	
      Locale locale=request.getLocale();	
      messages = ResourceBundle.getBundle("WebMessages",	
         locale); 	
      session.setAttribute("messages", messages);	
   }
 

A Web component retrieves the resource bundle from the session:

ResourceBundle messages =	
   (ResourceBundle)session.getAttribute("messages");
 

and looks up the string associated with the key TitleCashier as follows:

messages.getString("TitleCashier");
 

In addition to Duke's Bookstore, both the Web client and the J2EE application client distributed with this tutorial's case study application, Duke's Bank, are internationalized; see the section Internationalization in Chapter 18.

This has been a very brief introduction to internationalizing Web clients. For more information on this subject, see the J2EE Blueprints:

http://java.sun.com/blueprints
 
Home
TOC
Index
PREV TOP NEXT Search
Feedback