Issues (83)

Twig/TranslationExtension.php (1 issue)

1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[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 Translation\Bundle\Twig;
13
14
use Symfony\Component\Translation\TranslatorBagInterface;
15
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
0 ignored issues
show
The type Symfony\Component\Translation\TranslatorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Symfony\Contracts\Translation\TranslatorInterface;
17
use Translation\Bundle\Twig\Visitor\DefaultApplyingNodeVisitor;
18
use Translation\Bundle\Twig\Visitor\NormalizingNodeVisitor;
19
use Translation\Bundle\Twig\Visitor\RemovingNodeVisitor;
20
use Twig\Extension\AbstractExtension;
21
use Twig\TwigFilter;
22
23
/**
24
 * @author Johannes M. Schmitt <[email protected]>
25
 * @author Tobias Nyholm <[email protected]>
26
 */
27
final class TranslationExtension extends AbstractExtension
28
{
29
    /**
30
     * @var TranslatorInterface|TranslatorBagInterface
31
     */
32
    private $translator;
33
34
    /**
35
     * @var bool
36
     */
37
    private $debug;
38
39 3
    public function __construct($translator, bool $debug = false)
40
    {
41
        // The TranslatorInterface has been deprecated in favor of Symfony\Contracts\Translation\TranslatorInterface in sf4.2.
42
        // Use this class to type hint event & remove the following condition once sf ^4.2 become the minimum supported version.
43
        // @see https://github.com/symfony/symfony/blob/master/UPGRADE-4.2.md#translation
44 3
        if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
45
            throw new \InvalidArgumentException('Cannot deal with given translator.');
46
        }
47
48 3
        $this->translator = $translator;
49 3
        $this->debug = $debug;
50 3
    }
51
52 3
    public function getFilters(): array
53
    {
54
        return [
55 3
            new TwigFilter('desc', [$this, 'desc']),
56 3
            new TwigFilter('meaning', [$this, 'meaning']),
57
        ];
58
    }
59
60 3
    public function getNodeVisitors(): array
61
    {
62
        $visitors = [
63 3
            new NormalizingNodeVisitor(),
64 3
            new RemovingNodeVisitor(),
65
        ];
66
67 3
        if ($this->debug) {
68 1
            $visitors[] = new DefaultApplyingNodeVisitor();
69
        }
70
71 3
        return $visitors;
72
    }
73
74
    public function transchoiceWithDefault(string $message, string $defaultMessage, int $count, array $arguments = [], ?string $domain = null, ?string $locale = null): string
75
    {
76
        if (null === $domain) {
77
            $domain = 'messages';
78
        }
79
80
        if (false === $this->translator->getCatalogue($locale)->defines($message, $domain)) {
81
            return $this->translator->transChoice($defaultMessage, $count, \array_merge(['%count%' => $count], $arguments), $domain, $locale);
82
        }
83
84
        return $this->translator->transChoice($message, $count, \array_merge(['%count%' => $count], $arguments), $domain, $locale);
85
    }
86
87
    /**
88
     * @param mixed $v
89
     *
90
     * @return mixed
91
     */
92
    public function desc($v)
93
    {
94
        return $v;
95
    }
96
97
    /**
98
     * @param mixed $v
99
     *
100
     * @return mixed
101
     */
102
    public function meaning($v)
103
    {
104
        return $v;
105
    }
106
107
    public function getName(): string
108
    {
109
        return 'php-translation';
110
    }
111
}
112