Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 43d20b1

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 00e5cd9 commit 43d20b1

File tree

7 files changed

+30
-40
lines changed

7 files changed

+30
-40
lines changed

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

Lines changed: 12 additions & 4 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

@@ -50,7 +58,7 @@ public function getFilters()
5058
{
5159
return array(
5260
new TwigFilter('trans', array($this, 'trans')),
53-
new TwigFilter('transchoice', array($this, 'transchoice')),
61+
new TwigFilter('transchoice', array($this, 'transChoice')),
5462
);
5563
}
5664

@@ -91,16 +99,16 @@ 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);
98106
}
99107

100-
public function transchoice($message, $count, array $arguments = array(), $domain = null, $locale = null)
108+
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);

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -873,10 +873,6 @@ private function registerTemplatingConfiguration(array $config, ContainerBuilder
873873
} else {
874874
$container->removeDefinition('templating.helper.assets');
875875
}
876-
877-
if (!$this->translationConfigEnabled) {
878-
$container->removeDefinition('templating.helper.translator');
879-
}
880876
}
881877
}
882878

src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858

5959
<service id="templating.helper.translator" class="Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper">
6060
<tag name="templating.helper" alias="translator" />
61-
<argument type="service" id="translator" />
61+
<argument type="service" id="translator" on-invalid="null" />
6262
</service>
6363

6464
<service id="templating.helper.form" class="Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper">

src/Symfony/Bundle/FrameworkBundle/Templating/Helper/TranslatorHelper.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@
1313

1414
use Symfony\Component\Templating\Helper\Helper;
1515
use Symfony\Contracts\Translation\TranslatorInterface;
16+
use Symfony\Contracts\Translation\TranslatorTrait;
1617

1718
/**
1819
* @author Fabien Potencier <[email protected]>
1920
*/
2021
class TranslatorHelper extends Helper
2122
{
23+
use TranslatorTrait {
24+
getLocale as private;
25+
setLocale as private;
26+
trans as private doTrans;
27+
transChoice as private doTransChoice;
28+
}
29+
2230
protected $translator;
2331

24-
public function __construct(TranslatorInterface $translator)
32+
public function __construct(TranslatorInterface $translator = null)
2533
{
2634
$this->translator = $translator;
2735
}
@@ -31,6 +39,10 @@ public function __construct(TranslatorInterface $translator)
3139
*/
3240
public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
3341
{
42+
if (null === $this->translator) {
43+
return $this->doTrans($id, $parameters, $domain, $locale);
44+
}
45+
3446
return $this->translator->trans($id, $parameters, $domain, $locale);
3547
}
3648

@@ -39,6 +51,10 @@ public function trans($id, array $parameters = array(), $domain = 'messages', $l
3951
*/
4052
public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
4153
{
54+
if (null === $this->translator) {
55+
return $this->doTransChoice($id, $number, $parameters, $domain, $locale);
56+
}
57+
4258
return $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
4359
}
4460

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/templating_php_translator_disabled.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/templating_php_translator_enabled.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -701,20 +701,6 @@ public function testTranslatorMultipleFallbacks()
701701
$this->assertEquals(array('en', 'fr'), $calls[1][1][0]);
702702
}
703703

704-
public function testTranslatorHelperIsRegisteredWhenTranslatorIsEnabled()
705-
{
706-
$container = $this->createContainerFromFile('templating_php_translator_enabled');
707-
708-
$this->assertTrue($container->has('templating.helper.translator'));
709-
}
710-
711-
public function testTranslatorHelperIsNotRegisteredWhenTranslatorIsDisabled()
712-
{
713-
$container = $this->createContainerFromFile('templating_php_translator_disabled');
714-
715-
$this->assertFalse($container->has('templating.helper.translator'));
716-
}
717-
718704
/**
719705
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
720706
*/

0 commit comments

Comments
 (0)