Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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 Psr\Log\LoggerInterface;
15
use Symfony\Component\Translation\Exception\InvalidArgumentException;
16
use Symfony\Contracts\Translation\LocaleAwareInterface;
17
use Symfony\Contracts\Translation\TranslatorInterface;
18
 
19
/**
20
 * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
21
 */
22
class LoggingTranslator implements TranslatorInterface, TranslatorBagInterface, LocaleAwareInterface
23
{
24
    private TranslatorInterface $translator;
25
    private LoggerInterface $logger;
26
 
27
    /**
28
     * @param TranslatorInterface&TranslatorBagInterface&LocaleAwareInterface $translator The translator must implement TranslatorBagInterface
29
     */
30
    public function __construct(TranslatorInterface $translator, LoggerInterface $logger)
31
    {
32
        if (!$translator instanceof TranslatorBagInterface || !$translator instanceof LocaleAwareInterface) {
33
            throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and LocaleAwareInterface.', get_debug_type($translator)));
34
        }
35
 
36
        $this->translator = $translator;
37
        $this->logger = $logger;
38
    }
39
 
40
    public function trans(?string $id, array $parameters = [], string $domain = null, string $locale = null): string
41
    {
42
        $trans = $this->translator->trans($id = (string) $id, $parameters, $domain, $locale);
43
        $this->log($id, $domain, $locale);
44
 
45
        return $trans;
46
    }
47
 
48
    public function setLocale(string $locale)
49
    {
50
        $prev = $this->translator->getLocale();
51
        $this->translator->setLocale($locale);
52
        if ($prev === $locale) {
53
            return;
54
        }
55
 
56
        $this->logger->debug(sprintf('The locale of the translator has changed from "%s" to "%s".', $prev, $locale));
57
    }
58
 
59
    public function getLocale(): string
60
    {
61
        return $this->translator->getLocale();
62
    }
63
 
64
    public function getCatalogue(string $locale = null): MessageCatalogueInterface
65
    {
66
        return $this->translator->getCatalogue($locale);
67
    }
68
 
69
    public function getCatalogues(): array
70
    {
71
        return $this->translator->getCatalogues();
72
    }
73
 
74
    /**
75
     * Gets the fallback locales.
76
     */
77
    public function getFallbackLocales(): array
78
    {
79
        if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
80
            return $this->translator->getFallbackLocales();
81
        }
82
 
83
        return [];
84
    }
85
 
86
    /**
87
     * Passes through all unknown calls onto the translator object.
88
     */
89
    public function __call(string $method, array $args)
90
    {
91
        return $this->translator->{$method}(...$args);
92
    }
93
 
94
    /**
95
     * Logs for missing translations.
96
     */
97
    private function log(string $id, ?string $domain, ?string $locale)
98
    {
99
        $domain ??= 'messages';
100
 
101
        $catalogue = $this->translator->getCatalogue($locale);
102
        if ($catalogue->defines($id, $domain)) {
103
            return;
104
        }
105
 
106
        if ($catalogue->has($id, $domain)) {
107
            $this->logger->debug('Translation use fallback catalogue.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]);
108
        } else {
109
            $this->logger->warning('Translation not found.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]);
110
        }
111
    }
112
}