Skip to content

Commit

Permalink
[TwigBridge] suggest Translation Component when TranslationExtension …
Browse files Browse the repository at this point in the history
…is used
  • Loading branch information
nicolas-grekas committed Jun 1, 2019
1 parent 89f423f commit d4a9e7e
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/Symfony/Bridge/Twig/Extension/TranslationExtension.php
Expand Up @@ -49,11 +49,19 @@ public function __construct($translator = null, NodeVisitorInterface $translatio
}

/**
* @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;
}
Expand Down Expand Up @@ -108,31 +116,22 @@ public function trans($message, array $arguments = [], $domain = null, $locale =
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);
}

/**
* @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter
*/
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 ($this->translator instanceof TranslatorInterface) {
return $this->translator->trans($message, array_merge(['%count%' => $count], $arguments), $domain, $locale);
if ($translator instanceof TranslatorInterface) {
return $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);
}

/**
Expand Down

0 comments on commit d4a9e7e

Please sign in to comment.