Search code examples
phpsymfonytwigtranslationtwig-extension

Custom trans filter in Twig and Symfony2


I need to override the standard Twig trans filter for my own purposes, i.e I want to get my translations from a custom storage. I tried to figure it out in the docs. But there is no details about translations. I found the Symfony\Bridge\Twig\Extension\TranslationExtension class and I think that I only need to override this class? Thanks in advance!


Solution

  • The Translations component of Symfony basically overrides Twig's default trans filter. The component is part of the core frameworkBundle and can't be disabled.

    You can however do the same and re-override the trans filter with your own Twig extension.

    Just create a Twig extension as described here: http://symfony.com/doc/current/cookbook/templating/twig_extension.html

    If you add a trans filter, it will override the Translation component's code.