Pass parameters to messages from resource bundle to components other than **h:outputFormat**
Asked Answered
E

3

11

Is there a convenient way to pass parameters to messages from resource bundle to components other than h:outputFormat?

For instance, this is legal:

<h:outputFormat value="#{myBundle['parametricMessage']}">
    <f:param value="#{myBundle['someParameterValue']}"/>
</h:outputFormat>

But I need it for a button, like this (which won't work):

<h:commandButton value="#{myBundle['parametricMessage']}">
    <f:param value="#{myBundle['someParameterValue']}"/>
</h:commandButton>

Of course, I can use link instead of button, and I can make it through a property in a managed bean, but in this question I'm seeking for a convenient way to use the button...

I'm using RichFaces 3.3.3, JSF2, facelets.

Encampment answered 17/4, 2011 at 23:29 Comment(0)
M
14

How about this approach ?

EL expression allow you to define a function .You first define a EL expression 's function , which accepts a resource bundle , its message key and placeholder 's parameter and output the resolved message .

public static String geti18nMsg(ResourceBundle bundle ,String msgKey, String paramValue ) {
    String  msgValue = bundle.getString(msgKey);
    MessageFormat   messageFormat = new MessageFormat(msgValue);
    Object[] args = {paramValue};
    return messageFormat.format(args);
}

Then call this function to get the resolved message in the <h:commandButton> :

<h:commandButton value="#{f:geti18nMsg(myBundle , parametricMessage, someParameterValue)}"/>
Messidor answered 20/4, 2011 at 6:35 Comment(1)
This doesn't work when you are using <f:loadBundle> because what you are passing through is a ResouceBundleMap and not a ResouceBundle. If you use <f:loadBundle> you can change the ResouceBundle to Map<String,String> and then you do a .get(msgKey) on it.Treharne
M
4

Try this:

<h:commandButton>
    <h:outputFormat value="#{myBundle['parametricMessage']}">
        <f:param value="#{myBundle['someParameterValue']}"/>
    </h:outputFormat>
</h:commandButton>

Btw, this does what you want and also avoids having to write backing bean code.

Moustache answered 17/8, 2012 at 14:25 Comment(0)
E
0

Well I didn't find good answer on that, so the question will remain open. A good practice I've discovered, is to have a special class that represents each resource bundle (that has parametric stuff), and to transfer all the message formation, and working with context there (like, acquire locale from FacesContext, get a ResourceBundle, apply parameters, etc). And finally to provide access to a singleton of such service class from your ManagedBean.

This requires some additional work to be done, but solves the problem and is worth of the time.

Encampment answered 19/4, 2011 at 23:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.