Skip to content

Commit 54135cb

Browse files
committed
feature #24358 [TwigBundle] register an identity translator as fallback (xabbuh)
This PR was merged into the 3.4 branch. Discussion ---------- [TwigBundle] register an identity translator as fallback | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #24303 (comment) | License | MIT | Doc PR | 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. Commits ------- f0876e5 register an identity translator as fallback
2 parents 112cca7 + f0876e5 commit 54135cb

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/Symfony/Bridge/Twig/Extension/TranslationExtension.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class TranslationExtension extends AbstractExtension
3232
private $translator;
3333
private $translationNodeVisitor;
3434

35-
public function __construct(TranslatorInterface $translator, NodeVisitorInterface $translationNodeVisitor = null)
35+
public function __construct(TranslatorInterface $translator = null, NodeVisitorInterface $translationNodeVisitor = null)
3636
{
3737
if (!$translationNodeVisitor) {
3838
$translationNodeVisitor = new TranslationNodeVisitor();
@@ -94,11 +94,19 @@ public function getTranslationNodeVisitor()
9494

9595
public function trans($message, array $arguments = array(), $domain = null, $locale = null)
9696
{
97+
if (null === $this->translator) {
98+
return $message;
99+
}
100+
97101
return $this->translator->trans($message, $arguments, $domain, $locale);
98102
}
99103

100104
public function transchoice($message, $count, array $arguments = array(), $domain = null, $locale = null)
101105
{
106+
if (null === $this->translator) {
107+
return $message;
108+
}
109+
102110
return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
103111
}
104112

src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php

-7
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ public function process(ContainerBuilder $container)
3535
if (!interface_exists('Symfony\Component\Routing\Generator\UrlGeneratorInterface')) {
3636
$container->removeDefinition('twig.extension.routing');
3737
}
38-
if (!interface_exists('Symfony\Component\Translation\TranslatorInterface')) {
39-
$container->removeDefinition('twig.extension.trans');
40-
}
4138

4239
if (!class_exists('Symfony\Component\Yaml\Yaml')) {
4340
$container->removeDefinition('twig.extension.yaml');
@@ -49,10 +46,6 @@ public function process(ContainerBuilder $container)
4946
$container->getDefinition('twig.loader.native_filesystem')->addMethodCall('addPath', array(dirname(dirname($reflClass->getFileName())).'/Resources/views/Form'));
5047
}
5148

52-
if ($container->has('translator')) {
53-
$container->getDefinition('twig.extension.trans')->addTag('twig.extension');
54-
}
55-
5649
if ($container->has('router')) {
5750
$container->getDefinition('twig.extension.routing')->addTag('twig.extension');
5851
}

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@
7272
</service>
7373

7474
<service id="twig.extension.trans" class="Symfony\Bridge\Twig\Extension\TranslationExtension">
75-
<argument type="service" id="translator" />
75+
<argument type="service" id="translator" on-invalid="null" />
76+
<tag name="twig.extension" />
7677
</service>
7778

7879
<service id="twig.extension.assets" class="Symfony\Bridge\Twig\Extension\AssetExtension">

0 commit comments

Comments
 (0)