Skip to content

Commit

Permalink
[FrameworkBundle] Register an identity translator as fallback
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yceruto authored and nicolas-grekas committed Sep 20, 2018
1 parent 00e5cd9 commit 5330f2d
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 38 deletions.
12 changes: 10 additions & 2 deletions src/Symfony/Bridge/Twig/Extension/TranslationExtension.php
Expand Up @@ -17,6 +17,7 @@
use Symfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser;
use Symfony\Bridge\Twig\TokenParser\TransTokenParser;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Contracts\Translation\TranslatorTrait;
use Twig\Extension\AbstractExtension;
use Twig\NodeVisitor\NodeVisitorInterface;
use Twig\TokenParser\AbstractTokenParser;
Expand All @@ -29,6 +30,13 @@
*/
class TranslationExtension extends AbstractExtension
{
use TranslatorTrait {
getLocale as private;
setLocale as private;
trans as private doTrans;
transChoice as private doTransChoice;
}

private $translator;
private $translationNodeVisitor;

Expand Down Expand Up @@ -91,7 +99,7 @@ public function getTranslationNodeVisitor()
public function trans($message, array $arguments = array(), $domain = null, $locale = null)
{
if (null === $this->translator) {
return strtr($message, $arguments);
return $this->doTrans($message, $arguments, $domain, $locale);
}

return $this->translator->trans($message, $arguments, $domain, $locale);
Expand All @@ -100,7 +108,7 @@ public function trans($message, array $arguments = array(), $domain = null, $loc
public function transchoice($message, $count, array $arguments = array(), $domain = null, $locale = null)
{
if (null === $this->translator) {
return strtr($message, $arguments);
return $this->doTransChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
}

return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
Expand Down
Expand Up @@ -873,10 +873,6 @@ private function registerTemplatingConfiguration(array $config, ContainerBuilder
} else {
$container->removeDefinition('templating.helper.assets');
}

if (!$this->translationConfigEnabled) {
$container->removeDefinition('templating.helper.translator');
}
}
}

Expand Down
Expand Up @@ -58,7 +58,7 @@

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

<service id="templating.helper.form" class="Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper">
Expand Down
Expand Up @@ -13,15 +13,23 @@

use Symfony\Component\Templating\Helper\Helper;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Contracts\Translation\TranslatorTrait;

/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class TranslatorHelper extends Helper
{
use TranslatorTrait {
getLocale as private;
setLocale as private;
trans as private doTrans;
transChoice as private doTransChoice;
}

protected $translator;

public function __construct(TranslatorInterface $translator)
public function __construct(TranslatorInterface $translator = null)
{
$this->translator = $translator;
}
Expand All @@ -31,6 +39,10 @@ public function __construct(TranslatorInterface $translator)
*/
public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
{
if (null === $this->translator) {
return $this->doTrans($id, $parameters, $domain, $locale);
}

return $this->translator->trans($id, $parameters, $domain, $locale);
}

Expand All @@ -39,6 +51,10 @@ public function trans($id, array $parameters = array(), $domain = 'messages', $l
*/
public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
{
if (null === $this->translator) {
return $this->doTransChoice($id, $number, $parameters, $domain, $locale);
}

return $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
}

Expand Down

This file was deleted.

This file was deleted.

Expand Up @@ -701,20 +701,6 @@ public function testTranslatorMultipleFallbacks()
$this->assertEquals(array('en', 'fr'), $calls[1][1][0]);
}

public function testTranslatorHelperIsRegisteredWhenTranslatorIsEnabled()
{
$container = $this->createContainerFromFile('templating_php_translator_enabled');

$this->assertTrue($container->has('templating.helper.translator'));
}

public function testTranslatorHelperIsNotRegisteredWhenTranslatorIsDisabled()
{
$container = $this->createContainerFromFile('templating_php_translator_disabled');

$this->assertFalse($container->has('templating.helper.translator'));
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
Expand Down

0 comments on commit 5330f2d

Please sign in to comment.