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

前端 未结 3 2000
北荒
北荒 2020-12-16 18:10

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

For instance, this is legal:

相关标签:
3条回答
  • 2020-12-16 18:46

    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)}"/>
    
    0 讨论(0)
  • 2020-12-16 18:52

    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.

    0 讨论(0)
  • 2020-12-16 19:08

    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.

    0 讨论(0)
提交回复
热议问题