8000 Register an identity translator as fallback · symfony/symfony@ae03f59 · GitHub
[go: up one dir, main page]

Skip to content

Commit ae03f59

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

File tree

6 files changed

+10
-36
lines changed

6 files changed

+10
-36
lines changed

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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class TranslatorHelper extends Helper
2121
{
2222
protected $translator;
2323

24-
public function __construct(TranslatorInterface $translator)
24+
public function __construct(TranslatorInterface $translator = null)
2525
{
2626
$this->translator = $translator;
2727
}
@@ -31,6 +31,10 @@ public function __construct(TranslatorInterface $translator)
3131
*/
3232
public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
3333
{
34+
if (null === $this->translator) {
35+
return strtr($id, $parameters);
36+
}
37+
3438
return $this->translator->trans($id, $parameters, $domain, $locale);
3539
}
3640

@@ -39,6 +43,10 @@ public function trans($id, array $parameters = array(), $domain = 'messages', $l
3943
*/
4044
public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
4145
{
46+
if (null === $this->translator) {
47+
return strtr($id, $parameters);
48+
}
49+
4250
return $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
4351
}
4452

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)
0