Search code examples
jsfinternationalizationresourcebundle

Why f:param value is not set into message from resource bundle?


I work with internationalization in my webapp and I want reuse some messages again.
faces-config.xml

<locale-config>
        <default-locale>en</default-locale>
        <supported-locale>uk</supported-locale>
    </locale-config>
    <resource-bundle>
        <base-name>international.message-labels</base-name>
        <var>msgLabel</var>
    </resource-bundle>

Message from resource bundle

inputLength=Length should be {0} or more

Example:

<p:inputText id="firstName" value="#{user.firstName}"
                placeholder="#{msgLabel.namePlaceHolder}" size="25" required="true"
                styleClass="logRegLabel" validatorMessage="#{msgLabel.inputLength}"
                requiredMessage="#{msgLabel.fieldCannotEmpty}">
                <f:param value="3" />
                <f:validateLength minimum="3" />
                <p:ajax event="blur" update="firstNameMsg" global="false" />
            </p:inputText>  

In all examples that I saw it works. But in my case I always got Length should be {0} or more instead of Length should be 3 or more
I tried this validatorMessage="#{msgLabel['inputLength']}" too
Also, I tried to delete all another parameters of p:inputText but it did not help


Solution

  • The <f:param> to parameterize bundle messages works inside <h:outputFormat> only.

    <h:outputFormat value="#{bundle.key}">
        <f:param value="value for {0}" />
        <f:param value="value for {1}" />
        <f:param value="value for {2}" />
    </h:outputFormat>
    

    JSF doesn't offer any facility to parameterize an arbitrary attribute value. Your best bet would be creating a custom EL function which does the job like below:

    <p:inputText ... validatorMessage="#{my:format1(msgLabel.inputLength, '3')}" />
    

    If you happen to use JSF utility library OmniFaces, then you can use its of:format1() for that.

    <p:inputText ... validatorMessage="#{of:format1(msgLabel.inputLength, '3')}" />
    

    Or its <o:outputFormat> which supports capturing the output in an EL variable.

    <o:outputFormat value="#{msgLabel.inputLength}" var="validatorMessage">
        <f:param value="3" />
    </o:outputFormat>
    <p:inputText ... validatorMessage="#{validatorMessage}" />