8000 feature #28523 [FrameworkBundle] Register an identity translator as f… · symfony/symfony@e198a26 · GitHub
[go: up one dir, main page]

Skip to content

Commit e198a26

Browse files
feature #28523 [FrameworkBundle] Register an identity translator as fallback (yceruto)
This PR was merged into the 4.2-dev branch. Discussion ---------- [FrameworkBundle] Register an identity translator as fallback | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #27589 | License | MIT | Doc PR | - The same approach as #24358, suggested by @xabbuh here #27589 (comment) **Templating Engine Context** 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. Commits ------- 5330f2d [FrameworkBundle] Register an identity translator as fallback
2 parents 7d51b57 + 5330f2d commit e198a26

File tree

7 files changed

+28
-38
lines changed

7 files changed

+28
-38
lines changed

src/Symfony/Bridge/Twig/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);

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 <fabien@symfony.com>
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)
0