2

What I have is properties file as below.

welcome.properties

admin = Admin
welcomeAdmin = Welcome Admin
editAdmin = Edit Admin

as I have repeated Admin word, I want to use something like below.

admin = Admin
welcomeAdmin = Welcome #{admin}
editAdmin = Edit #{admin}

so that if I change at one place i.e. at admin = Admin, all places it will reflect.

Any idea/ suggestion how to get this done would be appreciated.

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276

1 Answers1

0

As per today, classic ResourceBundle class doesn't support named parameters and their substitution, but rather supports simple placeholders of {0} type in your key-value pairs of your bundles, that would be substituted by using MessageFormat#format.

In this light if you replace Welcome #{admin} by Welcome {0} you'd be able to achieve the functionality you seek by using one of the following:

  1. JSF's <h:outputFormat>:

    <h:outputFormat value="#{msg.welcomeAdmin}">
        <f:param value="#{msg.admin}" />
    </h:outputFormat>
    
  2. JSTL's <fmt:message>:

    <fmt:message key="msg.welcomeAdmin">
        <fmt:param value="#{msg.admin}">
    </fmt:message>
    

You could as well create your own EL function, or tag handler, or UI component, or make use of MessageFormat class methods in your beans, etc. Of course, you also could extend ResourceBundle class to support named parameters and return a message with named parameters already parsed.

Suggested reading:

  1. Do resource bundles in Java support runtime string substitution?;
  2. How to use Parameterized MessageFormat with non-Value attributes of JSF components;
  3. Pass parameters to messages from resource bundle to components other than h:outputFormat.
Community
  • 1
  • 1
skuntsel
  • 11,624
  • 11
  • 44
  • 67