You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Marcel Brückner <br...@amplify-media.de> on 2008/07/10 11:15:10 UTC

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

Re: retrieving resource bundles in backing bean

Posted by Richard Yee <ri...@gmail.com>.
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 <gu...@gmail.com> wrote:
> Hi,
>
> I also had the same problem. Using <s:loadBundle> from the myfaces sandbox
> solved this issue.
>
>
>
> Guy
>
>
>
> From: Marcel Brückner [mailto:brueckner@amplify-media.de]
> Sent: Thursday, July 10, 2008 12:15 PM
> To: users@myfaces.apache.org
> 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

RE: retrieving resource bundles in backing bean

Posted by Guy Bashan <gu...@gmail.com>.
Hi,

I also had the same problem. Using <s:loadBundle> from the myfaces sandbox
solved this issue.

 

Guy

 

From: Marcel Brückner [mailto:brueckner@amplify-media.de] 
Sent: Thursday, July 10, 2008 12:15 PM
To: users@myfaces.apache.org
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