Spring & JSF integration: Internationalization and Localization

If you are working on a JSF application that is targeted to multiple languages, you may well be familiar with the <f:loadBundle> tag. Even if your application does not support internationalization using message bundles is still probably a good idea. Under the hood the <f:loadBundle> tag reads messages from a Java java.util.ResourceBundle and, whilst this will work, Spring developers often prefer the org.springframework.context.MessageSource interface.

As an alternative to <f:loadBundle> I have been developing a new <s:messageSource> component that can be used to expose messages from any Spring MessageSource, as well as offering a few other advantages.

The new component is a drop-in replacement for <f:loadBundle>.

<s:messageSource source="#{messageSource}" var="messages"/>
<p>
  <h:outputText value="#{messages.hello}"/>
</p>

The source attribute can be any EL expression that resolves to a MessageSource instance. If the source is not specified the Spring ApplicationContext will be used. The var attribute is the name of the variable that will be used to access the messages.

Unlike standard JSF, the key of the message to load will be built from the ID of the page being rendered. For example, assuming the page above is from the file WEB-INF/pages/messages/simple.xhtml, the key used to load the hello message will be pages.messages.simple.hello. Using these compound keys prevents message key clashes and keeps the page mark-up nice and concise. You can use the prefix attribute to override this behaviour if you need to.

If you make reference to message in your XHTML that you have forgotten to define you will either see a warning message (when in development) or an exception will be thrown (when in production).

As with standard JSF, your messages and include place-holders for use with <h:outputFormat>

pages.message.simple.welcome=Welcome to {1} with {0}
<h:outputFormat value="#{messages.welcome}">
  <f:param value="Spring"/>
  <f:param value="JSF"/>
</h:outputFormat>

The <h:outputFormat> tag is a little bit verbose, so for convenience, Spring messages can be used as Maps. This allows you to reference place-holders in a much more concise way:

<h:outputText value="#{messages.welcome['Spring']['JSF']}"/>

The same syntax allows you to map Java objects to messages. By default objects are mapped by building a message key from class name. For example, the following class:

package org.example;
public class ExampleObject {
}

Can be referenced in JSF:

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

Resolving to the following message:

org.example.ExampleObject=example

For enum objects the message key includes the enum name as well as the class:

package org.example;
public enum ExampleObject {
  ONE, //mapped to message key org.example.ExampleObject.ONE
  TWO  //mapped to message key org.example.ExampleObject.TWO
}

Object messages can also make reference to properties that should form part of the message:

org.example.PersonName=Name is {first} {last}
...

package org.example;
public class PersonName {
  ...
  public String getFirst() {...}
  public String getLast() {...}
}

You can also define your own object message strategies by using a message source that implements the org.springframework.springfaces.message.ObjectMessageSource interface.

If you want to check out any of this code take a look at the org.springframework.springfaces.message and org.springframework.springfaces.message.ui packages from the GitHub Project.

Reference: Integrating Spring & JavaServer Faces : Internationalization and Localization from our JCG partner Phillip Webb at the Phil Webb’s Blog blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


8 × = eight



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close