Craig McClanahan wrote:
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?
I didn't realize there was a component that already wrapped the message
formatting and all, that is pretty cool. But it's not exactly what I
was looking for. I'm looking for something dead simple with minimal
extra lines of code. I'm always forgetting what params I need to create
the message bundle and where to look them up from. I always find myself
just copying the same bits of code from one place to another. So I just
want something that would give me a one-liner that I have to remember to
get the message I need.
Before I started using Shale and the AbstractViewController I had an
abstract class that all my backing beans extended that simply provided
these kinds of ease of use methods. The only one that I've found
missing in the AbstractViewController is this getMessage() method. If
nothing else I'll just change by abstract class to extend the
AbstractViewController and just add the getMessage() methods, but I
thought others might find it useful as well so thought I'd bring it up.
What do you think?
Thanks,
Rich
Craig
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]