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
30 private $messages = array();
33 * @param TranslatorInterface $translator The translator must implement TranslatorBagInterface
35 public function __construct(TranslatorInterface $translator)
37 if (!$translator instanceof TranslatorBagInterface) {
38 throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', \get_class($translator)));
41 $this->translator = $translator;
47 public function trans($id, array $parameters = array(), $domain = null, $locale = null)
49 $trans = $this->translator->trans($id, $parameters, $domain, $locale);
50 $this->collectMessage($locale, $domain, $id, $trans, $parameters);
58 public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
60 $trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
61 $this->collectMessage($locale, $domain, $id, $trans, $parameters, $number);
69 public function setLocale($locale)
71 $this->translator->setLocale($locale);
77 public function getLocale()
79 return $this->translator->getLocale();
85 public function getCatalogue($locale = null)
87 return $this->translator->getCatalogue($locale);
91 * Gets the fallback locales.
93 * @return array $locales The fallback locales
95 public function getFallbackLocales()
97 if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
98 return $this->translator->getFallbackLocales();
105 * Passes through all unknown calls onto the translator object.
107 public function __call($method, $args)
109 return \call_user_func_array(array($this->translator, $method), $args);
115 public function getCollectedMessages()
117 return $this->messages;
121 * @param string|null $locale
122 * @param string|null $domain
124 * @param string $translation
125 * @param array|null $parameters
126 * @param int|null $number
128 private function collectMessage($locale, $domain, $id, $translation, $parameters = array(), $number = null)
130 if (null === $domain) {
131 $domain = 'messages';
135 $catalogue = $this->translator->getCatalogue($locale);
136 $locale = $catalogue->getLocale();
137 if ($catalogue->defines($id, $domain)) {
138 $state = self::MESSAGE_DEFINED;
139 } elseif ($catalogue->has($id, $domain)) {
140 $state = self::MESSAGE_EQUALS_FALLBACK;
142 $fallbackCatalogue = $catalogue->getFallbackCatalogue();
143 while ($fallbackCatalogue) {
144 if ($fallbackCatalogue->defines($id, $domain)) {
145 $locale = $fallbackCatalogue->getLocale();
149 $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
152 $state = self::MESSAGE_MISSING;
155 $this->messages[] = array(
159 'translation' => $translation,
160 'parameters' => $parameters,
161 'transChoiceNumber' => $number,