Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate concatenated string in twig template using Symfony2 translator

I have a translation yml file like this:

tag:
  myfirsttag: Tag number one
  secondtag: Tag number two
  ....

and twig template like

    <select name="tag" required="required">
        {% for tag in tag_list %}
            <option value="{{ tag }}">{{ "tag." ~ tag | trans(domain='mydomain') }}</option>
        {% endfor %}
    </select>

So here is the problem. Items in select are rendered like "tag.myfirsttag", not translated. If I replace "tag." ~ tag with hardcoded string like "tag.myfirsttag" it works well. So obviously it is related to concatenation but official docs doesn't say anything about it.

To be more clear and simple

I can translate

{{ "hello.world" | trans(domain='mydomain') }}

but can't translate

{{ "hello." ~ "world" | trans(domain='mydomain') }}
like image 966
Andrew Avatar asked Apr 10 '14 09:04

Andrew


1 Answers

The solution is to put the string into parentheses as described here:

works:

{{ 'hello.world' | trans }}

doesn't work:

{{ 'hello.' ~ 'world' | trans }}

works:

{{ ('hello.' ~ 'world') | trans }}
like image 200
Nicolai Fröhlich Avatar answered Sep 24 '22 15:09

Nicolai Fröhlich