Twig 中的 Symfony4 翻译

Symfony4 translation in twig

至于Symfony4的翻译,多亏了这个article。它在 Controller 中运行良好。

 public function index(TranslatorInterface $translator)
    {
        $translated = $translator->trans('test');// it works
        print $translated;exit;

在messages.en.yaml

test: englishtest

但是我无法翻译 twig 中的消息。

<br>
{{ test|trans }}
<br>

显示错误Variable "test" does not exist.

我需要提前做一些事情以便在 twig 中进行翻译???

方法签名如下所示:

{{ message|trans(arguments = [], domain = null, locale = null) }}

https://symfony.com/doc/current/reference/twig_reference.html#trans

所以如果 test 不是变量,那么 {{ 'test'|trans }} 应该可以工作(正如 zalex 已经指出的那样)。