vendor/symfony/framework-bundle/Templating/Helper/TranslatorHelper.php line 14

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bundle\FrameworkBundle\Templating\Helper;
  11. @trigger_error('The '.TranslatorHelper::class.' class is deprecated since version 4.3 and will be removed in 5.0; use Twig instead.'E_USER_DEPRECATED);
  12. use Symfony\Component\Templating\Helper\Helper;
  13. use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. use Symfony\Contracts\Translation\TranslatorTrait;
  16. /**
  17.  * @author Fabien Potencier <fabien@symfony.com>
  18.  *
  19.  * @deprecated since version 4.3, to be removed in 5.0; use Twig instead.
  20.  */
  21. class TranslatorHelper extends Helper
  22. {
  23.     use TranslatorTrait {
  24.         getLocale as private;
  25.         setLocale as private;
  26.         trans as private doTrans;
  27.     }
  28.     protected $translator;
  29.     /**
  30.      * @param TranslatorInterface|null $translator
  31.      */
  32.     public function __construct($translator null)
  33.     {
  34.         if (null !== $translator && !$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
  35.             throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be an instance of "%s", "%s" given.'__METHOD__TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
  36.         }
  37.         $this->translator $translator;
  38.     }
  39.     /**
  40.      * @see TranslatorInterface::trans()
  41.      */
  42.     public function trans($id, array $parameters = [], $domain 'messages'$locale null)
  43.     {
  44.         if (null === $this->translator) {
  45.             return $this->doTrans($id$parameters$domain$locale);
  46.         }
  47.         return $this->translator->trans($id$parameters$domain$locale);
  48.     }
  49.     /**
  50.      * @see TranslatorInterface::transChoice()
  51.      * @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter
  52.      */
  53.     public function transChoice($id$number, array $parameters = [], $domain 'messages'$locale null)
  54.     {
  55.         @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the trans() one instead with a "%%count%%" parameter.'__METHOD__), E_USER_DEPRECATED);
  56.         if (null === $this->translator) {
  57.             return $this->doTrans($id, ['%count%' => $number] + $parameters$domain$locale);
  58.         }
  59.         if ($this->translator instanceof TranslatorInterface) {
  60.             return $this->translator->trans($id, ['%count%' => $number] + $parameters$domain$locale);
  61.         }
  62.         return $this->translator->transChoice($id$number$parameters$domain$locale);
  63.     }
  64.     /**
  65.      * {@inheritdoc}
  66.      */
  67.     public function getName()
  68.     {
  69.         return 'translator';
  70.     }
  71. }