Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Translation;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Translation\Exception\InvalidArgumentException;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
|
Chris@0
|
18 */
|
Chris@0
|
19 class DataCollectorTranslator implements TranslatorInterface, TranslatorBagInterface
|
Chris@0
|
20 {
|
Chris@0
|
21 const MESSAGE_DEFINED = 0;
|
Chris@0
|
22 const MESSAGE_MISSING = 1;
|
Chris@0
|
23 const MESSAGE_EQUALS_FALLBACK = 2;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * @var TranslatorInterface|TranslatorBagInterface
|
Chris@0
|
27 */
|
Chris@0
|
28 private $translator;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * @var array
|
Chris@0
|
32 */
|
Chris@0
|
33 private $messages = array();
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * @param TranslatorInterface $translator The translator must implement TranslatorBagInterface
|
Chris@0
|
37 */
|
Chris@0
|
38 public function __construct(TranslatorInterface $translator)
|
Chris@0
|
39 {
|
Chris@0
|
40 if (!$translator instanceof TranslatorBagInterface) {
|
Chris@0
|
41 throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', get_class($translator)));
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 $this->translator = $translator;
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * {@inheritdoc}
|
Chris@0
|
49 */
|
Chris@0
|
50 public function trans($id, array $parameters = array(), $domain = null, $locale = null)
|
Chris@0
|
51 {
|
Chris@0
|
52 $trans = $this->translator->trans($id, $parameters, $domain, $locale);
|
Chris@0
|
53 $this->collectMessage($locale, $domain, $id, $trans, $parameters);
|
Chris@0
|
54
|
Chris@0
|
55 return $trans;
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * {@inheritdoc}
|
Chris@0
|
60 */
|
Chris@0
|
61 public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
|
Chris@0
|
62 {
|
Chris@0
|
63 $trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
|
Chris@0
|
64 $this->collectMessage($locale, $domain, $id, $trans, $parameters, $number);
|
Chris@0
|
65
|
Chris@0
|
66 return $trans;
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * {@inheritdoc}
|
Chris@0
|
71 */
|
Chris@0
|
72 public function setLocale($locale)
|
Chris@0
|
73 {
|
Chris@0
|
74 $this->translator->setLocale($locale);
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 /**
|
Chris@0
|
78 * {@inheritdoc}
|
Chris@0
|
79 */
|
Chris@0
|
80 public function getLocale()
|
Chris@0
|
81 {
|
Chris@0
|
82 return $this->translator->getLocale();
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 /**
|
Chris@0
|
86 * {@inheritdoc}
|
Chris@0
|
87 */
|
Chris@0
|
88 public function getCatalogue($locale = null)
|
Chris@0
|
89 {
|
Chris@0
|
90 return $this->translator->getCatalogue($locale);
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 /**
|
Chris@0
|
94 * Gets the fallback locales.
|
Chris@0
|
95 *
|
Chris@0
|
96 * @return array $locales The fallback locales
|
Chris@0
|
97 */
|
Chris@0
|
98 public function getFallbackLocales()
|
Chris@0
|
99 {
|
Chris@0
|
100 if ($this->translator instanceof Translator) {
|
Chris@0
|
101 return $this->translator->getFallbackLocales();
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 return array();
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@0
|
107 /**
|
Chris@0
|
108 * Passes through all unknown calls onto the translator object.
|
Chris@0
|
109 */
|
Chris@0
|
110 public function __call($method, $args)
|
Chris@0
|
111 {
|
Chris@0
|
112 return call_user_func_array(array($this->translator, $method), $args);
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 /**
|
Chris@0
|
116 * @return array
|
Chris@0
|
117 */
|
Chris@0
|
118 public function getCollectedMessages()
|
Chris@0
|
119 {
|
Chris@0
|
120 return $this->messages;
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 /**
|
Chris@0
|
124 * @param string|null $locale
|
Chris@0
|
125 * @param string|null $domain
|
Chris@0
|
126 * @param string $id
|
Chris@0
|
127 * @param string $translation
|
Chris@0
|
128 * @param array|null $parameters
|
Chris@0
|
129 * @param int|null $number
|
Chris@0
|
130 */
|
Chris@0
|
131 private function collectMessage($locale, $domain, $id, $translation, $parameters = array(), $number = null)
|
Chris@0
|
132 {
|
Chris@0
|
133 if (null === $domain) {
|
Chris@0
|
134 $domain = 'messages';
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@0
|
137 $id = (string) $id;
|
Chris@0
|
138 $catalogue = $this->translator->getCatalogue($locale);
|
Chris@0
|
139 $locale = $catalogue->getLocale();
|
Chris@0
|
140 if ($catalogue->defines($id, $domain)) {
|
Chris@0
|
141 $state = self::MESSAGE_DEFINED;
|
Chris@0
|
142 } elseif ($catalogue->has($id, $domain)) {
|
Chris@0
|
143 $state = self::MESSAGE_EQUALS_FALLBACK;
|
Chris@0
|
144
|
Chris@0
|
145 $fallbackCatalogue = $catalogue->getFallbackCatalogue();
|
Chris@0
|
146 while ($fallbackCatalogue) {
|
Chris@0
|
147 if ($fallbackCatalogue->defines($id, $domain)) {
|
Chris@0
|
148 $locale = $fallbackCatalogue->getLocale();
|
Chris@0
|
149 break;
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
|
Chris@0
|
153 }
|
Chris@0
|
154 } else {
|
Chris@0
|
155 $state = self::MESSAGE_MISSING;
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 $this->messages[] = array(
|
Chris@0
|
159 'locale' => $locale,
|
Chris@0
|
160 'domain' => $domain,
|
Chris@0
|
161 'id' => $id,
|
Chris@0
|
162 'translation' => $translation,
|
Chris@0
|
163 'parameters' => $parameters,
|
Chris@0
|
164 'transChoiceNumber' => $number,
|
Chris@0
|
165 'state' => $state,
|
Chris@0
|
166 );
|
Chris@0
|
167 }
|
Chris@0
|
168 }
|