register an identity translator as fallback

The Form component can be used without the Translation component.
However, to be able to use the default form themes provided by the
TwigBridge you need to have the `trans` filter to be available.

This change ensure that there will always be a `trans` filter which as
a fallback will just return the message key if no translator is present.
This commit is contained in:
Christian Flothmann 2017-09-28 13:24:51 +02:00
parent bd3bc69036
commit f0876e5927
3 changed files with 11 additions and 9 deletions

View File

@ -32,7 +32,7 @@ class TranslationExtension extends AbstractExtension
private $translator;
private $translationNodeVisitor;
public function __construct(TranslatorInterface $translator, NodeVisitorInterface $translationNodeVisitor = null)
public function __construct(TranslatorInterface $translator = null, NodeVisitorInterface $translationNodeVisitor = null)
{
if (!$translationNodeVisitor) {
$translationNodeVisitor = new TranslationNodeVisitor();
@ -94,11 +94,19 @@ class TranslationExtension extends AbstractExtension
public function trans($message, array $arguments = array(), $domain = null, $locale = null)
{
if (null === $this->translator) {
return $message;
}
return $this->translator->trans($message, $arguments, $domain, $locale);
}
public function transchoice($message, $count, array $arguments = array(), $domain = null, $locale = null)
{
if (null === $this->translator) {
return $message;
}
return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
}

View File

@ -35,9 +35,6 @@ class ExtensionPass implements CompilerPassInterface
if (!interface_exists('Symfony\Component\Routing\Generator\UrlGeneratorInterface')) {
$container->removeDefinition('twig.extension.routing');
}
if (!interface_exists('Symfony\Component\Translation\TranslatorInterface')) {
$container->removeDefinition('twig.extension.trans');
}
if (!class_exists('Symfony\Component\Yaml\Yaml')) {
$container->removeDefinition('twig.extension.yaml');
@ -49,10 +46,6 @@ class ExtensionPass implements CompilerPassInterface
$container->getDefinition('twig.loader.native_filesystem')->addMethodCall('addPath', array(dirname(dirname($reflClass->getFileName())).'/Resources/views/Form'));
}
if ($container->has('translator')) {
$container->getDefinition('twig.extension.trans')->addTag('twig.extension');
}
if ($container->has('router')) {
$container->getDefinition('twig.extension.routing')->addTag('twig.extension');
}

View File

@ -72,7 +72,8 @@
</service>
<service id="twig.extension.trans" class="Symfony\Bridge\Twig\Extension\TranslationExtension">
<argument type="service" id="translator" />
<argument type="service" id="translator" on-invalid="null" />
<tag name="twig.extension" />
</service>
<service id="twig.extension.assets" class="Symfony\Bridge\Twig\Extension\AssetExtension">