Skip to content

Commit 7371753

Browse files
committed
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.
1 parent 2bbf765 commit 7371753

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

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

0 commit comments

Comments
 (0)