| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |