SeamFramework.orgCommunity Documentation

Chapter 17. Locales

17.1. Default Locale
17.2. User Locale
17.3. Available Locales

In a similar fashion to TimeZones we have an application Locale retrieved by

@Inject

java.util.Locale lc;

accessible via EL with "defaultLocale".

By default the Locale will be set to the JVM default, unless you override the DefaultLocaleProducer Bean via the Seam Config module. Here are a few examples of XML that can be used to define the various types of Locales that are available.

This will set the default language to be French.


<beans xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:s="urn:java:seam:core" 
   xmlns:lc="urn:java:org.jboss.seam.international.locale"
   xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee 
      http://docs.jboss.org/cdi/beans_1_0.xsd">

    <lc:DefaultLocaleProducer>
        <s:replaces/>
        <lc:defaultLocaleKey>fr</lc:defaultLocaleKey>
    </lc:DefaultLocaleProducer>
</beans>

This will set the default language to be English with the country of US.


<beans xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:s="urn:java:seam:core" 
   xmlns:lc="urn:java:org.jboss.seam.international.locale"
   xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee 
      http://docs.jboss.org/cdi/beans_1_0.xsd">

    <lc:DefaultLocaleProducer>
        <s:replaces/>
        <lc:defaultLocaleKey>en_US</lc:defaultLocaleKey>
    </lc:DefaultLocaleProducer>
</beans>

As you can see from the previous examples, you can define the Locale with lang_country_variant. It's important to note that the first two parts of the locale definition are not expected to be greater than 2 characters otherwise an error will be produced and it will default to the JVM Locale.

The Locale associated with the User Session can be retrieved by

@Inject

@UserLocale
java.util.Locale locale;

which is EL accessible via userLocale.

By default the Locale will be the same as that of the application when the User Session is initially created. However, changing the User's Locale is a simple matter of firing an event to update it. An example would be

@Inject

@Changed
Event<java.util.Locale> localeEvent;
 
public void setUserLocale()
{
     Locale canada = Locale.CANADA;
     localeEvent.fire(canada);
}

We've also provided a list of available Locales that can be accessed via

@Inject

List<java.util.Locale> locales;

The locales that will be returned with this can be defined with XML configuration of the AvailableLocales Bean such as


<beans xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:s="urn:java:seam:core" 
   xmlns:lc="urn:java:org.jboss.seam.international.locale"
   xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee 
      http://docs.jboss.org/cdi/beans_1_0.xsd">

    <lc:AvailableLocales>
        <s:modifies/>
        <lc:supportedLocaleKeys>
            <s:value>en</s:value>
            <s:value>fr</s:value>
        </lc:supportedLocaleKeys>
    </lc:AvailableLocales>
</beans>