Use placeholders in translations using tags

In Symfony / Twig, I can use tags using percentages in my translated block. For example:

Hello {{nickname}} 

will become

 {% trans %}Hello %nickname%{% endtrans %} 

This works as expected. The placeholder array that I pass to Twig is automatically mapped to% placeHolder%. No extra work. So this works with my PHP array from the controller:

 Array('nickname' => 'rolandow') 

When I want to use an alias inside a translation block, all I need to do is surround it with% percent. Unfortunately, this does not work when I pass it trans .

Now I would like to translate an entire block of text using tags. I cannot figure out how to use tags in my translation. So, my twig looked something like this:

 {{ say.hello|trans }} 

And my translation fragment

 <trans-unit id="1"> <source>say.hello</source> <target>Hello %nickName%, how are you doing today? lots-of-text-here</target> </trans-unit> 

It works for me, using it in my template, but it seems to me that to do it twice. Now I need to put the placeholder array again in the trans function. For example:

 {{ say.hello|trans('%nickName%' : nickName) }} 

If I want to use other tags that are passed to the branch in my controller, I need to pass them to the translator as well. Can I somehow pass the full array?

+7
source share
2 answers

There are a few questions here, so let's cover them.

1) Twig's behavior is not like a Doctrine request, where each parameter should be limited. You can pass an array containing unused parameters to trans , so if you do not want to specify {'key': 'value', 'key2': 'value2'...} for the filter, just pass the entire array (example: | trans(array) ). This is @Luke.

2) You can translate a block of texts in several ways, the simplest is {% set %} . The {% set %} tag can be used in two ways:

  • {% set var = expression %} or {% set var1, var2 = expression1, expression2 %} is the most famous and used way: you simply add some value to one or more variables.

  • {% set var %} block of text {% endset %} allows you to set the entire block of text inside this variable. This is useful if you want to put this block in a filter (for example, escape or, in your case, trans ).

So, to translate a block of text, you will do something like:

 {% set variable %} block to translate %placeholder% {% endset %} {{ variable | trans(array) }} 

In any case, I don’t see interest in translating the whole block at a time: we usually use | trans | trans after the property (e.g. say.hello ), and I cannot imagine your xlf / yml translation file with such a construction. If you want to use the translator only to fill in placeholders, just use Twig as written for this work :-)

3) About replacing placeholder with %placeholder% in your array parameters: Twig point: put what you want as a placeholder. That way, if your translated sentence contains a few % , you can use $something$ , #something# or even something as a placeholder.

If your array keys do not contain those % , you need to add them, you have no choice. If you really want to do this in a Twig file, you can create a macro that will do the job for you and put it in a file that you import in your base layout.

Something like:

 {% macro trans_pct(property, params) %} {% set newParams = [] } {% if params %} {% for key, value in params %} {% set newParams['%' ~ key ~ '%'] = value %} {% endfor %} {% endif %} {{ property | trans(newParams) }} {% endmacro %} 

And then use it with {{ _self.trans_pct('hello.say', array) | trim }} {{ _self.trans_pct('hello.say', array) | trim }} .

Notes:

  • _self is the template in which the macro is stored (see details).

    / li>
  • trim used because I wrote a macro with indentation and line breaks (this one is clean for reading). These spaces are printed by default.

+1
source

{{say.hello | trans ('% nickname%': 'rolandow')}}

+1
source

All Articles