Drupal investigation

DataCollectorTranslator.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. * Gets the fallback locales.
  78. *
  79. * @return array $locales The fallback locales
  80. */
  81. public function getFallbackLocales()
  82. {
  83. if ($this->translator instanceof Translator) {
  84. return $this->translator->getFallbackLocales();
  85. }
  86. return array();
  87. }
  88. /**
  89. * Passes through all unknown calls onto the translator object.
  90. */
  91. public function __call($method, $args)
  92. {
  93. return call_user_func_array(array($this->translator, $method), $args);
  94. }
  95. /**
  96. * @return array
  97. */
  98. public function getCollectedMessages()
  99. {
  100. return $this->messages;
  101. }
  102. /**
  103. * @param string|null $locale
  104. * @param string|null $domain
  105. * @param string $id
  106. * @param string $translation
  107. * @param array|null $parameters
  108. * @param int|null $number
  109. */
  110. private function collectMessage($locale, $domain, $id, $translation, $parameters = array(), $number = null)
  111. {
  112. if (null === $domain) {
  113. $domain = 'messages';
  114. }
  115. $id = (string) $id;
  116. $catalogue = $this->translator->getCatalogue($locale);
  117. $locale = $catalogue->getLocale();
  118. if ($catalogue->defines($id, $domain)) {
  119. $state = self::MESSAGE_DEFINED;
  120. } elseif ($catalogue->has($id, $domain)) {
  121. $state = self::MESSAGE_EQUALS_FALLBACK;
  122. $fallbackCatalogue = $catalogue->getFallbackCatalogue();
  123. while ($fallbackCatalogue) {
  124. if ($fallbackCatalogue->defines($id, $domain)) {
  125. $locale = $fallbackCatalogue->getLocale();
  126. break;
  127. }
  128. $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
  129. }
  130. } else {
  131. $state = self::MESSAGE_MISSING;
  132. }
  133. $this->messages[] = array(
  134. 'locale' => $locale,
  135. 'domain' => $domain,
  136. 'id' => $id,
  137. 'translation' => $translation,
  138. 'parameters' => $parameters,
  139. 'transChoiceNumber' => $number,
  140. 'state' => $state,
  141. );
  142. }
  143. }