Do resource bundles in Java support runtime string substitution?

前端 未结 8 663
夕颜
夕颜 2020-12-25 10:32

Can you do the following with a Java ResourceBundle?

In the properties file...

example.dynamicresource=You currently have {0} accounts.
相关标签:
8条回答
  • 2020-12-25 11:00

    There are various ways, depending on the view technology you're using. If you're using "plain vanilla" Java (e.g. Swing), then use MessageFormat API as answered before. If you're using a webapplication framework (which is true, if I judge your question history here correctly), then the way depends on the view technology and/or MVC framework you're using. If it is for example "plain vanilla" JSP, then you can use JSTL fmt:message for this.

    <fmt:message key="example.dynamicresource">
        <fmt:param value="${bean.accountCount}">
    </fmt:message>
    

    If it is for example JSF, you can use h:outputFormat for this.

    <h:outputFormat value="#{bundle['example.dynamicresource']}">
        <f:param value="#{bean.accountCount}">
    </h:outputFormat>
    

    Best place is to just consult the documentation of the technology/framework you're using (or to tell it here so that we can give better suited and more detailed answers).

    0 讨论(0)
  • 2020-12-25 11:08

    MessageFormoat#format will work for the case like:

    greetingTo=Have Param, saying hello {0}
    

    You can declare two methods like this where RB is a instance of ResourceBundle:

    /**This is a method that takes the param to substitute the placeholder**/
    public String getString(String key, Object... params  ) {
        try {
            return MessageFormat.format(this.RB.getString(key), params);
        } catch (MissingResourceException e) {
            return "[" + key + "]";
        }
    }
    
    /**Without a param, this will derectly delegate to ResourceBundle#getString**/
    public String getString(String key) {
        try {
            return this.RB.getString(key);
        } catch (MissingResourceException e) {
            return "[" + key + "]";
        }
    } 
    
    0 讨论(0)
  • 2020-12-25 11:14

    Remember that when using MessageFormat.format() you need to use a double quote ('') in your resource bundle if you want to express single quote (').

    0 讨论(0)
  • 2020-12-25 11:16

    I don't believe ResourceBundle can do that itself, but String can:

    String.format(bundle.getString("example.dynamicresource"), accountCount);
    
    0 讨论(0)
  • 2020-12-25 11:17

    On their own, ResourceBundle does not support property placeholders. The usual idea is to take the String you get from the bundle, and stick it into a MessageFormat, and then use that to get your parameterized message.

    If you're using JSP/JSTL, then you can combine <fmt:message> and <fmt:param> to do this, which uses ResourceBundle and MessageFormat under the covers.

    If you happen to be using Spring, then it has the ResourceBundleMessageSource which does something similar, and can be used anywhere in your program. This MessageSource abstraction (combined with MessageSourceAccessor) is much nicer to use than ResourceBundle.

    0 讨论(0)
  • 2020-12-25 11:18

    Struts have a nice util called MessageResources which does exactly what you ask for....

    e.g.

    MessageResources resources = getResources(request, "my_resource_bundle"); // Call your bundle exactly like ResourceBundle.getBundle() method
    resources.getMessage("example.dynamicresource",accountCount,param2,...);
    

    Limitation It only allows maximum of 3 parameters (i.e. resource attribute, param1, ..., param3).

    I suggest using MessageFormat (if you want to use more than 3 parameter values) as suggested by David Sykes.

    PS the getResources method is available only in the Struts Action class.

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