feature #24358 [TwigBundle] register an identity translator as fallback (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 | https://github.com/symfony/symfony/pull/24303#issuecomment-331864529
| 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
-------

f0876e5927 register an identity translator as fallback
This commit is contained in:
Fabien Potencier 2017-09-28 06:27:55 -07:00
commit 54135cb9ca
3 changed files with 11 additions and 9 deletions

View File

@ -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();
@ -94,11 +94,19 @@ class TranslationExtension extends AbstractExtension
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);
}

View File

@ -35,9 +35,6 @@ class ExtensionPass implements CompilerPassInterface
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');
@ -49,10 +46,6 @@ class ExtensionPass implements CompilerPassInterface
$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');
}

View File

@ -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">