comparison vendor/symfony/translation/DataCollectorTranslator.php @ 0:4c8ae668cc8c

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