Do resource bundles in Java support runtime string substitution?

benstpierre picture benstpierre · Mar 15, 2010 · Viewed 30.7k times · Source

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

Answer

David Sykes picture David Sykes · Mar 15, 2010

Not without using the MessageFormat class, such as:

String pattern = bundle.getString("example.dynamicresource");
String message = MessageFormat.format(pattern, accountCount);