bug #31761 [TwigBridge] suggest Translation Component when TranslationExtension is used (nicolas-grekas)

This PR was merged into the 4.3 branch.

Discussion
----------

[TwigBridge] suggest Translation Component when TranslationExtension is used

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #31754
| License       | MIT
| Doc PR        | -

Commits
-------

d4a9e7e639 [TwigBridge] suggest Translation Component when TranslationExtension is used
This commit is contained in:
Fabien Potencier 2019-06-01 11:33:08 +03:00
commit d8224b87c8

View File

@ -49,11 +49,19 @@ class TranslationExtension extends AbstractExtension
}
/**
* @deprecated since Symfony 4.2
* @return TranslatorInterface|null
*/
public function getTranslator()
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2.', __METHOD__), E_USER_DEPRECATED);
if (null === $this->translator) {
if (!interface_exists(TranslatorInterface::class)) {
throw new \LogicException(sprintf('You cannot use the "%s" if the Translation Contracts are not available. Try running "composer require symfony/translation".', __CLASS__));
}
$this->translator = new class() implements TranslatorInterface {
use TranslatorTrait;
};
}
return $this->translator;
}
@ -108,13 +116,8 @@ class TranslationExtension extends AbstractExtension
if (null !== $count) {
$arguments['%count%'] = $count;
}
if (null === $this->translator) {
$this->translator = new class() implements TranslatorInterface {
use TranslatorTrait;
};
}
return $this->translator->trans($message, $arguments, $domain, $locale);
return $this->getTranslator()->trans($message, $arguments, $domain, $locale);
}
/**
@ -122,17 +125,13 @@ class TranslationExtension extends AbstractExtension
*/
public function transchoice($message, $count, array $arguments = [], $domain = null, $locale = null)
{
if (null === $this->translator) {
$this->translator = new class() implements TranslatorInterface {
use TranslatorTrait;
};
$translator = $this->getTranslator();
if ($translator instanceof TranslatorInterface) {
return $translator->trans($message, array_merge(['%count%' => $count], $arguments), $domain, $locale);
}
if ($this->translator instanceof TranslatorInterface) {
return $this->translator->trans($message, array_merge(['%count%' => $count], $arguments), $domain, $locale);
}
return $this->translator->transChoice($message, $count, array_merge(['%count%' => $count], $arguments), $domain, $locale);
return $translator->transChoice($message, $count, array_merge(['%count%' => $count], $arguments), $domain, $locale);
}
/**