Chapter 13. Internationalization and themes
Seam makes it easy to build internationalized applications by providing several built-in components for handling multi-language UI messages.
Each user login session has an associated instance of java.util.Locale
(available to the application as a session-scoped component named locale
). Under normal circumstances, you won't need to do any special configuration to set the locale. Seam just delegates to JSF to determine the active locale:
-
If there is a locale associated with the HTTP request (the browser locale), and that locale is in the list of supported locales from faces-config.xml
, use that locale for the rest of the session.
-
Otherwise, if a default locale was specified in the faces-config.xml
, use that locale for the rest of the session.
-
Otherwise, use the default locale of the server.
It is
possible
to set the locale manually via the Seam configuration properties org.jboss.seam.core.localeSelector.language
, org.jboss.seam.core.localeSelector.country
and org.jboss.seam.core.localeSelector.variant
, but we can't think of any good reason to ever do this.
It is, however, useful to allow the user to set the locale manually via the application user interface. Seam provides built-in functionality for overriding the locale determined by the algorithm above. All you have to do is add the following fragment to a form in your JSP or Facelets page:
<h:selectOneMenu value="#{localeSelector.language}">
<f:selectItem itemLabel="English" itemValue="en"/>
<f:selectItem itemLabel="Deutsch" itemValue="de"/>
<f:selectItem itemLabel="Francais" itemValue="fr"/>
</h:selectOneMenu>
<h:commandButton action="#{localeSelector.select}" value="#{messages['ChangeLanguage']}"/>
Or, if you want a list of all supported locales from faces-config.xml
, just use:
<h:selectOneMenu value="#{localeSelector.localeString}">
<f:selectItems value="#{localeSelector.supportedLocales}"/>
</h:selectOneMenu>
<h:commandButton action="#{localeSelector.select}" value="#{messages['ChangeLanguage']}"/>
When this use selects an item from the drop-down, and clicks the button, the Seam and JSF locales will be overridden for the rest of the session.