Skip to content

Commit fd9ca66

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 4f61840 commit fd9ca66

File tree

6 files changed

+18
-36
lines changed

6 files changed

+18
-36
lines changed

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

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">

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

Tests/DependencyInjection/Fixtures/php/templating_php_translator_disabled.php

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

Tests/DependencyInjection/Fixtures/php/templating_php_translator_enabled.php

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

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)