0

I have a filter bar that let me search some results within my database.

I have a flash message that shows how many results are found when I enter a word inside it.

this is my controller

 if ($filter != '') {
        $this->get('session')->getFlashBag()->add('info', 'worked!');
      }

and this is in my template

{% for message in app.session.flashbag.get('info') %}
    {{ message }}
{% endfor %}

so when i research things actually, whether I have 1 result or more it doesn't change my sentence.

enter image description here

enter image description here

résultats is still written with an s as it is hard coded. How can I create something that will allow me to pluralize and singularize a word in my template? Should I go directly in the controller for that ?

EDIT

I used this method directly in the template but I don't think it is a "good practice" one. Any help for making that better?

{{ results.getTotalItemCount }}
{% if results.getTotalItemCount <= 1 %}
   {{ 'this.is.your.result'|trans({ '%count%': results.getTotalItemCount}) }}
{% else %}
   {{ 'this.is.your.results'|trans({ '%count%': results.getTotalItemCount}) }}
{% endif %}

in translation

this:
    is:
       your.result:    "résultat"
       your.results:   "résultats"
chicken burger
  • 774
  • 2
  • 12
  • 32

1 Answers1

3

You should maybe check pluralization in translations here, you can use transChoice() method, it is explained here how to use it.

Here you can see how to use it in twig:

https://stackoverflow.com/a/10817495/5258172

Edit: your answer in question can be done like this:

this:
    is:
       your.result: "1 résultat|%count% résultats"

and then in your twig:

{{ 'this.is.your.result'|transchoice(results.getTotalItemCount) }}
kunicmarko20
  • 2,095
  • 2
  • 15
  • 25
  • Yeah I was looking at that already, but as a new developer I find it tedious to understand where to place things. But thanks for the infos. – chicken burger Sep 26 '17 at 07:26
  • @chickenburger I edited the answer and provided a link to other SO answer where you can see how to use it and where to place things. – kunicmarko20 Sep 26 '17 at 07:28
  • i succeeded in a different way, see my question. – chicken burger Sep 26 '17 at 08:12
  • @chickenburger I edited my answer with code, how it can be done with `transChoice() ` method. – kunicmarko20 Sep 26 '17 at 08:24
  • hmm there is an error when I apply your code :/ `An exception has been thrown during the rendering of a template ("Unable to choose a translation for "{1}1 résultat|]1,Inf]%count% résultats" with locale "fr". Double check that this translation has the correct plural options (e.g. "There is one apple|There are %count% apples").")` – chicken burger Sep 26 '17 at 08:28
  • @chickenburger try to add a space to `{1} 1` – kunicmarko20 Sep 26 '17 at 08:31
  • @chickenburger what symfony version are you using? – kunicmarko20 Sep 26 '17 at 08:33
  • Maybe I guess it is because I'm working on a symfony 2.3 project and it doesn't understand transchoice i guess. But your solution might be working well on 2.8 and higher so i'm validating it as it works by tweaking a little bit. Thank you ! – chicken burger Sep 26 '17 at 08:34
  • @chickenburger transchoice should work in 2.3 also, I guess it worked? – kunicmarko20 Sep 26 '17 at 08:38