Translator.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\Component\Translation;
  11. use Symfony\Component\Translation\Loader\LoaderInterface;
  12. /**
  13. * Translator.
  14. *
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. */
  17. class Translator implements TranslatorInterface
  18. {
  19. protected $catalogues;
  20. protected $locale;
  21. protected $fallbackLocale;
  22. protected $loaders;
  23. protected $resources;
  24. protected $selector;
  25. /**
  26. * Constructor.
  27. *
  28. * @param string $locale The locale
  29. * @param MessageSelector $selector The message selector for pluralization
  30. */
  31. public function __construct($locale = null, MessageSelector $selector)
  32. {
  33. $this->locale = $locale;
  34. $this->selector = $selector;
  35. $this->loaders = array();
  36. $this->resources = array();
  37. $this->catalogues = array();
  38. }
  39. /**
  40. * Adds a Loader.
  41. *
  42. * @param string $format The name of the loader (@see addResource())
  43. * @param LoaderInterface $loader A LoaderInterface instance
  44. */
  45. public function addLoader($format, LoaderInterface $loader)
  46. {
  47. $this->loaders[$format] = $loader;
  48. }
  49. /**
  50. * Adds a Resource.
  51. *
  52. * @param string $format The name of the loader (@see addLoader())
  53. * @param mixed $resource The resource name
  54. * @param string $locale The locale
  55. * @param string $domain The domain
  56. */
  57. public function addResource($format, $resource, $locale, $domain = 'messages')
  58. {
  59. if (!isset($this->resources[$locale])) {
  60. $this->resources[$locale] = array();
  61. }
  62. $this->resources[$locale][] = array($format, $resource, $domain);
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function setLocale($locale)
  68. {
  69. $this->locale = $locale;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function getLocale()
  75. {
  76. return $this->locale;
  77. }
  78. /**
  79. * Sets the fallback locale.
  80. *
  81. * @param string $locale The fallback locale
  82. */
  83. public function setFallbackLocale($locale)
  84. {
  85. // needed as the fallback locale is used to fill-in non-yet translated messages
  86. $this->catalogues = array();
  87. $this->fallbackLocale = $locale;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
  93. {
  94. if (!isset($locale)) {
  95. $locale = $this->getLocale();
  96. }
  97. if (!isset($this->catalogues[$locale])) {
  98. $this->loadCatalogue($locale);
  99. }
  100. return strtr($this->catalogues[$locale]->get($id, $domain), $parameters);
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
  106. {
  107. if (!isset($locale)) {
  108. $locale = $this->getLocale();
  109. }
  110. if (!isset($this->catalogues[$locale])) {
  111. $this->loadCatalogue($locale);
  112. }
  113. return strtr($this->selector->choose($this->catalogues[$locale]->get($id, $domain), (int) $number, $locale), $parameters);
  114. }
  115. protected function loadCatalogue($locale)
  116. {
  117. $this->catalogues[$locale] = new MessageCatalogue($locale);
  118. if (isset($this->resources[$locale])) {
  119. foreach ($this->resources[$locale] as $resource) {
  120. if (!isset($this->loaders[$resource[0]])) {
  121. throw new \RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0]));
  122. }
  123. $this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]));
  124. }
  125. }
  126. $this->optimizeCatalogue($locale);
  127. }
  128. protected function optimizeCatalogue($locale)
  129. {
  130. if (strlen($locale) > 3) {
  131. $fallback = substr($locale, 0, -strlen(strrchr($locale, '_')));
  132. } else {
  133. $fallback = $this->fallbackLocale;
  134. }
  135. if (!$fallback) {
  136. return;
  137. }
  138. if (!isset($this->catalogues[$fallback])) {
  139. $this->loadCatalogue($fallback);
  140. }
  141. $this->catalogues[$locale]->addFallbackCatalogue($this->catalogues[$fallback]);
  142. }
  143. }