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