简体   繁体   中英

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!

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

Try

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

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