在模板中的 Symfony 翻译中使用 Twig 变量

Using a Twig variable inside a Symfony translation in a template

天色已晚,我想我的大脑正在关闭……我这辈子都不知道如何更改翻译键以使用 currentType 的值。

{% set currentType = site.getCurrentType() %}
{{ 'messages.site.{currentType}'|trans({ '%url%' : path('appbundle_course_url', { 'subdomain': site.subdomain }) }) }}

我正在做一些愚蠢的事情,当我看到解决方案时会踢自己。我才知道。

DOZ 提示几乎是正确的。

'messages.site.'~currentType|trans

表示转换 currentType 然后附加到字符串(首先应用过滤器)。 所以适当的大括号就可以了。

这适用于我的代码:

{{ ('messages.site.'~currentType)|trans({ '%url%' : path('appbundle_course_url', { 'subdomain': site.subdomain }) }) }}