May 19, 2011, 8:47 a.m.
posted by reo
Internationalizing and Localizing Web ApplicationsInternationalization 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. While all client user interfaces should be internationalized and localized, it is particularly important for Web applications 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 There are two approaches to internationalizing a Web application:
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 (page 394)):
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");
This has been a very brief introduction to internationalizing Web applications. For more information on this subject see the Java BluePrints: http://java.sun.com/blueprints |
- Comment