Translator.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Translation\Translator as BaseTranslator;
  12. use Symfony\Component\Translation\Loader\LoaderInterface;
  13. use Symfony\Component\Translation\MessageSelector;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Symfony\Component\HttpFoundation\Session;
  16. use Symfony\Component\Config\ConfigCache;
  17. /**
  18. * Translator.
  19. *
  20. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21. */
  22. class Translator extends BaseTranslator
  23. {
  24. protected $container;
  25. protected $options;
  26. protected $session;
  27. /**
  28. * Constructor.
  29. *
  30. * Available options:
  31. *
  32. * * cache_dir: The cache directory (or null to disable caching)
  33. * * debug: Whether to enable debugging or not (false by default)
  34. *
  35. * @param ContainerInterface $container A ContainerInterface instance
  36. * @param MessageSelector $selector The message selector for pluralization
  37. * @param array $options An array of options
  38. * @param Session $session A Session instance
  39. */
  40. public function __construct(ContainerInterface $container, MessageSelector $selector, array $options = array(), Session $session = null)
  41. {
  42. parent::__construct(null, $selector);
  43. $this->session = $session;
  44. $this->container = $container;
  45. $this->options = array(
  46. 'cache_dir' => null,
  47. 'debug' => false,
  48. );
  49. // check option names
  50. if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
  51. throw new \InvalidArgumentException(sprintf('The Router does not support the following options: \'%s\'.', implode('\', \'', $diff)));
  52. }
  53. $this->options = array_merge($this->options, $options);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function getLocale()
  59. {
  60. if (null === $this->locale && null !== $this->session) {
  61. $this->locale = $this->session->getLocale();
  62. }
  63. return $this->locale;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. protected function loadCatalogue($locale)
  69. {
  70. if (isset($this->catalogues[$locale])) {
  71. return;
  72. }
  73. if (null === $this->options['cache_dir']) {
  74. $this->initialize();
  75. return parent::loadCatalogue($locale);
  76. }
  77. $cache = new ConfigCache($this->options['cache_dir'], 'catalogue.'.$locale, $this->options['debug']);
  78. if (!$cache->isFresh()) {
  79. $this->initialize();
  80. parent::loadCatalogue($locale);
  81. $content = sprintf(
  82. "<?php use Symfony\Component\Translation\MessageCatalogue; return new MessageCatalogue('%s', %s);",
  83. $locale,
  84. var_export($this->catalogues[$locale]->all(), true)
  85. );
  86. $cache->write($content, $this->catalogues[$locale]->getResources());
  87. return;
  88. }
  89. $this->catalogues[$locale] = include $cache;
  90. }
  91. protected function initialize()
  92. {
  93. foreach ($this->container->getParameter('translation.loaders') as $id => $alias) {
  94. $this->addLoader($alias, $this->container->get($id));
  95. }
  96. foreach ($this->container->getParameter('translation.resources') as $resource) {
  97. $this->addResource($resource[0], $resource[1], $resource[2], $resource[3]);
  98. }
  99. }
  100. }