8000 [FrameworkBundle] Register an identity translator as fallback by yceruto · Pull Request #28523 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Register an identity translator as fallback #28523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/Symfony/Bridge/Twig/Extension/TranslationExtension.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change< 8000 /th>
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.

Original file line number Diff line number Diff line change
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