views:

61

answers:

1

Wicket has a flexible internationalisation system that also 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:

cur=Current value is {0}

Specifically, I don't want to have to create a model object (with getters for the values to be replaced; like WeatherStation in the above example) just for this purpose. That's just overkill if I already have the value(s) in local variable(s), 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 = ... // this contains the dynamic value to use
String text = getLocalizer().getString("cur", this).replaceAll("\\{0\\}", value);
add(new Label("message", text));

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

+2  A: 

There's a way, which although still involves creating a model, doesn't requires a bean with a getter.

given this message in a properties file:

msg=${} persons

Here's how to replace the placeholder with a value, be it a local variable, a field or a literal:

add(new Label("label", new StringResourceModel("msg", new Model<Serializable>(5))));
Jawher
Thanks! Btw, do you know of any name for this `${}` notation, and is it documented somewhere? Would it be possible to replace more than one placeholder (something like `${0}` and `${1}`?) in a message using this approach?
Jonik