Zwen2012
Zwen2012

Reputation: 3498

TWIG: Translation of string with variables?

In my translation.yml I have this var:

all.in.EN: All In Great Britain

In my Twig I have something like this:

{% trans %}all.in.{{ countryKey }}{% endtrans %}

This doesn't work so I tried to put this in a variable:

{% set allInName = 'all.in.{{ countryKey }}' %}
{% trans %}allInName{% endtrans %}

But then the output is allInName and not the translation. Someone an idea what I'm doing wrong? THNAKS!

Upvotes: 5

Views: 4334

Answers (2)

Nawfal Serrar
Nawfal Serrar

Reputation: 2263

You could try to do it like this :

{{ ('all.in.'~countryKey)|trans }}

already tested this works. the '~' sign is to concatenate, and |trans filter translate

Upvotes: 9

chapay
chapay

Reputation: 1315

Try

{% set allInName = 'all.in.' ~ countryKey %}
{{allInName | trans}}

Upvotes: 1

Related Questions