DataCollectorTranslator.php 4.0 KB

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