Selecting Localized Text

The next JSF action in Example 11-1 is the <f:loadBundle> action. This action loads the resource bundle specified by the basename attribute that corresponds to the locale identified by the locale property value in UIViewRoot. A resource bundle holds localized resources (such as text, images, and sounds) and is represented by an instance of the java.util.ResourceBundle class. This class is actually the abstract superclass for two subclasses that do the real work, ListResourceBundle and PropertyResourceBundle, but it provides methods that let you get an appropriate subclass instance, hiding the details about which subclass actually provides the resources. Details about the difference between these two subclasses are beyond the scope of this book. Suffice it to say that the <f:loadBundle> action can use resources provided through either one of them, or even custom subclasses.

For most web applications, an instance of the PropertyResourceBundle is used. A PropertyResourceBundle instance is associated with a named set of localized text resources. Keys identifying resources and their corresponding text values are stored in a regular text file, known as a resource bundle file:

prefLangHeader=Language
langSelectLabel=Language:
enChoice=English
svChoice=Swedish

This is the same kind of file we used in Chapter 7 when we defined error messages. This example shows four keys: prefLangHeader, langSelectLabel, enChoice, and svChoice. The key is a string, without space ...

Get JavaServer Faces now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.