symfonytwigtwig-extensionsymfony5

How to pass the Translator to Twig Date Extension


I am using Symfony5 and Twig 1.5 and want to translate the time_diff filter. My service configuration looks like this:

twig.extension:
        class: Twig_Extensions_Extension_Date
        arguments: ['@translator']
        tags:
          - { name: twig.extension }

Unfortunately I get the following error message:

Argument 1 passed to Twig_Extensions_Extension_Date::__construct() must be an instance of Symfony\Component\Translation\TranslatorInterface or null, instance of Symfony\Component\Translation\DataCollectorTranslator

The reason for this is that Twig_Extensions_Extension_Date expects Symfony\Component\Translation\TranslatorInterface but DataCollectorTranslator implements Symfony\Contracts\Translation\TranslatorInterface.

How can I pass the correct translator to Twig_Extensions_Extension_Date?


Solution

  • It looks like Twig-extensions was abandoned and you have to write it yourself or use another bundle (https://github.com/twigphp/Twig-extensions/issues/264).