I have a Page, this Page has many, one or no comments.
At one point i’d like to display only the number of comments without iterating through all of them. However due to translation it needs to be distinguished between two Plural Versions of the comment count, so a simple call of $Comment.Count is out. Since the translator component does not seem to have pluralization support whatsoever, i need to do it on the template level.
So far all i got is this:
<% if Comments %>
<% control Comments %>
<span class="comments">
<% if Count == 1 %> ein Kommentar<% else %>$Count Kommentare<% end_if %></span>
<% end_control %>
<% end_if %>
This works but repeats Count-Times. Is there a workaround for this? Im aware of the possibility to create a function in my Page class like
function CommentCount(){
return $this->Comments->Count();
}
but this feels a little bit tedious.
I made a litle Decorator as this kind of thing was annoying me.
It’s perhaps a bit heavyweight for such a small thing, but you could easily extend it to provide what you need.
Basically it adds a method to DataObjectSet so you can do:
Currently if you don’t provide PluralWord it will try to guess a plural using typical english spelling changes.
Code is here:
https://github.com/lingo/silverstripe-bits-and-bobs/blob/master/code/Pluraliser.php
Help and readme are here:
https://github.com/lingo/silverstripe-bits-and-bobs/