Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to use parameterised UI messages in Wicket?

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

like image 238
Jonik Avatar asked Oct 20 '10 10:10

Jonik


3 Answers

In case you have a Model in your Component which holds an object with values you want to access from your placeholders as substitutions, you can write:

new StringResourceModel("salutation.text", getModel());

Let's imagine getModel()'s return type is IModel<User> and User contains fields like firstName and lastName. In this case you can easily access firstName and lastName fields inside your property string:

salutation.text=Hej ${firstName} ${lastName}, have a nice day!

Further information you can find here: https://ci.apache.org/projects/wicket/apidocs/8.x/org/apache/wicket/model/StringResourceModel.html#StringResourceModel-java.lang.String-org.apache.wicket.model.IModel-

like image 117
Philipp Wirth Avatar answered Sep 24 '22 14:09

Philipp Wirth


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}
like image 12
svenmeier Avatar answered Nov 13 '22 00:11

svenmeier


I think the most consistent WICKETY way could be accomplished by improving Jonik's answer with MessageFormat:

.properties:

msg=Saving record {0} with value {1}

.java:

add(new Label("label", MessageFormat.format(getString("msg"),obj1,obj2)));
//or
info(MessageFormat.format(getString("msg"),obj1,obj2));

Why I like it:

  • Clean, simple solution
  • Uses plain Java and nothing else
  • You can replace as many values as you want
  • Work with labels, info(), validation, etc.
  • It's not completely wickety but it is consistent with wicket so you may reuse these properties with StringResourceModel.

Notes:

if you want to use Models you simply need to create a simple model that override toString function of the model like this:

abstract class MyModel extends AbstractReadOnlyModel{
    @Override
    public String toString()
    {
        if(getObject()==null)return "";
        return getObject().toString();
    }
}

and pass it as MessageFormat argument.

I don't know why Wicket does not support Model in feedback message. but if it was supported there was no reason to use these solutions and you could use StringResourceModel everywhere.

like image 5
Hossein Nasr Avatar answered Nov 13 '22 01:11

Hossein Nasr