PluginProbe
Metricool – Social media and site statistics / 2.1.1
Metricool – Social media and site statistics v2.1.1
2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
metricool / vendor / _scoped / symfony / translation / LoggingTranslator.php

LoggingTranslator.php in Metricool – Social media and site statistics 2.1.1, at vendor/_scoped/symfony/translation/LoggingTranslator.php

115 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 namespace Metricool\Vendor\Symfony\Component\Translation;
12
13 use Metricool\Vendor\Psr\Log\LoggerInterface;
14 use Metricool\Vendor\Symfony\Component\Translation\Exception\InvalidArgumentException;
15 use Metricool\Vendor\Symfony\Contracts\Translation\LocaleAwareInterface;
16 use Metricool\Vendor\Symfony\Contracts\Translation\TranslatorInterface;
17 /**
18 * @author Abdellatif Ait boudad <[email protected]>
19 */
20 class LoggingTranslator implements TranslatorInterface, TranslatorBagInterface, LocaleAwareInterface
21 {
22 private $translator;
23 private $logger;
24 /**
25 * @param TranslatorInterface&TranslatorBagInterface&LocaleAwareInterface $translator The translator must implement TranslatorBagInterface
26 */
27 public function __construct(TranslatorInterface $translator, LoggerInterface $logger)
28 {
29 if (!$translator instanceof TranslatorBagInterface || !$translator instanceof LocaleAwareInterface) {
30 throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and LocaleAwareInterface.', get_debug_type($translator)));
31 }
32 $this->translator = $translator;
33 $this->logger = $logger;
34 }
35 /**
36 * {@inheritdoc}
37 */
38 public function trans(?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null)
39 {
40 $trans = $this->translator->trans($id = (string) $id, $parameters, $domain, $locale);
41 $this->log($id, $domain, $locale);
42 return $trans;
43 }
44 /**
45 * {@inheritdoc}
46 */
47 public function setLocale(string $locale)
48 {
49 $prev = $this->translator->getLocale();
50 $this->translator->setLocale($locale);
51 if ($prev === $locale) {
52 return;
53 }
54 $this->logger->debug(sprintf('The locale of the translator has changed from "%s" to "%s".', $prev, $locale));
55 }
56 /**
57 * {@inheritdoc}
58 */
59 public function getLocale()
60 {
61 return $this->translator->getLocale();
62 }
63 /**
64 * {@inheritdoc}
65 */
66 public function getCatalogue(?string $locale = null)
67 {
68 return $this->translator->getCatalogue($locale);
69 }
70 /**
71 * {@inheritdoc}
72 */
73 public function getCatalogues(): array
74 {
75 return $this->translator->getCatalogues();
76 }
77 /**
78 * Gets the fallback locales.
79 *
80 * @return array
81 */
82 public function getFallbackLocales()
83 {
84 if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
85 return $this->translator->getFallbackLocales();
86 }
87 return [];
88 }
89 /**
90 * Passes through all unknown calls onto the translator object.
91 */
92 public function __call(string $method, array $args)
93 {
94 return $this->translator->{$method}(...$args);
95 }
96 /**
97 * Logs for missing translations.
98 */
99 private function log(string $id, ?string $domain, ?string $locale)
100 {
101 if (null === $domain) {
102 $domain = 'messages';
103 }
104 $catalogue = $this->translator->getCatalogue($locale);
105 if ($catalogue->defines($id, $domain)) {
106 return;
107 }
108 if ($catalogue->has($id, $domain)) {
109 $this->logger->debug('Translation use fallback catalogue.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]);
110 } else {
111 $this->logger->warning('Translation not found.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]);
112 }
113 }
114 }
115