Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 transchoice tag

I have added the translator service for my Symfony2 project. I use it both in controllers and in twig templates. It's configured fine and all the {% trans %} tags work as they mean to. But in some cases I need to use the {% transchoice %} tag, and it's not getting the translation. Here is an example from my code.

{% transchoice post['comments']['count'] %}
    {0} Comments| {1} Comment| ]1,Inf] Comments
{% endtranschoice %}

Also have tried writing this in one line.

I get the correct choice for the comment count, but the word itself is not beeng translated. Like the translator is not able to find the corresponding translation. In the messages.de.yml I have

Comment: "Kommentar"
Comments: "Kommentare"

Is it something wrong with my transchoice syntax? Maybe I need to place spaces somewhere, or anything like that?

like image 515
ArVan Avatar asked Feb 28 '12 13:02

ArVan


1 Answers

In your translation file, you should write this:

{0} Comments| {1} Comment| ]1,Inf] Comments: "{0} Kommentare| {1} Kommentar| ]1,Inf] Kommentare"

UPDATE: An xliff example that works for me :

        <trans-unit id="search.results.summary">
            <source>search.results.summary</source>
            <target>{0}Pas d'annotations trouvée pour "%search_text%"|{1}Une annotation trouvée pour "%search_text%"|]1,Inf]%search_count% annotations trouvées pour "%search_text%"</target>
        </trans-unit>

How I use it:

<h2>{{ 'search.results.summary' | transchoice(search_count, {
'%search_text%': search_text,
'%search_count%': search_count}) }}</h2>

As you see, I don't use the complicated notation as the source for my translation, since it is pretty useless and would make the template less readable. Instead, I put a dot-separated string representing the semantical value of my string.

In your case, I guess the correct thing to use would be something like this:

comment.summary: "{0} Kommentare|{1} Kommentar|]1,Inf] Kommentare"

and

{% transchoice post['comments']['count'] %}
    'comment.summary'
{% endtranschoice %}

Good luck.

like image 100
greg0ire Avatar answered Oct 23 '22 22:10

greg0ire