'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 1:[1]

Just to expand on @Webberig's answer above, in Symphony v3.0.4 and Twig v1.24.0, the way you define your service seems important.

I wanted to add a domain fallback capability to the default trans() filter but couldn't figure out how to override the default filter. I finally succeeded like so:

In app/config/services.yml

# This is important!! Use this exact service ID (twig.extension.trans)
twig.extension.trans:
  class: AppBundle\Twig\AppTranslationExtension
  public: false
  arguments: ['@translator']
  tags:
    - { name: twig.extension }

And that's what basically did it for me: using the same exact service ID in my config.

For the rest I just overrided the trans filter method. Here it is for those interested:

namespace AppBundle\Twig;

use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Component\Translation\TranslatorInterface;

class AppTranslationExtension extends TranslationExtension
{
    public function __construct(
            TranslatorInterface $translator, 
            \Twig_NodeVisitorInterface $translationNodeVisitor = null)
    {
        parent::__construct($translator, $translationNodeVisitor);
    }

    /**
     * {@inheritdoc}
     */
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('trans', array($this, 'trans')),
        );
    }

    public function trans($id, array $parameters = array(), $domain = null, $locale = null)
    {
        if (null === $locale) {
            $locale = $this->getTranslator()->getLocale();
        }

        if (null === $domain) {
            $domain = 'messages';
        }

        if ('messages' !== $domain 
        && false === $this->translationExists($id, $domain, $locale)) {
            $domain = 'messages';
        }

        return $this->getTranslator()->trans($id, $parameters, $domain, $locale);
    }

    protected function translationExists($id, $domain, $locale)
    {
        return $this->getTranslator()->getCatalogue($locale)->has((string) $id, $domain);
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'app_translator';
    }
}

Solution 2:[2]

To elaborate after @Webberig, you can define your own translation extension without overriding the original one (wich IMO is bad because you have to maintain at least the constructor signature) and just use the original extension as inner service and access the original trans() member function wich should be always public by definition...

# config/services.yaml
...
    App\Twig\TranslationExtension:
        arguments: ['@twig.extension.trans']
        tags:
            - { name: twig.extension, priority: 100 }
...
// src\Twig\TranslationExtension.php
namespace App\Twig;

use Symfony\Bridge\Twig\Extension\TranslationExtension as BaseTranslationExtension;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class TranslationExtension extends AbstractExtension
{
    /**
     * @var BaseTranslationExtension
     */
    protected $inner;
    public function __construct(BaseTranslationExtension $inner)
    {
        $this->inner = $inner;
    }

    public function getFilters()
    {
        return [
            new TwigFilter('trans', [$this, 'trans']),
        ];
    }

    public function trans($message, array $arguments = [], $domain = null, $locale = null, $count = null)
    {
        // do ugly stuff

        return $this->inner->trans($message, $arguments, $domain, $locale, $count);
    }
}

(used against SF4.2)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 peamak
Solution 2 quazardous