简体   繁体   中英

Twig translation concatenate

I want to translate a variable that contains a string plus a variable like the next one:

  {{ 'string' ~ variable|trans }}

It seems that it's only taking the variable to translate without the string part.

I just discovered it that I can fix this problem creating a previous variable that contains the concatenation from the string and the previous variable like the next example:

   {% set X = 'string' ~ variable %} 

And now we are able to translate what we were trying to translate just translating the X variable:

   {{ X|trans }}

I tried to translate this concatenation like this:

   {% trans %} {{'string' ~ variable }} {% endtrans %}

But also was not a succesfull result for me.

Is any other way to avoid creating this "x" varible to be able to translate all the concatenation straight?

Try this way:

{{ ('string'~variable)|trans }}

Positioning of the braces is important here otherwise filters always will be handled before the concatenation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM