简体   繁体   English

TWIG:用变量翻译字符串?

[英]TWIG: Translation of string with variables?

In my translation.yml I have this var: 在我的translation.yml中我有这个var:

all.in.EN: All In Great Britain

In my Twig I have something like this: 在我的Twig我有这样的事情:

{% 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. 但是输出是allInName而不是翻译。 Someone an idea what I'm doing wrong? 有人知道我做错了什么? THNAKS! 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 '〜'符号用于连接,并且| trans过滤

Try 尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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