Aucune description

DataCollectorTranslator.php 4.4KB

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