On 2/21/06, Richard Wallace <[EMAIL PROTECTED]> wrote:
>
> Hello everyone,
>
> I have a simple feature request.  Can we get a method added to the
> AbstractViewController class that gets a message out of the applications
> configured message bundle?  Something as simple as:
>
>     protected String getMessage (String messageKey) {
>         ResourceBundle rb = ResourceBundle.getBundle (getApplication
> ().getMessageBundle (), getFacesContext ().getViewRoot ().getLocale ());
>         return rb.getString (messageKey);
>     }
>
> Something that might also be useful is one that takes an optional
> Object[] as parameters and then uses MessageFormat to format the message:
>
>     protected String getMessage (String messageKey, Object[] params) {
>         String messageBundleName = getApplication ().getMessageBundle ();
>         Locale locale = getFacesContext ().getViewRoot ().getLocale ();
>         ResourceBundle rb = ResourceBundle.getBundle (messageBundleName,
> locale);
>         String msgPattern = rb.getString (messageKey);
>         String message;
>         if (params != null) {
>             message = MessageFormat.format (msgPattern, params);
>         } else {
>             message = msgPattern;
>         }
>         return message;
>     }
>
>
> I've found this to be of tremendous help in simplifying error handling
> code, or when I want to display a success message or something like
> that.  It makes things so much cleaner.


You can do something similar in spirit to this with the existing
org.apache.shale.util.Messages class.  The javadocs describe how to define
an application scope managed bean, configured for a particuar resource
bundle.  Let's say you stored this under managed bean name "messages".  Now,
you can generate formatted messages with it:

    Messages messages = (Messages) getBean("messages");
    Locale locale = context.getViewRoot().getLocale();
    String localizedMessage = messages.getMessage("message.key",
        locale, new Object[] { ... parameters ... });

Is that close enough to what you are after?


What do you think?
>
> Thanks,
> Rich


Craig

Reply via email to