Chapter 5. Internationalization

Seam makes it easy to build internationalized applications by providing several built-in components for handling multi-language UI messages.

5.1. Locales

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 localeSelector.language, localeSelector.country and 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.

5.2. Labels

JSF supports internationalization of user interface labels and descriptive text via the use of <f:loadBundle />. You can use this approach in Seam applications. Alternatively, you can take advantage of the Seam messages component to display templated labels with embedded EL expressions.

Each login session has an associated instance of java.util.ResourceBundle (available to the application as a session-scoped component named resourceBundle). You'll need to make your internationalized labels available via this special resource bundle. By default, the resource bundle used by Seam is named messages and so you'll need to define your labels in files named messages.properties, messages_en.properties, messages_en_AU.properties, etc. These files usually belong in the WEB-INF/classes directory.

So, in messages_en.properties:

Hello=Hello

And in messages_en_AU.properties:

Hello=G'day

You can select a different name for the resource bundle by setting the Seam configuration property named resourceBundle.bundleName.

If you define your labels in this special resource bundle, you'll be able to use them without having to type <f:loadBundle ... /> on every page. Instead, you can simply type:

<h:outputText value="#{messages['Hello']}"/>

or:

<h:outputText value="#{messages.Hello}"/>

Even better, the messages themselves may contain EL expressions:

Hello=Hello, #{user.firstName} #{user.lastName}
Hello=G'day, #{user.firstName}

You can even use the messages in your code:

@In(create=true) private Map<String, String> messages;
@In("#{messages['Hello']}") private String helloMessage;

5.3. Faces messages

The facesMessages component is a super-convenient way to display success or failure messages to the user. The functionality we just described also works for faces messages:

@Name("hello")
@Stateless
public class HelloBean implements Hello {
    @In(create=true) 
    FacesMessages facesMessages;
    
    public String sayIt() {
        facesMessages.addFromResourceBundle("Hello");
    }
}

This will display Hello, Gavin King or G'day, Gavin, depending upon the user's locale.