Debian Codegraph @

lib/3rdParty/composer/symfony/translation/LoggingTranslator.php

raw symbols SCIP
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
12 namespace Symfony\Component\Translation;
13
14 use Psr\Log\LoggerInterface;
15 use Symfony\Contracts\Translation\LocaleAwareInterface;
16 use Symfony\Contracts\Translation\TranslatorInterface;
17
18 /**
19 * @author Abdellatif Ait boudad <[email protected]>
20 */
21 class LoggingTranslator implements TranslatorInterface, TranslatorBagInterface, LocaleAwareInterface
22 {
23 public function __construct(
24 private TranslatorInterface&TranslatorBagInterface&LocaleAwareInterface $translator,
25 private LoggerInterface $logger,
26 ) {
27 }
28
29 public function trans(?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
30 {
31 $trans = $this->translator->trans($id = (string) $id, $parameters, $domain, $locale);
32 $this->log($id, $domain, $locale);
33
34 return $trans;
35 }
36
37 public function setLocale(string $locale): void
38 {
39 $prev = $this->translator->getLocale();
40 $this->translator->setLocale($locale);
41 if ($prev === $locale) {
42 return;
43 }
44
45 $this->logger->debug(\sprintf('The locale of the translator has changed from "%s" to "%s".', $prev, $locale));
46 }
47
48 public function getLocale(): string
49 {
50 return $this->translator->getLocale();
51 }
52
53 public function getCatalogue(?string $locale = null): MessageCatalogueInterface
54 {
55 return $this->translator->getCatalogue($locale);
56 }
57
58 public function getCatalogues(): array
59 {
60 return $this->translator->getCatalogues();
61 }
62
63 /**
64 * Gets the fallback locales.
65 */
66 public function getFallbackLocales(): array
67 {
68 if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
69 return $this->translator->getFallbackLocales();
70 }
71
72 return [];
73 }
74
75 public function __call(string $method, array $args): mixed
76 {
77 return $this->translator->{$method}(...$args);
78 }
79
80 /**
81 * Logs for missing translations.
82 */
83 private function log(string $id, ?string $domain, ?string $locale): void
84 {
85 $domain ??= 'messages';
86
87 $catalogue = $this->translator->getCatalogue($locale);
88 if ($catalogue->defines($id, $domain)) {
89 return;
90 }
91
92 if ($catalogue->has($id, $domain)) {
93 $this->logger->debug('Translation use fallback catalogue.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]);
94 } else {
95 $this->logger->warning('Translation not found.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]);
96 }
97 }
98 }