|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Charcoal\View\Mustache; |
|
6
|
|
|
|
|
7
|
|
|
use LogicException; |
|
8
|
|
|
|
|
9
|
|
|
// From Mustache |
|
10
|
|
|
use Mustache_LambdaHelper as LambdaHelper; |
|
11
|
|
|
|
|
12
|
|
|
// From 'charcoal-translator' |
|
13
|
|
|
use Charcoal\Translator\Translator; |
|
14
|
|
|
|
|
15
|
|
|
// From 'charcoal-view' |
|
16
|
|
|
use Charcoal\View\Mustache\HelpersInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Translating Mustache Templates |
|
20
|
|
|
*/ |
|
21
|
|
|
class TranslatorHelpers implements HelpersInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Store the translator service. |
|
25
|
|
|
* |
|
26
|
|
|
* @var Translator|null |
|
27
|
|
|
*/ |
|
28
|
|
|
private $translator; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Store the given number to use to find the indice of the message (Mustache tag node). |
|
32
|
|
|
* |
|
33
|
|
|
* This can be a variable name in the context stack or an integer. |
|
34
|
|
|
* |
|
35
|
|
|
* Floats are not supported due to their decimal symbol conflicting |
|
36
|
|
|
* with Mustache's dot-notation. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string|integer|null |
|
39
|
|
|
*/ |
|
40
|
|
|
private $number; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Store the given locale (Mustache tag node). |
|
44
|
|
|
* |
|
45
|
|
|
* This must be an available locale on the Translator. |
|
46
|
|
|
* |
|
47
|
|
|
* @var string|null |
|
48
|
|
|
*/ |
|
49
|
|
|
private $locale; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Store the given domain for the message (Mustache tag node). |
|
53
|
|
|
* |
|
54
|
|
|
* This must be an available domain on the Translator. |
|
55
|
|
|
* |
|
56
|
|
|
* @var string|null |
|
57
|
|
|
*/ |
|
58
|
|
|
private $domain; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param array $data Class Dependencies. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function __construct(array $data = null) |
|
64
|
|
|
{ |
|
65
|
|
|
if (isset($data['translator'])) { |
|
66
|
|
|
$this->setTranslator($data['translator']); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Set the translator service. |
|
72
|
|
|
* |
|
73
|
|
|
* @param Translator $translator The Translator service. |
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function setTranslator(Translator $translator): void |
|
77
|
|
|
{ |
|
78
|
|
|
$this->translator = $translator; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Retrieve the helpers. |
|
83
|
|
|
* |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
|
|
public function toArray(): array |
|
87
|
|
|
{ |
|
88
|
|
|
return [ |
|
89
|
|
|
'_t' => $this, |
|
90
|
|
|
]; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Clear macros. |
|
95
|
|
|
* |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function reset(): void |
|
99
|
|
|
{ |
|
100
|
|
|
$this->number = null; |
|
101
|
|
|
$this->domain = null; |
|
102
|
|
|
$this->locale = null; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Magic: Render the Mustache section. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $text The translation key. |
|
109
|
|
|
* @param LambdaHelper|null $helper For rendering strings in the current context. |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
public function __invoke(string $text, LambdaHelper $helper = null): string |
|
113
|
|
|
{ |
|
114
|
|
|
if ($this->translator) { |
|
115
|
|
|
if ($this->number === null) { |
|
116
|
|
|
$text = $this->translator->trans($text, [], $this->domain, $this->locale); |
|
117
|
|
|
} else { |
|
118
|
|
|
if (!is_numeric($this->number) && is_string($this->number)) { |
|
119
|
|
|
$this->number = $helper->render('{{ '.$this->number.' }}'); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$text = $this->translator->transChoice($text, (int)$this->number, [], $this->domain, $this->locale); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$this->reset(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** @var string Render any Mustache tags in the translation. */ |
|
129
|
|
|
return $helper->render($text); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Magic: Determine if a property is set and is not NULL. |
|
134
|
|
|
* |
|
135
|
|
|
* Required by Mustache. |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $macro A domain, locale, or number. |
|
138
|
|
|
* @return boolean |
|
139
|
|
|
*/ |
|
140
|
|
|
public function __isset(string $macro): bool |
|
141
|
|
|
{ |
|
142
|
|
|
return boolval($macro); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Magic: Process domain, locale, and number. |
|
147
|
|
|
* |
|
148
|
|
|
* Required by Mustache. |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $macro A domain, locale, or number. |
|
151
|
|
|
* @throws LogicException If the macro is unresolved. |
|
152
|
|
|
* @return mixed |
|
153
|
|
|
*/ |
|
154
|
|
|
public function __get(string $macro) |
|
155
|
|
|
{ |
|
156
|
|
|
if (!$this->translator) { |
|
157
|
|
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
if ($this->locale === null && in_array($macro, $this->translator->availableLocales())) { |
|
161
|
|
|
$this->locale = $macro; |
|
162
|
|
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
if ($this->domain === null && in_array($macro, $this->translator->availableDomains())) { |
|
166
|
|
|
$this->domain = $macro; |
|
167
|
|
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
if ($this->number === null) { |
|
171
|
|
|
$this->number = $macro; |
|
172
|
|
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
throw new LogicException(sprintf('Unknown translator macro: %s', $macro)); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: