Thanks a lot, James.
I attach a simple class that makes it a little easier to work with
such messages.
On 13.06.2008, at 15:38, James Carman wrote:
The StringResourceModel uses MessageFormat patterns, so you could try
using a ChoiceFormat-based pattern. Try this out in a main method
somewhere to get an idea of what happens:
final String pattern = "{0,choice,0#none|1#one|2#couple|2<many}";
System.out.println(MessageFormat.format(pattern, 0));
System.out.println(MessageFormat.format(pattern, 1));
System.out.println(MessageFormat.format(pattern, 2));
System.out.println(MessageFormat.format(pattern, 100));
On Fri, Jun 13, 2008 at 9:22 AM, Kaspar Fischer
<[EMAIL PROTECTED]> wrote:
I frequently need to deal with singular and plural versions of
wicket:message's.
I would like to do something like:
add(new Label("links", new PluralStringResourceModel("link", this)
{
@Override
public boolean isPlural()
{
return /* some code like: */
model.getObject().getChildren().size() > 1;
}
}));
and this will use "link.plural" if isPlural() returns true and "link"
otherwise.
Has anybody found an elegant solution for such situations?
P.S. Here is a working implementation (neither efficient nor
elegant) for
the above approach:
add(new Label("link", new StringResourceModel("link${plural}",
this, new Model<Serializable>()
{
@Override
public Serializable getObject()
{
return new Serializable()
{
public String getPlural()
{
return model.getObject().getChildren().size() > 1 ?
".plural"
: "";
}
};
}
})));
/**
* A simple wrapper around StringResourceModel to facilitate working
with singular, plural, and
* similar messages.
* <p>
* In the simplest case, you have a property
<tt>website=Website{0,choice,0#|1<s}</tt> and
* create a label like this:
* <code>new Label<String>("website", new
StringResourceModelWithCount("website", this) { public int getCount()
{ return size; } })</code>.
*
* @author hbf
*
*/
public abstract class StringResourceModelWithCount extends
StringResourceModel
{
/**
* See [EMAIL PROTECTED] StringResourceModel#StringResourceModel()}.
*
* @param resourceKey
* @param component
* @param model
*/
public StringResourceModelWithCount(String resourceKey, Component<?
> component, IModel<?> model)
{
// Construct
super(resourceKey, component, model, new Object[] { new
CountExecuter() });
// Initialize
((CountExecuter) getParameters()[0]).target = this;
}
/**
* Shorthand for <code>StringResourceModelWithCount(resourceKey,
component, null)</code>.
*
* @param resourceKey
* @param component
* @param model
*/
public StringResourceModelWithCount(String resourceKey, Component<?
> component)
{
this(resourceKey, component, null);
}
/**
* Subclasses should override this to return the number of items.
*
* @return
*/
public abstract int getCount();
/**
* Helper class needed to make the actual call to [EMAIL PROTECTED]
StringResourceModelWithCount#getCount()}.
* (It is needed because in StringResourceModelWithCount's call
<code>super(...)</code> as we
* cannot reference the non-static method getCount() during
construction.)
*/
private static class CountExecuter extends Model<Integer>
{
private StringResourceModelWithCount target;
@Override
public Integer getObject()
{
return target.getCount();
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]