JSF 2 Resource/Message Bundle

 

Prepare Eclipse Project

* Follow this post to setup test.cdi test app if needed
* Or you can download the source code for this example from test.cdi.msg.bundle

Create a Resource Bundle File

* Create a new Java package named resources
* Create a message bundle file named mymessage.properties with content:

message1=Hello
message2= world
msg.dotted.one=This is a dotted message.
msg.with.params.one=This is message with parameters: parameter one: "{0}" and parameter two: "{1}"

Use in JSF Pages

* Configure either a message-bundle and/or(?) resource-bundle in WEB-INF/faces-config.xml

  <application>
    ...
    <message-bundle>resources.mymessage</message-bundle>
    <resource-bundle>
		<base-name>resources.mymessage</base-name>
		<var>mymsg</var>
   </resource-bundle>
  </application>

* Use message-bundle in pages.
* Note that you use f:loadbundle to load bundle in individual pages.

<body>
	<p>
	<f:loadBundle basename="resources.mymessage" var="msg"></f:loadBundle>
	From message bundle: #{msg.message1} #{msg.message2}  
	</p>

* Or use resource-bundle in pages.
* Note that you do not need to use f:loadbundle to load bundle in individual pages.

<body>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:tr="http://myfaces.apache.org/trinidad"
	xmlns:f="http://java.sun.com/jsf/core">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
	<b>Greetings from resource bundle:</b> #{mymsg.message1} #{mymsg.message2}  
 
	<br />
	<b>Greetings from resource-bundle with "dotted" message:</b> #{mymsg['msg.dotted.one']}
 
	<br />
	<b>Greetings from resource-bundle with parameters:</b>
	<h:outputFormat value="#{mymsg['msg.with.params.one']}">
		<f:param value="Hello"/>
		<f:param value="World"/>
	</h:outputFormat>
</body>
</html>

* Browser output:

Greetings from resource bundle: Hello world
Greetings from resource-bundle with "dotted" message: This is a dotted message.
Greetings from resource-bundle with parameters:This is message with parameters: parameter one: "Hello" and parameter two: "World"

Use ResourceBundle Directly in Back Beans

* Back bean class:

package hello;
 
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
 
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.inject.Named;
 
@Named
public class GreetingSvc1 {
	ResourceBundle rs;
	String msg1 = "message2";
 
	@PostConstruct
	private void init(){
		FacesContext context = FacesContext.getCurrentInstance();
        Locale locale=context.getViewRoot().getLocale();
        rs=ResourceBundle.getBundle("resources.mymessage",locale);
	}
 
	public String getMessageBundleGreeting(){
		return rs.getString(msg1);
	}
 
	public String getParameterizedGreeting() {
		return MessageFormat.format(rs.getString("msg.with.params.one"),
				"Hello", "World");
	}
}

* JSF page:

	<br />
	<b>Greetings from greetingSvc1:</b> #{greetingSvc1.messageBundleGreeting}
 
	<br />
	<b>Greetings from greetingSvc1:</b> #{greetingSvc1.parameterizedGreeting}

* Browser output:

Greetings from greetingSvc1: world
Greetings from greetingSvc1: This is message with parameters: parameter one: "Hello" and parameter two: "World"

Use Injectable ResourceBundle via CDI Producer

* Create a new MyMessageBundle qualifier class:

package hello;
 
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
 
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
 
import javax.inject.Qualifier;
@Qualifier
@Retention(RUNTIME)
@Target({ TYPE, METHOD, FIELD, PARAMETER })
public @interface MyMessageBundle {
 
}

* Create a new MyMessageBundleProvider producer class:

package hello;
 
import java.util.Locale;
import java.util.ResourceBundle;
 
import javax.enterprise.inject.Produces;
import javax.faces.context.FacesContext;
 
public class MyMessageBundleProvider {
 
	private ResourceBundle bundle;
 
	@Produces
	@MyMessageBundle
	public ResourceBundle getBundle() {
		if (this.bundle == null) {
			FacesContext context = FacesContext.getCurrentInstance();
			Locale locale = context.getViewRoot().getLocale();
			bundle = ResourceBundle.getBundle("resources.mymessage", locale);
		}
		return this.bundle;
	}
}

* Create a new GreetingSvc2 back bean class and inject ResourceBundle :

package hello;
 
import java.text.MessageFormat;
import java.util.ResourceBundle;
 
import javax.inject.Inject;
import javax.inject.Named;
 
@Named
public class GreetingSvc2 {
	String msg1 = "message1";
 
	@Inject
	@MyMessageBundle
	private ResourceBundle rs;
 
	public String getMessageBundleGreeting() {
		return rs.getString(msg1);
	}
 
	public String getParameterizedGreeting() {
		return MessageFormat.format(rs.getString("msg.with.params.one"),
				"Hello", "World");
	}
}

* JSF page:

	<br />
	<b>Greetings from greetingSvc2:</b> #{greetingSvc2.messageBundleGreeting}
 
	<br />
	<b>Greetings from greetingSvc2 parameterized:</b> #{greetingSvc2.parameterizedGreeting}

* Browser output:

Greetings from greetingSvc2: Hello
Greetings from greetingSvc2 parameterized: This is message with parameters: parameter one: "Hello" and parameter two: "World"

References

* JSF 2.0 and Resource Bundles example
* JSF2 and Internationalization (I18N)

This entry was posted in jsf. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

This site uses Akismet to reduce spam. Learn how your comment data is processed.