Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TWIG: Translation of string with variables?

Tags:

twig

symfony

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!

like image 834
Zwen2012 Avatar asked Jan 09 '23 16:01

Zwen2012


2 Answers

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

like image 93
Nawfal Serrar Avatar answered Jan 14 '23 22:01

Nawfal Serrar


Try

{% set allInName = 'all.in.' ~ countryKey %}
{{allInName | trans}}
like image 29
chapay Avatar answered Jan 14 '23 21:01

chapay