Using Localized Messages
All data and messages in the Duke's Bookstore application have been localized for Spanish, French, German, and American English. Performing Localization explains how to produce the localized messages as well as how to localize dynamic data and messages.
The image map on the first page allows you to select your preferred locale. See Chapter 20 for information on how the image map custom component was created.
This section explains how to use localized static data and messages for JavaServer Faces applications. If you are not familiar with the basics of localizing web applications, see Chapter 22. Localized static data can be included in a page by using the
loadBundletag, defined injsf_core.tld. Follow these steps:A
ResourceBundlecontains a set of localized messages. For more information about resource bundles, seeAfter the application developer has produced a
ResourceBundle, the application architect puts it in the same directory as the application classes. Much of the data for the Duke's Bookstore application is stored in aResourceBundlecalledBookstoreMessages.Referencing a ResourceBundle from a Page
For a page with JavaServer Faces tags to use the localized messages contained in a
ResourceBundle, the page must reference theResourceBundleusing aloadBundletag.The
loadBundletag frombookstore.jspisThe
basenameattribute value refers to theResourceBundle, located in themessagespackage of thebookstoreapplication. Make sure that thebasenameattribute specifies the fully qualified class name of the file.The
varattribute is an alias to theResourceBundle. This alias can be used by other tags in the page in order to access the localized messages.Referencing a Localized Message
To reference a localized message from a
ResourceBundle, you use a value-binding expression from an attribute of the component tag that will display the localized data. You can reference the message from any component tag attribute that is value-binding-enabled.The value-binding expression has the notation "
var.message", in whichvarmatches thevarattribute of theloadBundletag, andmessagematches the key of the message contained in theResourceBundlereferred to by thevarattribute. Here is an example frombookstore.jsp:Notice that
bundlematches thevarattribute from theloadBundletag and thatTalkmatches the key in theResourceBundle.Another example is the
graphicImagetag fromchooselocale.jsp:<h:graphicImage id="mapImage" url="/template/world.jpg" alt="#{bundle.ChooseLocale}" usemap="#worldMap" />The
altattribute is value-binding-enabled, and this means that it can use value-binding expressions. In this case, thealtattribute refers to localized text, which will be included in the alternative text of the image rendered by this tag.See Creating the Component Tag Handler and Enabling Value-Binding of Component Properties for information on how to enable value binding on your custom component's attributes.