Do resource bundles in Java support runtime string substitution?
Asked Answered
U

8

46

Can you do the following with a Java ResourceBundle?

In the properties file...

example.dynamicresource=You currently have {0} accounts.

At runtime...

int accountAcount = 3;
bundle.get("example.dynamicresource",accountCount,param2,...);

To give a result of

"You currently have 3 accounts."

Urquhart answered 15/3, 2010 at 22:56 Comment(0)
G
79

Not without using the MessageFormat class, such as:

String pattern = bundle.getString("example.dynamicresource");
String message = MessageFormat.format(pattern, accountCount);
Gluck answered 15/3, 2010 at 22:58 Comment(1)
While this works, it is not necessarily "Yes" that ResourceBundle can do this. ResourceBundle does not have a mechanism.Planet
C
11

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.

Communize answered 15/3, 2010 at 23:2 Comment(0)
W
6

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).

Whipstall answered 15/3, 2010 at 23:3 Comment(0)
Z
3

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.

Zizith answered 15/3, 2010 at 23:13 Comment(4)
As a note/update: The Struts 1.x web framework has reached its end of life and is no longer officially supported since May 2013.Cooperative
As an addition: Struts 2 provides a better alternative in the form of getText() methods. Those methods are defined in the TextProvider interface, which ActionSupport class implements. There are two getText() methods that support message parameter substitution: one that takes an array and one that takes a list, and both do not have any (reasonable) limitations on the number of parameters.Cooperative
@Cooperative though your answer is valid at this moment, this answer was posted in March 2010, when Struts 1.x was still supported.Zizith
I know, that's why I marked the first comment as an update. And I was not trying to compromise your answer, it's just a note for others who are not aware of those changes. No offense :-)Cooperative
F
1

I don't think you can make this work for Non-English properties file.

My message.properties file has the following line:

info.fomat.log.message.start=Starting to parse log message in {0} format.

And my message_fr_FR.properties file has the following line:

info.fomat.log.message.start=A partir d'analyser le message connecter {0} format.

This code works only for the English one

String.format((String) messages .getString(GlobalConstants.MESSAGE_FORMAT_START), GlobalConstants.STR_JSON));

It does NOT replace the placeholder with the value when my language / locale is French :-(

Even MessageFormat.fomat() is no good

Firenew answered 19/7, 2011 at 17:44 Comment(2)
When using String.format(..) you can use classical C format format specifiers like '%d', '%s'.Polio
MRay: It seems that the single quote in your French text may be the problem according to the answer by @tleveque. What happens When you change your string to info.fomat.log.message.start=A partir d''analyser le message connecter {0} format.Hocuspocus
A
0

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

String.format(bundle.getString("example.dynamicresource"), accountCount);
Applesauce answered 15/3, 2010 at 23:3 Comment(0)
G
0

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

Gwen answered 7/10, 2013 at 18:42 Comment(0)
D
0

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 + "]";
    }
} 
Decided answered 1/4, 2015 at 2:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.