While working on an article module in Symfony2, I have to display how many times something is read. To make the 'sentence' grammatically correct I used the code below.
This works obvious but it botters me that I can't find a shorter, cleaner way.
Is there something like article.getReads|length|displayresult('No results', '%d result', '%d results) available or do I have to make this on my own?
{% if article.getReads|length == 0 %}Be the first one to read this!
{% else %}
{{ article.getReads|length|number_format(0, ',', '.') }}
read{% if article.getReads|length != 1 %}s{% endif %}
{% endif %}
You can use the symfony2 pluralization translate component as described here.
As example you can declare a file like:
#src / Acme / DemoBundle / Resources / translations / messages.en.xliff
<trans-unit id="11">
<source>article.read</source>
<target>{0} No results|{1} one result|[2,Inf] results</target>
</trans-unit>
Ad use it as follow in the twig template:
{{ 'article.read'|transchoice(article.getReads|length) }}
Hope this help
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With