Providing Localized Messages and Labels
Messages and labels should be tailored according to the conventions of a user's language and region. There are two approaches to providing localized messages and labels in a web application:
- Provide a version of the JSP page 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 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.
The Duke's Bookstore applications follow the second approach. Here are a few lines from the default resource bundle
messages
.BookstoreMessages.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"},Establishing the Locale
To get the correct strings for a given user, a web application either retrieves the locale (set by a browser language preference) from the request using the
getLocale
method, or allows the user to explicitly select the locale.The JSTL versions of Duke's Bookstore automatically retrieve the locale from the request and store it in a localization context (see Internationalization Tag Library, page 184). It is also possible for a component to explicitly set the locale via the
fmt:setLocale
tag.The JavaServer Faces version of Duke's Bookstore allows the user to explicitly select the locale. The user selection triggers a method that stores the locale in the
FacesContext
object. The locale is then used in resource bundle selection and is available for localizing dynamic data and messages (see Localizing Dynamic Data, page 391):<h:commandLink id="NAmerica" action="storeFront" actionListener="#{localeBean.chooseLocaleFromLink}"> <h:outputText value="#{bundle.english}" /> </h:commandLink> public void chooseLocaleFromLink(ActionEvent event) { String current = event.getComponent().getId(); FacesContext context = FacesContext.getCurrentInstance(); context.getViewRoot().setLocale((Locale) locales.get(current)); }Setting the Resource Bundle
After the locale is set, the controller of a web application typically retrieves the resource bundle for that locale and saves it as a session attribute (see Associating Objects with a Session, page 86) for use by other components:
messages = ResourceBundle. getBundle("com.sun.bookstore.messages.BookstoreMessages", locale); session.setAttribute("messages", messages);The resource bundle base name for the JSTL versions of Duke's Bookstore is set at deployment time through a context parameter. When a session is initiated, the resource bundle for the user's locale is stored in the localization context. It is also possible to override the resource bundle at runtime for a given scope using the
fmt:setBundle
tag and for a tag body using thefmt:bundle
tag.The JavaServer Faces version of Duke's Bookstore uses two methods for setting the resource bundle. One method is letting the JSP pages set the resource bundle using the
f:loadBundle
tag. This tag loads the correct resource bundle according to the locale stored inFacesContext
.For information on this tag, see Loading a Resource Bundle (page 343).
Another way a JavaServer Faces application sets the resource bundle is by configuring it in the application configuration file. There are two XML elements that you can use to set the resource bundle:
message-bundle
andresource-bundle
.If the error messages are queued onto a component as a result of a converter or validator being registered on the component, then these messages are automatically displayed on the page using the
message
ormessages
tag. These messages must be registered with the application using themessage-bundle
tag:For more information on using this element, see Registering Custom Error Messages (page 459).
Resource bundles containing messages that are explicitly referenced from a JavaServer Faces tag attribute using a value expression must be registered using the
resource-bundle
element of the configuration file:<resource-bundle> <base-name>com.sun.bookstore6.resources.CustomMessages</base-name> <var>customMessages</var> </resource-bundle>For more information on using this element, see Registering Custom Localized Static Text (page 460)
Retrieving Localized Messages
A web component written in the Java programming language retrieves the resource bundle from the session:
Then it looks up the string associated with the key
Talk
as follows:The JSP versions of the Duke's Bookstore application uses the
fmt:message
tag to provide localized strings for messages, HTML link text, button labels, and error messages:For information on the JSTL messaging tags, see Messaging Tags (page 185).
The JavaServer Faces version of Duke's Bookstore retrieves messages using either the
message
ormessages
tag, or by referencing the message from a tag attribute using a value expression.You can only use a
message
ormessages
tag to display messages that are queued onto a component as a result of a converter or validator being registered on the component. The following example shows amessage
tag that displays the error message queued on theuserNo
input component if the validator registered on the component fails to validate the value the user enters into the component.<h:inputText id="userNo" value="#{UserNumberBean.userNumber}"> <f:validateLongRange minimum="0" maximum="10" /> ... <h:message style="color: red; text-decoration: overline" id="errors1" for="userNo"/>For more information on using the
message
ormessages
tags, see The UIMessage and UIMessages Components (page 337).Messages that are not queued on a component and are therefore not loaded automatically are referenced using a value expression. You can reference a localized message from almost any JavaServer Faces tag attribute.
The value expression that references a message has the same notation whether you loaded the resource bundle with the
loadBundle
tag or registered it with theresource-bundle
element in the configuration file.The value expression notation is "
var.message
", in whichvar
matches thevar
attribute of theloadBundle
tag or the var element defined in theresource-bundle
element of the configuration file, andmessage
matches the key of the message contained in the resource bundle, referred to by thevar
attribute.Here is an example from
bookstore.jsp
:Notice that
bundle
matches thevar
attribute from theloadBundle
tag and thatTalk
matches the key in the resource bundle.For information on using localized messages in JavaServer Faces, see Using Localized Data (page 343).