This is how we do it. We have a message utility class with this code in it.
public static String getMessageFromBundle(String bundleName, String key,
Object[] params, Locale locale) {
String message = null;
ResourceBundle bundle =
ResourceBundle.getBundle(bundleName, locale,
getCurrentClassLoader(params));
try {
message = bundle.getString(key);
} catch (MissingResourceException error) {
message =
new StringBuilder().append("key:'").append(key).append("':
").append(NO_RESOURCE_FOUND).append(" with error:
").append(error).toString();
}
if (params != null) {
MessageFormat mf = new MessageFormat(message, locale);
message = mf.format(params, new StringBuffer(), null).toString();
}
return message;
}
We also have a backing bean that is abstract that we extend all our
backing beans from with this method in it:
/**
* A convenience method for retreiving a message from a message bundle
* @param key - The message key.
* @param params - An optional array of parameters to supply to the message
*
* @return A String containing the message retrieved from the message bundle.
*/
public static String getMessage(String key, Object[] params) {
return MessageUtil.getMessageFromBundle(Constants.MESSAGE_RESOURCE_LOCATION,
key, params,
FacesContext.getCurrentInstance().getViewRoot().getLocale());
}
-Richard
On Thu, Jul 10, 2008 at 2:48 AM, Guy Bashan <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I also had the same problem. Using <s:loadBundle> from the myfaces sandbox
> solved this issue.
>
>
>
> Guy
>
>
>
> From: Marcel Brückner [mailto:[EMAIL PROTECTED]
> Sent: Thursday, July 10, 2008 12:15 PM
> To: [email protected]
> Subject: retrieving resource bundles in backing bean
>
>
>
> MyFaces 1.1.5, Tomcat 5.5
>
> I currently use the following way to retrieve resource bundles:
>
> FacesContext cont = FacesContext.getCurrentInstance();
> UIViewRoot viewRoot = cont.getViewRoot();
> Locale loc = viewRoot.getLocale();
> labelsResource = ResourceBundle.getBundle("labels", loc);
>
> This code is part of a CTOR of a request scoped backing bean.
>
> It sometimes happens that the resource bundle returned from "getBundle" is
> "null".
>
> Is there a better way to retrieve the bundle? Is the bundle-loading or
> view-root depending on a certain
> render-phase?
>
> If nobody has an idea I would try to post it as a bug, since it is
> reproducable behaviour.
>
> Marcel