JSF message bundle VS Resource Bundle

By W.ZH Aug 04 2009Message Bundle +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Message Bundle in fact is the way before JSF 1.2.

As for localization, JSF allows you to specify a resource bundle in your faces-config.xml file. This will allow JSF to load the appropriate message bundles defined by the users browser. You still have to include it with loadBundle if you want to access it via JSP. JSF also allows you to mess with these resource bundles programatically. Here’s an example of the entry in your faces-config.xml file.


This will tell JSF that MessageResources.properties is the en (English) locale and to load this by default but it also tells JSF that you have a MessageResources_de.properties file that supports the German language which would be specified by the browser.

Loading it into your JSP.

    <f:loadBundle basename="com.facesite.resource.MessageResources" var="msg"/>
The MessageResources.properties file is in the standard java.util.Properties format.  If you want to access the msg from the backing bean, use this code:

String bundleName = getFacesContext().getApplication().getMessageBundle();
Locale locale = facesContext.getApplication().getDefaultLocale();
ResourceBundle bundle = ResourceBundle.getBundle (bundleName, locale);
String msg = bundle.getString(“your key here”);

normally there is a function in the JSFUtil.java can do this kind of thing.



Resource Bundle +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

But from JSF 1.2, you have simple way to access the msg. You just need to define the bundle in the resource-bundle tag, then you can directly access from JSF page.  Sth like this:


Of coz in the backing bean,
getFacesContext().getApplication().getMessageBundle() will get null if you only use resource-bundle tag.

And i guess in the backing bean, you can use the
getFacesContext().getApplication().getResourceBundle( xxx,xxx);  to access the msg.

Related link: