4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation
;
14 use Symfony\Component\Translation\Exception\InvalidArgumentException
;
17 * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
19 class DataCollectorTranslator
implements TranslatorInterface
, TranslatorBagInterface
21 const MESSAGE_DEFINED
= 0;
22 const MESSAGE_MISSING
= 1;
23 const MESSAGE_EQUALS_FALLBACK
= 2;
26 * @var TranslatorInterface|TranslatorBagInterface
33 private $messages = array();
36 * @param TranslatorInterface $translator The translator must implement TranslatorBagInterface
38 public function __construct(TranslatorInterface
$translator)
40 if (!$translator instanceof TranslatorBagInterface
) {
41 throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', get_class($translator)));
44 $this->translator
= $translator;
50 public function trans($id, array $parameters = array(), $domain = null, $locale = null)
52 $trans = $this->translator
->trans($id, $parameters, $domain, $locale);
53 $this->collectMessage($locale, $domain, $id, $trans, $parameters);
61 public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
63 $trans = $this->translator
->transChoice($id, $number, $parameters, $domain, $locale);
64 $this->collectMessage($locale, $domain, $id, $trans, $parameters, $number);
72 public function setLocale($locale)
74 $this->translator
->setLocale($locale);
80 public function getLocale()
82 return $this->translator
->getLocale();
88 public function getCatalogue($locale = null)
90 return $this->translator
->getCatalogue($locale);
94 * Gets the fallback locales.
96 * @return array $locales The fallback locales
98 public function getFallbackLocales()
100 if ($this->translator
instanceof Translator ||
method_exists($this->translator
, 'getFallbackLocales')) {
101 return $this->translator
->getFallbackLocales();
108 * Passes through all unknown calls onto the translator object.
110 public function __call($method, $args)
112 return call_user_func_array(array($this->translator
, $method), $args);
118 public function getCollectedMessages()
120 return $this->messages
;
124 * @param string|null $locale
125 * @param string|null $domain
127 * @param string $translation
128 * @param array|null $parameters
129 * @param int|null $number
131 private function collectMessage($locale, $domain, $id, $translation, $parameters = array(), $number = null)
133 if (null === $domain) {
134 $domain = 'messages';
138 $catalogue = $this->translator
->getCatalogue($locale);
139 $locale = $catalogue->getLocale();
140 if ($catalogue->defines($id, $domain)) {
141 $state = self
::MESSAGE_DEFINED
;
142 } elseif ($catalogue->has($id, $domain)) {
143 $state = self
::MESSAGE_EQUALS_FALLBACK
;
145 $fallbackCatalogue = $catalogue->getFallbackCatalogue();
146 while ($fallbackCatalogue) {
147 if ($fallbackCatalogue->defines($id, $domain)) {
148 $locale = $fallbackCatalogue->getLocale();
152 $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
155 $state = self
::MESSAGE_MISSING
;
158 $this->messages
[] = array(
162 'translation' => $translation,
163 'parameters' => $parameters,
164 'transChoiceNumber' => $number,