Home TOC |
![]() ![]() ![]() |
Internationalizing and Localizing Web Applications
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. 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.htmlThere are two approaches to internationalizing a Web application:
- Provide a version of the JSP in each of the target locales and have a controller servlet dispatch the request to the appropriate page (depending on the requested locale). This approach is useful if large amounts of data on a page or an entire Web application need to be internationalized.
- Isolate any locale-sensitive data on a page (such as error messages, string literals, or button labels) into resource bundles, and access the data so that the corresponding translated message is fetched automatically and inserted into the page. 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 (page 422)):
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
Home TOC |
![]() ![]() ![]() |