Symfony2+Twig, variable in translation return “A message must be a simple text”

前端 未结 2 1018
梦如初夏
梦如初夏 2020-12-19 02:43

When I was doing plain PHP, I was simply doing this:

printf(_(\"Hello %s !\"), $name);

Now with Twig, I must use the trans tag. So I\'ve co

相关标签:
2条回答
  • 2020-12-19 03:36

    One missing bit with the previous answer is the "with" portion that is needed to do the replacement of the variable part of the message.

    {% trans with {'%name%':name} %}Hello %name%!{% endtrans %}
    
    0 讨论(0)
  • 2020-12-19 03:46

    The precise syntax for translations is a little different in Symfony2 than it is in standalone Twig. You'll want to check out the Symfony2 documentation for translations in twig templates, found here. The correct syntax would look something like this:

    {% trans %}Hello %name%!{% endtrans %}
    
    0 讨论(0)
提交回复
热议问题