Translator.php 4.4 KB

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