Translator.php 5.7 KB

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