DataCollectorTranslator.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\Component\Translation;
  11. use Symfony\Component\Translation\Exception\InvalidArgumentException;
  12. /**
  13. * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
  14. */
  15. class DataCollectorTranslator implements TranslatorInterface, TranslatorBagInterface
  16. {
  17. const MESSAGE_DEFINED = 0;
  18. const MESSAGE_MISSING = 1;
  19. const MESSAGE_EQUALS_FALLBACK = 2;
  20. /**
  21. * @var TranslatorInterface|TranslatorBagInterface
  22. */
  23. private $translator;
  24. private $messages = array();
  25. /**
  26. * @param TranslatorInterface $translator The translator must implement TranslatorBagInterface
  27. */
  28. public function __construct(TranslatorInterface $translator)
  29. {
  30. if (!$translator instanceof TranslatorBagInterface) {
  31. throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', get_class($translator)));
  32. }
  33. $this->translator = $translator;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function trans($id, array $parameters = array(), $domain = null, $locale = null)
  39. {
  40. $trans = $this->translator->trans($id, $parameters, $domain, $locale);
  41. $this->collectMessage($locale, $domain, $id, $trans, $parameters);
  42. return $trans;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
  48. {
  49. $trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
  50. $this->collectMessage($locale, $domain, $id, $trans, $parameters, $number);
  51. return $trans;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function setLocale($locale)
  57. {
  58. $this->translator->setLocale($locale);
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function getLocale()
  64. {
  65. return $this->translator->getLocale();
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getCatalogue($locale = null)
  71. {
  72. return $this->translator->getCatalogue($locale);
  73. }
  74. /**
  75. * Gets the fallback locales.
  76. *
  77. * @return array $locales The fallback locales
  78. */
  79. public function getFallbackLocales()
  80. {
  81. if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
  82. return $this->translator->getFallbackLocales();
  83. }
  84. return array();
  85. }
  86. /**
  87. * Passes through all unknown calls onto the translator object.
  88. */
  89. public function __call($method, $args)
  90. {
  91. return call_user_func_array(array($this->translator, $method), $args);
  92. }
  93. /**
  94. * @return array
  95. */
  96. public function getCollectedMessages()
  97. {
  98. return $this->messages;
  99. }
  100. /**
  101. * @param string|null $locale
  102. * @param string|null $domain
  103. * @param string $id
  104. * @param string $translation
  105. * @param array|null $parameters
  106. * @param int|null $number
  107. */
  108. private function collectMessage($locale, $domain, $id, $translation, $parameters = array(), $number = null)
  109. {
  110. if (null === $domain) {
  111. $domain = 'messages';
  112. }
  113. $id = (string) $id;
  114. $catalogue = $this->translator->getCatalogue($locale);
  115. $locale = $catalogue->getLocale();
  116. if ($catalogue->defines($id, $domain)) {
  117. $state = self::MESSAGE_DEFINED;
  118. } elseif ($catalogue->has($id, $domain)) {
  119. $state = self::MESSAGE_EQUALS_FALLBACK;
  120. $fallbackCatalogue = $catalogue->getFallbackCatalogue();
  121. while ($fallbackCatalogue) {
  122. if ($fallbackCatalogue->defines($id, $domain)) {
  123. $locale = $fallbackCatalogue->getLocale();
  124. break;
  125. }
  126. $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
  127. }
  128. } else {
  129. $state = self::MESSAGE_MISSING;
  130. }
  131. $this->messages[] = array(
  132. 'locale' => $locale,
  133. 'domain' => $domain,
  134. 'id' => $id,
  135. 'translation' => $translation,
  136. 'parameters' => $parameters,
  137. 'transChoiceNumber' => $number,
  138. 'state' => $state,
  139. );
  140. }
  141. }