O'Reilly logo

JavaServer Faces by Hans Bergsten

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required