-
Notifications
You must be signed in to change notification settings - Fork 0
/
Translation.php
253 lines (228 loc) · 7.12 KB
/
Translation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
namespace Charcoal\Translator;
use ArrayAccess;
use Charcoal\Translator\LocalesManager;
use DomainException;
use InvalidArgumentException;
use JsonSerializable;
use Symfony\Component\Translation\TranslatorInterface;
/**
* A translation object holds a localized message in all available locales.
*
* Available locales is provided with a locales manager.
*/
class Translation implements
TranslatableInterface,
ArrayAccess,
JsonSerializable
{
/**
* The object's translations.
*
* Stored as a `[ $lang => $val ]` hash.
*
* @var string[]
*/
private $val = [];
/**
* @var LocalesManager
*/
private $manager;
/**
* @param Translation|array|string $val The translation values.
* @param LocalesManager $manager A LocalesManager instance.
*/
public function __construct($val, LocalesManager $manager)
{
$this->manager = $manager;
$this->setVal($val);
}
/**
* Output the current language's value, when cast to string.
*
* @return string
*/
public function __toString()
{
$lang = $this->manager->currentLocale();
if (isset($this->val[$lang])) {
return $this->val[$lang];
} else {
return '';
}
}
/**
* Get the array of translations in all languages.
*
* @return string[]
*/
public function data()
{
return $this->val;
}
/**
* @param string $lang A language identifier.
* @return boolean
* @see ArrayAccess::offsetExists()
* @throws InvalidArgumentException If array key isn't a string.
*/
public function offsetExists($lang)
{
if (!is_string($lang)) {
throw new InvalidArgumentException(sprintf(
'Invalid language; must be a string, received %s',
(is_object($lang) ? get_class($lang) : gettype($lang))
));
}
return isset($this->val[$lang]);
}
/**
* @param string $lang A language identifier.
* @return string A translated string.
* @see ArrayAccess::offsetGet()
* @throws InvalidArgumentException If array key isn't a string.
* @throws DomainException If the array key is not found.
*/
public function offsetGet($lang)
{
if (!is_string($lang)) {
throw new InvalidArgumentException(sprintf(
'Invalid language; must be a string, received %s',
(is_object($lang) ? get_class($lang) : gettype($lang))
));
}
if (!isset($this->val[$lang])) {
throw new DomainException(sprintf(
'Translation for "%s" is not defined.',
$lang
));
}
return $this->val[$lang];
}
/**
* @param string $lang A language identifier.
* @param string $val A translation value.
* @return void
* @see ArrayAccess::offsetSet()
* @throws InvalidArgumentException If array key isn't a string.
*/
public function offsetSet($lang, $val)
{
if (!is_string($lang)) {
throw new InvalidArgumentException(sprintf(
'Invalid language; must be a string, received %s',
(is_object($lang) ? get_class($lang) : gettype($lang))
));
}
if (!is_string($val)) {
throw new InvalidArgumentException(sprintf(
'Translation must be a string, received %s',
(is_object($val) ? get_class($val) : gettype($val))
));
}
$this->val[$lang] = $val;
}
/**
* @param string $lang A language identifier.
* @return void
* @see ArrayAccess::offsetUnset()
* @throws InvalidArgumentException If array key isn't a string.
*/
public function offsetUnset($lang)
{
if (!is_string($lang)) {
throw new InvalidArgumentException(sprintf(
'Invalid language; must be a string, received %s',
(is_object($lang) ? get_class($lang) : gettype($lang))
));
}
unset($this->val[$lang]);
}
/**
* Retrieve translations that can be serialized natively by json_encode().
*
* @return string[]
* @see JsonSerializable::jsonSerialize()
*/
public function jsonSerialize()
{
return $this->data();
}
/**
* Transform each language's value using a callback.
*
* @param callable $callback The callback function to run for each value.
* The callback takes on the value only.
* @return self
*/
public function sanitize(callable $callback)
{
foreach ($this->val as $lang => $val) {
$this->val[$lang] = call_user_func($callback, $val);
}
return $this;
}
/**
* Transform each language's value, with locale, using a callback.
*
* @param callable $callback The callback function to run for each value.
* The callback takes on two parameters. The value being the first, and the language code second.
* @return self
*/
public function each(callable $callback)
{
foreach ($this->val as $lang => $val) {
$this->val[$lang] = call_user_func($callback, $val, $lang);
}
return $this;
}
/**
* @param TranslatorInterface $translator The translator.
* @param ?string $locale The locale.
* @return string
*/
public function trans(TranslatorInterface $translator, ?string $locale = null)
{
$locale ??= $translator->getLocale();
return $this->val[$locale];
}
/**
* Assign the current translation value(s).
*
* @param Translation|array|string $val The translation value(s).
* Add one or more translation values.
*
* Accept 3 types of arguments:
* - object (TranslationInterface): The data will be copied from the object's.
* - array: All languages available in the array. The format of the array should
* be a hash in the `lang` => `string` format.
* - string: The value will be assigned to the current language.
* @return self
* @throws InvalidArgumentException If language or value are invalid.
*/
private function setVal($val)
{
if ($val instanceof Translation) {
$this->val = $val->data();
} elseif (is_array($val)) {
$this->val = [];
foreach ($val as $lang => $l10n) {
if (!is_string($lang)) {
throw new InvalidArgumentException(sprintf(
'Invalid language; must be a string, received %s',
(is_object($lang) ? get_class($lang) : gettype($lang))
));
}
$this->val[$lang] = (string)$l10n;
}
} elseif (is_string($val)) {
$lang = $this->manager->currentLocale();
$this->val[$lang] = $val;
} else {
throw new InvalidArgumentException(
'Invalid localized value.'
);
}
return $this;
}
}