Skip to content

Commit

Permalink
feature #24358 [TwigBundle] register an identity translator as fallba…
Browse files Browse the repository at this point in the history
…ck (xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[TwigBundle] register an identity translator as fallback

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #24303 (comment)
| License       | MIT
| Doc PR        |

The Form component can be used without the Translation component.
However, to be able to use the default form themes provided by the
TwigBridge you need to have the `trans` filter to be available.

This change ensure that there will always be a `trans` filter which as
a fallback will just return the message key if no translator is present.

Commits
-------

f0876e5 register an identity translator as fallback
  • Loading branch information
fabpot committed Sep 28, 2017
2 parents 112cca7 + f0876e5 commit 54135cb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
10 changes: 9 additions & 1 deletion src/Symfony/Bridge/Twig/Extension/TranslationExtension.php
Expand Up @@ -32,7 +32,7 @@ class TranslationExtension extends AbstractExtension
private $translator;
private $translationNodeVisitor;

public function __construct(TranslatorInterface $translator, NodeVisitorInterface $translationNodeVisitor = null)
public function __construct(TranslatorInterface $translator = null, NodeVisitorInterface $translationNodeVisitor = null)
{
if (!$translationNodeVisitor) {
$translationNodeVisitor = new TranslationNodeVisitor();
Expand Down Expand Up @@ -94,11 +94,19 @@ public function getTranslationNodeVisitor()

public function trans($message, array $arguments = array(), $domain = null, $locale = null)
{
if (null === $this->translator) {
return $message;
}

return $this->translator->trans($message, $arguments, $domain, $locale);
}

public function transchoice($message, $count, array $arguments = array(), $domain = null, $locale = null)
{
if (null === $this->translator) {
return $message;
}

return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
}

Expand Down
Expand Up @@ -35,9 +35,6 @@ public function process(ContainerBuilder $container)
if (!interface_exists('Symfony\Component\Routing\Generator\UrlGeneratorInterface')) {
$container->removeDefinition('twig.extension.routing');
}
if (!interface_exists('Symfony\Component\Translation\TranslatorInterface')) {
$container->removeDefinition('twig.extension.trans');
}

if (!class_exists('Symfony\Component\Yaml\Yaml')) {
$container->removeDefinition('twig.extension.yaml');
Expand All @@ -49,10 +46,6 @@ public function process(ContainerBuilder $container)
$container->getDefinition('twig.loader.native_filesystem')->addMethodCall('addPath', array(dirname(dirname($reflClass->getFileName())).'/Resources/views/Form'));
}

if ($container->has('translator')) {
$container->getDefinition('twig.extension.trans')->addTag('twig.extension');
}

if ($container->has('router')) {
$container->getDefinition('twig.extension.routing')->addTag('twig.extension');
}
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Expand Up @@ -72,7 +72,8 @@
</service>

<service id="twig.extension.trans" class="Symfony\Bridge\Twig\Extension\TranslationExtension">
<argument type="service" id="translator" />
<argument type="service" id="translator" on-invalid="null" />
<tag name="twig.extension" />
</service>

<service id="twig.extension.assets" class="Symfony\Bridge\Twig\Extension\AssetExtension">
Expand Down

0 comments on commit 54135cb

Please sign in to comment.