|
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\Translator; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
15
|
|
|
use Symfony\Component\Translation\MessageCatalogueInterface; |
|
16
|
|
|
use Symfony\Component\Translation\TranslatorBagInterface; |
|
17
|
|
|
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface; |
|
|
|
|
|
|
18
|
|
|
use Symfony\Contracts\Translation\LocaleAwareInterface; |
|
19
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface as NewTranslatorInterface; |
|
20
|
|
|
use Translation\Bundle\EditInPlace\ActivatorInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Custom Translator for HTML rendering only (output `<x-trans>` tags). |
|
24
|
|
|
* |
|
25
|
|
|
* @author Damien Alexandre <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
final class EditInPlaceTranslator implements TranslatorInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var LegacyTranslatorInterface|NewTranslatorInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $translator; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var ActivatorInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $activator; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var RequestStack |
|
41
|
|
|
*/ |
|
42
|
|
|
private $requestStack; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* $translator param can't be type hinted as we have to deal with both LegacyTranslatorInterface & NewTranslatorInterface. |
|
46
|
|
|
* Once we won't support sf ^3.4 anymore, we will be able to type hint $translator with NewTranslatorInterface. |
|
47
|
|
|
* |
|
48
|
|
|
* @param LegacyTranslatorInterface|NewTranslatorInterface $translator |
|
49
|
|
|
*/ |
|
50
|
4 |
|
public function __construct($translator, ActivatorInterface $activator, RequestStack $requestStack) |
|
51
|
|
|
{ |
|
52
|
4 |
|
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof LocaleAwareInterface) { |
|
53
|
1 |
|
throw new \InvalidArgumentException('The given translator must implements LocaleAwareInterface.'); |
|
54
|
|
|
} |
|
55
|
3 |
|
if (!$translator instanceof TranslatorBagInterface) { |
|
56
|
|
|
throw new \InvalidArgumentException('The given translator must implements TranslatorBagInterface.'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
$this->translator = $translator; |
|
60
|
3 |
|
$this->activator = $activator; |
|
61
|
3 |
|
$this->requestStack = $requestStack; |
|
62
|
3 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @see Translator::getCatalogue |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getCatalogue($locale = null): MessageCatalogueInterface |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->translator->getCatalogue($locale); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @see Translator::trans |
|
74
|
|
|
*/ |
|
75
|
3 |
|
public function trans($id, array $parameters = [], $domain = null, $locale = null): ?string |
|
76
|
|
|
{ |
|
77
|
3 |
|
$original = $this->translator->trans($id, $parameters, $domain, $locale); |
|
78
|
3 |
|
if (!$this->activator->checkRequest($this->requestStack->getMasterRequest())) { |
|
79
|
1 |
|
return $original; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
$plain = $this->translator->trans($id, [], $domain, $locale); |
|
83
|
|
|
|
|
84
|
2 |
|
if (null === $domain) { |
|
85
|
2 |
|
$domain = 'messages'; |
|
86
|
|
|
} |
|
87
|
2 |
|
if (null === $locale) { |
|
88
|
2 |
|
$locale = $this->translator->getLocale(); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// Render all data in the translation tag required to allow in-line translation |
|
92
|
2 |
|
return \sprintf('<x-trans data-key="%s|%s" data-value="%s" data-plain="%s" data-domain="%s" data-locale="%s">%s</x-trans>', |
|
93
|
2 |
|
$domain, |
|
94
|
2 |
|
$id, |
|
95
|
2 |
|
\htmlspecialchars($original), |
|
96
|
2 |
|
\htmlspecialchars($plain), |
|
97
|
2 |
|
$domain, |
|
98
|
2 |
|
$locale, |
|
99
|
2 |
|
$original |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @see Translator::trans |
|
105
|
|
|
*/ |
|
106
|
|
|
public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null): ?string |
|
107
|
|
|
{ |
|
108
|
|
|
if (!$this->activator->checkRequest($this->requestStack->getMasterRequest())) { |
|
109
|
|
|
return $this->translator->transChoice($id, $number, $parameters, $domain, $locale); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$parameters = \array_merge([ |
|
113
|
|
|
'%count%' => $number, |
|
114
|
|
|
], $parameters); |
|
115
|
|
|
|
|
116
|
|
|
return $this->trans($id, $parameters, $domain, $locale); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @see Translator::trans |
|
121
|
|
|
*/ |
|
122
|
|
|
public function setLocale($locale): void |
|
123
|
|
|
{ |
|
124
|
|
|
$this->translator->setLocale($locale); |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @see Translator::trans |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getLocale(): string |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->translator->getLocale(); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths