comparison vendor/symfony/translation/DataCollectorTranslator.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
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 private $messages = array();
31
32 /**
33 * @param TranslatorInterface $translator The translator must implement TranslatorBagInterface
34 */
35 public function __construct(TranslatorInterface $translator)
36 {
37 if (!$translator instanceof TranslatorBagInterface) {
38 throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', get_class($translator)));
39 }
40
41 $this->translator = $translator;
42 }
43
44 /**
45 * {@inheritdoc}
46 */
47 public function trans($id, array $parameters = array(), $domain = null, $locale = null)
48 {
49 $trans = $this->translator->trans($id, $parameters, $domain, $locale);
50 $this->collectMessage($locale, $domain, $id, $trans, $parameters);
51
52 return $trans;
53 }
54
55 /**
56 * {@inheritdoc}
57 */
58 public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
59 {
60 $trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
61 $this->collectMessage($locale, $domain, $id, $trans, $parameters, $number);
62
63 return $trans;
64 }
65
66 /**
67 * {@inheritdoc}
68 */
69 public function setLocale($locale)
70 {
71 $this->translator->setLocale($locale);
72 }
73
74 /**
75 * {@inheritdoc}
76 */
77 public function getLocale()
78 {
79 return $this->translator->getLocale();
80 }
81
82 /**
83 * {@inheritdoc}
84 */
85 public function getCatalogue($locale = null)
86 {
87 return $this->translator->getCatalogue($locale);
88 }
89
90 /**
91 * Gets the fallback locales.
92 *
93 * @return array $locales The fallback locales
94 */
95 public function getFallbackLocales()
96 {
97 if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
98 return $this->translator->getFallbackLocales();
99 }
100
101 return array();
102 }
103
104 /**
105 * Passes through all unknown calls onto the translator object.
106 */
107 public function __call($method, $args)
108 {
109 return call_user_func_array(array($this->translator, $method), $args);
110 }
111
112 /**
113 * @return array
114 */
115 public function getCollectedMessages()
116 {
117 return $this->messages;
118 }
119
120 /**
121 * @param string|null $locale
122 * @param string|null $domain
123 * @param string $id
124 * @param string $translation
125 * @param array|null $parameters
126 * @param int|null $number
127 */
128 private function collectMessage($locale, $domain, $id, $translation, $parameters = array(), $number = null)
129 {
130 if (null === $domain) {
131 $domain = 'messages';
132 }
133
134 $id = (string) $id;
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;
141
142 $fallbackCatalogue = $catalogue->getFallbackCatalogue();
143 while ($fallbackCatalogue) {
144 if ($fallbackCatalogue->defines($id, $domain)) {
145 $locale = $fallbackCatalogue->getLocale();
146 break;
147 }
148
149 $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
150 }
151 } else {
152 $state = self::MESSAGE_MISSING;
153 }
154
155 $this->messages[] = array(
156 'locale' => $locale,
157 'domain' => $domain,
158 'id' => $id,
159 'translation' => $translation,
160 'parameters' => $parameters,
161 'transChoiceNumber' => $number,
162 'state' => $state,
163 );
164 }
165 }