PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/fanch1/testlb
PHP | 141 lines | 74 code | 19 blank | 48 comment | 3 complexity | 30dec912a41e4e2ec648765fc50afd99 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, BSD-2-Clause, GPL-2.0, GPL-3.0
  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\HttpKernel\CacheWarmer\WarmableInterface;
  12. use Symfony\Component\Translation\Translator as BaseTranslator;
  13. use Symfony\Component\Translation\MessageSelector;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  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. }