blob: 2a08b0960489e6160dc1134f7105a6aa36b610dd [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?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
12namespace Symfony\Component\Translation;
13
14use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
15use Symfony\Component\Translation\Exception\InvalidArgumentException;
16use Symfony\Contracts\Translation\LocaleAwareInterface;
17use Symfony\Contracts\Translation\TranslatorInterface;
18
19/**
20 * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
21 */
22class DataCollectorTranslator implements TranslatorInterface, TranslatorBagInterface, LocaleAwareInterface, WarmableInterface
23{
24 public const MESSAGE_DEFINED = 0;
25 public const MESSAGE_MISSING = 1;
26 public const MESSAGE_EQUALS_FALLBACK = 2;
27
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020028 private $translator;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010029 private array $messages = [];
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020030
31 /**
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010032 * @param TranslatorInterface&TranslatorBagInterface&LocaleAwareInterface $translator
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020033 */
34 public function __construct(TranslatorInterface $translator)
35 {
36 if (!$translator instanceof TranslatorBagInterface || !$translator instanceof LocaleAwareInterface) {
37 throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and LocaleAwareInterface.', get_debug_type($translator)));
38 }
39
40 $this->translator = $translator;
41 }
42
43 /**
44 * {@inheritdoc}
45 */
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010046 public function trans(?string $id, array $parameters = [], string $domain = null, string $locale = null): string
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020047 {
48 $trans = $this->translator->trans($id = (string) $id, $parameters, $domain, $locale);
49 $this->collectMessage($locale, $domain, $id, $trans, $parameters);
50
51 return $trans;
52 }
53
54 /**
55 * {@inheritdoc}
56 */
57 public function setLocale(string $locale)
58 {
59 $this->translator->setLocale($locale);
60 }
61
62 /**
63 * {@inheritdoc}
64 */
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010065 public function getLocale(): string
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020066 {
67 return $this->translator->getLocale();
68 }
69
70 /**
71 * {@inheritdoc}
72 */
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010073 public function getCatalogue(string $locale = null): MessageCatalogueInterface
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020074 {
75 return $this->translator->getCatalogue($locale);
76 }
77
78 /**
79 * {@inheritdoc}
80 */
81 public function getCatalogues(): array
82 {
83 return $this->translator->getCatalogues();
84 }
85
86 /**
87 * {@inheritdoc}
88 *
89 * @return string[]
90 */
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010091 public function warmUp(string $cacheDir): array
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020092 {
93 if ($this->translator instanceof WarmableInterface) {
94 return (array) $this->translator->warmUp($cacheDir);
95 }
96
97 return [];
98 }
99
100 /**
101 * Gets the fallback locales.
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200102 */
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100103 public function getFallbackLocales(): array
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200104 {
105 if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
106 return $this->translator->getFallbackLocales();
107 }
108
109 return [];
110 }
111
112 /**
113 * Passes through all unknown calls onto the translator object.
114 */
115 public function __call(string $method, array $args)
116 {
117 return $this->translator->{$method}(...$args);
118 }
119
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100120 public function getCollectedMessages(): array
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200121 {
122 return $this->messages;
123 }
124
125 private function collectMessage(?string $locale, ?string $domain, string $id, string $translation, ?array $parameters = [])
126 {
127 if (null === $domain) {
128 $domain = 'messages';
129 }
130
131 $catalogue = $this->translator->getCatalogue($locale);
132 $locale = $catalogue->getLocale();
133 $fallbackLocale = null;
134 if ($catalogue->defines($id, $domain)) {
135 $state = self::MESSAGE_DEFINED;
136 } elseif ($catalogue->has($id, $domain)) {
137 $state = self::MESSAGE_EQUALS_FALLBACK;
138
139 $fallbackCatalogue = $catalogue->getFallbackCatalogue();
140 while ($fallbackCatalogue) {
141 if ($fallbackCatalogue->defines($id, $domain)) {
142 $fallbackLocale = $fallbackCatalogue->getLocale();
143 break;
144 }
145 $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
146 }
147 } else {
148 $state = self::MESSAGE_MISSING;
149 }
150
151 $this->messages[] = [
152 'locale' => $locale,
153 'fallbackLocale' => $fallbackLocale,
154 'domain' => $domain,
155 'id' => $id,
156 'translation' => $translation,
157 'parameters' => $parameters,
158 'state' => $state,
159 'transChoiceNumber' => isset($parameters['%count%']) && is_numeric($parameters['%count%']) ? $parameters['%count%'] : null,
160 ];
161 }
162}