vendor/symfony/framework-bundle/Translation/Translator.php line 123

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\Translation;
  11. use Psr\Container\ContainerInterface;
  12. use Symfony\Component\Config\Resource\DirectoryResource;
  13. use Symfony\Component\Config\Resource\FileExistenceResource;
  14. use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
  15. use Symfony\Component\Translation\Exception\InvalidArgumentException;
  16. use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
  17. use Symfony\Component\Translation\Translator as BaseTranslator;
  18. /**
  19.  * @author Fabien Potencier <fabien@symfony.com>
  20.  */
  21. class Translator extends BaseTranslator implements WarmableInterface
  22. {
  23.     protected $container;
  24.     protected $loaderIds;
  25.     protected $options = [
  26.         'cache_dir' => null,
  27.         'debug' => false,
  28.         'resource_files' => [],
  29.         'scanned_directories' => [],
  30.         'cache_vary' => [],
  31.     ];
  32.     /**
  33.      * @var list<string>
  34.      */
  35.     private $resourceLocales;
  36.     /**
  37.      * Holds parameters from addResource() calls so we can defer the actual
  38.      * parent::addResource() calls until initialize() is executed.
  39.      *
  40.      * @var array[]
  41.      */
  42.     private $resources = [];
  43.     /**
  44.      * @var string[][]
  45.      */
  46.     private $resourceFiles;
  47.     /**
  48.      * @var string[]
  49.      */
  50.     private $scannedDirectories;
  51.     /**
  52.      * @var string[]
  53.      */
  54.     private $enabledLocales;
  55.     /**
  56.      * Constructor.
  57.      *
  58.      * Available options:
  59.      *
  60.      *   * cache_dir:      The cache directory (or null to disable caching)
  61.      *   * debug:          Whether to enable debugging or not (false by default)
  62.      *   * resource_files: List of translation resources available grouped by locale.
  63.      *   * cache_vary:     An array of data that is serialized to generate the cached catalogue name.
  64.      *
  65.      * @throws InvalidArgumentException
  66.      */
  67.     public function __construct(ContainerInterface $containerMessageFormatterInterface $formatterstring $defaultLocale, array $loaderIds = [], array $options = [], array $enabledLocales = [])
  68.     {
  69.         $this->container $container;
  70.         $this->loaderIds $loaderIds;
  71.         $this->enabledLocales $enabledLocales;
  72.         // check option names
  73.         if ($diff array_diff(array_keys($options), array_keys($this->options))) {
  74.             throw new InvalidArgumentException(sprintf('The Translator does not support the following options: \'%s\'.'implode('\', \''$diff)));
  75.         }
  76.         $this->options array_merge($this->options$options);
  77.         $this->resourceLocales array_keys($this->options['resource_files']);
  78.         $this->resourceFiles $this->options['resource_files'];
  79.         $this->scannedDirectories $this->options['scanned_directories'];
  80.         parent::__construct($defaultLocale$formatter$this->options['cache_dir'], $this->options['debug'], $this->options['cache_vary']);
  81.     }
  82.     /**
  83.      * {@inheritdoc}
  84.      *
  85.      * @return string[]
  86.      */
  87.     public function warmUp(string $cacheDir)
  88.     {
  89.         // skip warmUp when translator doesn't use cache
  90.         if (null === $this->options['cache_dir']) {
  91.             return [];
  92.         }
  93.         $localesToWarmUp $this->enabledLocales ?: array_merge($this->getFallbackLocales(), [$this->getLocale()], $this->resourceLocales);
  94.         foreach (array_unique($localesToWarmUp) as $locale) {
  95.             // reset catalogue in case it's already loaded during the dump of the other locales.
  96.             if (isset($this->catalogues[$locale])) {
  97.                 unset($this->catalogues[$locale]);
  98.             }
  99.             $this->loadCatalogue($locale);
  100.         }
  101.         return [];
  102.     }
  103.     public function addResource(string $format$resourcestring $localestring $domain null)
  104.     {
  105.         if ($this->resourceFiles) {
  106.             $this->addResourceFiles();
  107.         }
  108.         $this->resources[] = [$format$resource$locale$domain];
  109.     }
  110.     /**
  111.      * {@inheritdoc}
  112.      */
  113.     protected function initializeCatalogue(string $locale)
  114.     {
  115.         $this->initialize();
  116.         parent::initializeCatalogue($locale);
  117.     }
  118.     /**
  119.      * @internal
  120.      */
  121.     protected function doLoadCatalogue(string $locale): void
  122.     {
  123.         parent::doLoadCatalogue($locale);
  124.         foreach ($this->scannedDirectories as $directory) {
  125.             $resourceClass file_exists($directory) ? DirectoryResource::class : FileExistenceResource::class;
  126.             $this->catalogues[$locale]->addResource(new $resourceClass($directory));
  127.         }
  128.     }
  129.     protected function initialize()
  130.     {
  131.         if ($this->resourceFiles) {
  132.             $this->addResourceFiles();
  133.         }
  134.         foreach ($this->resources as $params) {
  135.             [$format$resource$locale$domain] = $params;
  136.             parent::addResource($format$resource$locale$domain);
  137.         }
  138.         $this->resources = [];
  139.         foreach ($this->loaderIds as $id => $aliases) {
  140.             foreach ($aliases as $alias) {
  141.                 $this->addLoader($alias$this->container->get($id));
  142.             }
  143.         }
  144.     }
  145.     private function addResourceFiles(): void
  146.     {
  147.         $filesByLocale $this->resourceFiles;
  148.         $this->resourceFiles = [];
  149.         foreach ($filesByLocale as $files) {
  150.             foreach ($files as $file) {
  151.                 // filename is domain.locale.format
  152.                 $fileNameParts explode('.'basename($file));
  153.                 $format array_pop($fileNameParts);
  154.                 $locale array_pop($fileNameParts);
  155.                 $domain implode('.'$fileNameParts);
  156.                 $this->addResource($format$file$locale$domain);
  157.             }
  158.         }
  159.     }
  160. }