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\TranslatorInterface; |
16
|
|
|
use Symfony\Component\Translation\TranslatorBagInterface; |
17
|
|
|
use Translation\Bundle\EditInPlace\ActivatorInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Custom Translator for HTML rendering only (output `<x-trans>` tags). |
21
|
|
|
* |
22
|
|
|
* @author Damien Alexandre <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class EditInPlaceTranslator implements TranslatorInterface, TranslatorBagInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var TranslatorInterface|\Symfony\Component\Translation\Translator |
28
|
|
|
*/ |
29
|
|
|
private $translator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ActivatorInterface |
33
|
|
|
*/ |
34
|
|
|
private $activator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var RequestStack |
38
|
|
|
*/ |
39
|
|
|
private $requestStack; |
40
|
|
|
|
41
|
|
|
public function __construct(TranslatorInterface $translator, ActivatorInterface $activator, RequestStack $requestStack) |
42
|
|
|
{ |
43
|
|
|
$this->translator = $translator; |
44
|
|
|
$this->activator = $activator; |
45
|
|
|
$this->requestStack = $requestStack; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @see Translator::getCatalogue |
50
|
|
|
*/ |
51
|
|
|
public function getCatalogue($locale = null) |
52
|
|
|
{ |
53
|
|
|
return $this->translator->getCatalogue($locale); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @see Translator::trans |
58
|
|
|
*/ |
59
|
|
|
public function trans($id, array $parameters = [], $domain = null, $locale = null) |
60
|
|
|
{ |
61
|
|
|
if (!$this->activator->checkRequest($this->requestStack->getMasterRequest())) { |
62
|
|
|
return $this->translator->trans($id, $parameters, $domain, $locale); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (null === $domain) { |
66
|
|
|
$domain = 'messages'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$original = $this->translator->trans($id, $parameters, $domain, $locale); |
70
|
|
|
|
71
|
|
|
// todo add data-value="" or data-type with real content, add parameters, domain... |
72
|
|
|
return sprintf('<x-trans data-key="%s|%s">%s</x-trans>', |
73
|
|
|
$domain, |
74
|
|
|
$id, |
75
|
|
|
$original |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @see Translator::trans |
81
|
|
|
*/ |
82
|
|
|
public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null) |
83
|
|
|
{ |
84
|
|
|
if (!$this->activator->checkRequest($this->requestStack->getMasterRequest())) { |
85
|
|
|
return $this->translator->transChoice($id, $number, $parameters, $domain, $locale); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$parameters = array_merge([ |
89
|
|
|
'%count%' => $number, |
90
|
|
|
], $parameters); |
91
|
|
|
|
92
|
|
|
return $this->trans($id, $parameters, $domain, $locale); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @see Translator::trans |
97
|
|
|
*/ |
98
|
|
|
public function setLocale($locale) |
99
|
|
|
{ |
100
|
|
|
$this->translator->setLocale($locale); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @see Translator::trans |
105
|
|
|
*/ |
106
|
|
|
public function getLocale() |
107
|
|
|
{ |
108
|
|
|
return $this->translator->getLocale(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: