vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php line 128

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\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
  13. use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
  14. use Symfony\Component\Translation\Translator as BaseTranslator;
  15. use Symfony\Component\Translation\Exception\InvalidArgumentException;
  16. use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
  17. /**
  18.  * Translator.
  19.  *
  20.  * @author Fabien Potencier <fabien@symfony.com>
  21.  */
  22. class Translator extends BaseTranslator implements WarmableInterface
  23. {
  24.     protected $container;
  25.     protected $loaderIds;
  26.     protected $options = array(
  27.         'cache_dir' => null,
  28.         'debug' => false,
  29.         'resource_files' => array(),
  30.     );
  31.     /**
  32.      * @var array
  33.      */
  34.     private $resourceLocales;
  35.     /**
  36.      * Holds parameters from addResource() calls so we can defer the actual
  37.      * parent::addResource() calls until initialize() is executed.
  38.      *
  39.      * @var array
  40.      */
  41.     private $resources = array();
  42.     /**
  43.      * Constructor.
  44.      *
  45.      * Available options:
  46.      *
  47.      *   * cache_dir: The cache directory (or null to disable caching)
  48.      *   * debug:     Whether to enable debugging or not (false by default)
  49.      *   * resource_files: List of translation resources available grouped by locale.
  50.      *
  51.      * @param ContainerInterface        $container     A ContainerInterface instance
  52.      * @param MessageFormatterInterface $formatter     The message formatter
  53.      * @param string                    $defaultLocale
  54.      * @param array                     $loaderIds     An array of loader Ids
  55.      * @param array                     $options       An array of options
  56.      *
  57.      * @throws InvalidArgumentException
  58.      */
  59.     public function __construct(ContainerInterface $container$formatter$defaultLocale null, array $loaderIds = array(), array $options = array())
  60.     {
  61.         // BC 3.x, to be removed in 4.0 along with the $defaultLocale default value
  62.         if (is_array($defaultLocale) || func_num_args()) {
  63.             if (!$container instanceof SymfonyContainerInterface) {
  64.                 throw new \InvalidArgumentException('Missing third $defaultLocale argument.');
  65.             }
  66.             $options $loaderIds;
  67.             $loaderIds $defaultLocale;
  68.             $defaultLocale $container->getParameter('kernel.default_locale');
  69.             @trigger_error(sprintf('Method %s() takes the default locale as 3rd argument since Symfony 3.3. Not passing it is deprecated and will trigger an error in 4.0.'__METHOD__), E_USER_DEPRECATED);
  70.         }
  71.         $this->container $container;
  72.         $this->loaderIds $loaderIds;
  73.         // check option names
  74.         if ($diff array_diff(array_keys($options), array_keys($this->options))) {
  75.             throw new InvalidArgumentException(sprintf('The Translator does not support the following options: \'%s\'.'implode('\', \''$diff)));
  76.         }
  77.         $this->options array_merge($this->options$options);
  78.         $this->resourceLocales array_keys($this->options['resource_files']);
  79.         $this->addResourceFiles($this->options['resource_files']);
  80.         parent::__construct($defaultLocale$formatter$this->options['cache_dir'], $this->options['debug']);
  81.     }
  82.     /**
  83.      * {@inheritdoc}
  84.      */
  85.     public function warmUp($cacheDir)
  86.     {
  87.         // skip warmUp when translator doesn't use cache
  88.         if (null === $this->options['cache_dir']) {
  89.             return;
  90.         }
  91.         $locales array_merge($this->getFallbackLocales(), array($this->getLocale()), $this->resourceLocales);
  92.         foreach (array_unique($locales) as $locale) {
  93.             // reset catalogue in case it's already loaded during the dump of the other locales.
  94.             if (isset($this->catalogues[$locale])) {
  95.                 unset($this->catalogues[$locale]);
  96.             }
  97.             $this->loadCatalogue($locale);
  98.         }
  99.     }
  100.     public function addResource($format$resource$locale$domain null)
  101.     {
  102.         $this->resources[] = array($format$resource$locale$domain);
  103.     }
  104.     /**
  105.      * {@inheritdoc}
  106.      */
  107.     protected function initializeCatalogue($locale)
  108.     {
  109.         $this->initialize();
  110.         parent::initializeCatalogue($locale);
  111.     }
  112.     protected function initialize()
  113.     {
  114.         foreach ($this->resources as $key => $params) {
  115.             list($format$resource$locale$domain) = $params;
  116.             parent::addResource($format$resource$locale$domain);
  117.         }
  118.         $this->resources = array();
  119.         foreach ($this->loaderIds as $id => $aliases) {
  120.             foreach ($aliases as $alias) {
  121.                 $this->addLoader($alias$this->container->get($id));
  122.             }
  123.         }
  124.     }
  125.     private function addResourceFiles($filesByLocale)
  126.     {
  127.         foreach ($filesByLocale as $locale => $files) {
  128.             foreach ($files as $key => $file) {
  129.                 // filename is domain.locale.format
  130.                 list($domain$locale$format) = explode('.'basename($file), 3);
  131.                 $this->addResource($format$file$locale$domain);
  132.             }
  133.         }
  134.     }
  135. }