vendor/sylius/theme-bundle/src/Translation/ThemeAwareTranslator.php line 99

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ThemeBundle\Translation;
  12. use Sylius\Bundle\ThemeBundle\Context\ThemeContextInterface;
  13. use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
  14. use Symfony\Component\Translation\MessageCatalogueInterface;
  15. use Symfony\Component\Translation\TranslatorBagInterface;
  16. use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
  17. use Symfony\Contracts\Translation\LocaleAwareInterface;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. final class ThemeAwareTranslator implements TranslatorInterfaceTranslatorBagInterfaceWarmableInterfaceLocaleAwareInterfaceLegacyTranslatorInterface
  20. {
  21.     /** @var LegacyTranslatorInterface&TranslatorBagInterface */
  22.     private $translator;
  23.     /** @var ThemeContextInterface */
  24.     private $themeContext;
  25.     public function __construct(LegacyTranslatorInterface $translatorThemeContextInterface $themeContext)
  26.     {
  27.         if (!$translator instanceof TranslatorBagInterface) {
  28.             throw new \InvalidArgumentException(sprintf(
  29.                 'The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.',
  30.                 get_class($translator)
  31.             ));
  32.         }
  33.         $this->translator $translator;
  34.         $this->themeContext $themeContext;
  35.     }
  36.     /**
  37.      * Passes through all unknown calls onto the translator object.
  38.      */
  39.     public function __call(string $method, array $arguments)
  40.     {
  41.         $translator $this->translator;
  42.         $arguments array_values($arguments);
  43.         return $translator->$method(...$arguments);
  44.     }
  45.     /**
  46.      * @psalm-suppress MissingParamType Two interfaces defining the same method
  47.      */
  48.     public function trans($id, array $parameters = [], $domain null$locale null): string
  49.     {
  50.         return $this->translator->trans($id$parameters$domain$this->transformLocale($locale));
  51.     }
  52.     public function transChoice($id$number, array $parameters = [], $domain null$locale null): string
  53.     {
  54.         return $this->translator->transChoice($id$number$parameters$domain$this->transformLocale($locale));
  55.     }
  56.     public function getLocale(): string
  57.     {
  58.         return $this->translator->getLocale();
  59.     }
  60.     /**
  61.      * @param string $locale
  62.      */
  63.     public function setLocale($locale): void
  64.     {
  65.         /** @var string $locale */
  66.         $locale $this->transformLocale($locale);
  67.         $this->translator->setLocale($locale);
  68.     }
  69.     public function getCatalogue($locale null): MessageCatalogueInterface
  70.     {
  71.         return $this->translator->getCatalogue($locale);
  72.     }
  73.     public function warmUp($cacheDir): void
  74.     {
  75.         if ($this->translator instanceof WarmableInterface) {
  76.             $this->translator->warmUp($cacheDir);
  77.         }
  78.     }
  79.     private function transformLocale(?string $locale): ?string
  80.     {
  81.         $theme $this->themeContext->getTheme();
  82.         if (null === $theme) {
  83.             return $locale;
  84.         }
  85.         if (null === $locale) {
  86.             $locale $this->getLocale();
  87.         }
  88.         return $locale '@' str_replace('/''-'$theme->getName());
  89.     }
  90. }