Simple way to use parameterised UI messages in Wicket?

Jonik picture Jonik · Oct 20, 2010 · Viewed 15.1k times · Source

Wicket has a flexible internationalisation system that supports parameterising UI messages in many ways. There are examples e.g. in StringResourceModel javadocs, such as this:

WeatherStation ws = new WeatherStation();
add(new Label("weatherMessage", new StringResourceModel(
    "weather.${currentStatus}", this, new Model<String>(ws)));

But I want something really simple, and couldn't find a good example of that.

Consider this kind of UI message in a .properties file:

msg=Value is {0}

Specifically, I wouldn't want to create a model object (with getters for the values to be replaced; like WeatherStation in the above example) only for this purpose. That's just overkill if I already have the values in local variables, and there is otherwise no need for such object.

Here's a stupid "brute force" way to replace the {0} with the right value:

String value = ... // contains the dynamic value to use
add(new Label("message", getString("msg").replaceAll("\\{0\\}", value)));

Is there a clean, more Wicket-y way to do this (that isn't awfully much longer than the above)?

Answer

svenmeier picture svenmeier · Apr 21, 2012

Take a look at Example 4 in the StringResourceModel javadoc - you can pass a null model and explicit parameters:

add(new Label("message",
         new StringResourceModel(
             "msg", this, null, value)));

msg=Value is {0}