Custom trans filter in Twig and Symfony2
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';
}
}
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.