Skip to content

Commit 7871073

Browse files
ycerutonicolas-grekas
authored andcommitted
[FrameworkBundle] 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 FrameworkBundle you need to have the `translator` helper to be available. This change ensure that there will always be a `translator` helper which as a fallback will just return the message key if no translator is present.
1 parent c5af46d commit 7871073

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Extension/TranslationExtension.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser;
1818
use Symfony\Bridge\Twig\TokenParser\TransTokenParser;
1919
use Symfony\Contracts\Translation\TranslatorInterface;
20+
use Symfony\Contracts\Translation\TranslatorTrait;
2021
use Twig\Extension\AbstractExtension;
2122
use Twig\NodeVisitor\NodeVisitorInterface;
2223
use Twig\TokenParser\AbstractTokenParser;
@@ -29,6 +30,13 @@
2930
*/
3031
class TranslationExtension extends AbstractExtension
3132
{
33+
use TranslatorTrait {
34+
getLocale as private;
35+
setLocale as private;
36+
trans as private doTrans;
37+
transChoice as private doTransChoice;
38+
}
39+
3240
private $translator;
3341
private $translationNodeVisitor;
3442

@@ -91,7 +99,7 @@ public function getTranslationNodeVisitor()
9199
public function trans($message, array $arguments = array(), $domain = null, $locale = null)
92100
{
93101
if (null === $this->translator) {
94-
return strtr($message, $arguments);
102+
return $this->doTrans($message, $arguments, $domain, $locale);
95103
}
96104

97105
return $this->translator->trans($message, $arguments, $domain, $locale);
@@ -100,7 +108,7 @@ public function trans($message, array $arguments = array(), $domain = null, $loc
100108
public function transchoice($message, $count, array $arguments = array(), $domain = null, $locale = null)
101109
{
102110
if (null === $this->translator) {
103-
return strtr($message, $arguments);
111+
return $this->doTransChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
104112
}
105113

106114
return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);

0 commit comments

Comments
 (0)