For example, consider the case where we would like to set one of these three feedback messages. Notice how the phrasing is slightly different depending on the number of items. Furthermore in the latter two examples we would like to embed a hyperlink to a bookmarkable page.
Normally in Wicket you would accomplish this using tedious if/else statements and string concatenation, conditionally visible fragments/containers, or the incredibly arcane syntax of Java's {@link java.text.ChoiceFormat ChoiceFormat} (and note that you'd have tocarefully escape the "<" and ">" if you want to include HTML markup as we need for the hyperlinks in this example). All of these approaches are clumsy, and it would be hard for a non-programmer to localize or edit the strings.
With CountMessageModel there is a much easier way: simply declare three separate localizable strings in the properties file of your page or panel.
friends.zero = There are no friends in your network. friends.one = There is <a href="${href}">one friend</a> in your network. friends.many = There are <a href="${href}">${count} friends</a> in your network.
And your Java would be:
new CountMessageModel("friends", pageOrPanelObject, numFriends) setLink(FriendsPage.class);
CountMessageModel selects the appropriate localized string depending on the value of {@code numFriends}, and subtitutes values for ${href}
and ${count}
automatically.
You can now use this model in a label (also consider {@link fiftyfive.wicket.basic.CountLabel CountLabel} as a shortcut for thiscommon use case), or call {@link #getObject getObject()} to get theinterpolated string value for use in a feedback message. @since 2.0
|
|