1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Lang |
5
|
|
|
* |
6
|
|
|
* Platine Lang is a translation library with extensible translator and storage |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine Lang |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @file Lang.php |
33
|
|
|
* |
34
|
|
|
* The translator main class |
35
|
|
|
* |
36
|
|
|
* @package Platine\Lang |
37
|
|
|
* @author Platine Developers Team |
38
|
|
|
* @copyright Copyright (c) 2020 |
39
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
40
|
|
|
* @link https://www.platine-php.com |
41
|
|
|
* @version 1.0.0 |
42
|
|
|
* @filesource |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
declare(strict_types=1); |
46
|
|
|
|
47
|
|
|
namespace Platine\Lang; |
48
|
|
|
|
49
|
|
|
use Platine\Lang\Translator\TranslatorInterface; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @class Lang |
53
|
|
|
* @package Platine\Lang |
54
|
|
|
*/ |
55
|
|
|
class Lang |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* The translator instance |
59
|
|
|
* @var TranslatorInterface |
60
|
|
|
*/ |
61
|
|
|
protected TranslatorInterface $translator; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Create new instance |
65
|
|
|
* @param TranslatorInterface $translator |
66
|
|
|
*/ |
67
|
|
|
public function __construct(TranslatorInterface $translator) |
68
|
|
|
{ |
69
|
|
|
$this->translator = $translator; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return the translator instance |
74
|
|
|
* @return TranslatorInterface |
75
|
|
|
*/ |
76
|
|
|
public function getTranslator(): TranslatorInterface |
77
|
|
|
{ |
78
|
|
|
return $this->translator; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inhereitdoc} |
83
|
|
|
* @see TranslatorInterface::getEncoding |
84
|
|
|
*/ |
85
|
|
|
public function getEncoding(): string |
86
|
|
|
{ |
87
|
|
|
return $this->translator->getEncoding(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inhereitdoc} |
92
|
|
|
* @see TranslatorInterface::setEncoding |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function setEncoding(string $encoding): self |
96
|
|
|
{ |
97
|
|
|
$this->translator->setEncoding($encoding); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inhereitdoc} |
104
|
|
|
* @see TranslatorInterface::getLocale |
105
|
|
|
*/ |
106
|
|
|
public function getLocale(): string |
107
|
|
|
{ |
108
|
|
|
return $this->translator->getLocale(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inhereitdoc} |
113
|
|
|
* @see TranslatorInterface::setLocale |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
|
|
public function setLocale(string $locale): self |
117
|
|
|
{ |
118
|
|
|
if ($locale !== $this->getLocale()) { |
119
|
|
|
$this->translator->setLocale($locale); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the language portion of the locale |
127
|
|
|
* (example: en_EN returns en) |
128
|
|
|
* @param string|null $locale |
129
|
|
|
* @return string|null |
130
|
|
|
*/ |
131
|
|
|
public function getLocaleLanguage(?string $locale = null): ?string |
132
|
|
|
{ |
133
|
|
|
if ($locale === null) { |
134
|
|
|
$locale = $this->getLocale(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$locales = explode('_', $locale); |
138
|
|
|
|
139
|
|
|
if (is_array($locales) && isset($locales[0])) { |
140
|
|
|
return $locales[0]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inhereitdoc} |
148
|
|
|
* @see TranslatorInterface::setDomain |
149
|
|
|
* @return $this |
150
|
|
|
*/ |
151
|
|
|
public function setDomain(string $domain): self |
152
|
|
|
{ |
153
|
|
|
$this->translator->setDomain($domain); |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inhereitdoc} |
160
|
|
|
* @see TranslatorInterface::getDomain |
161
|
|
|
*/ |
162
|
|
|
public function getDomain(): string |
163
|
|
|
{ |
164
|
|
|
return $this->translator->getDomain(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inhereitdoc} |
169
|
|
|
*/ |
170
|
|
|
public function addDomain(string $domain, ?string $path = null): self |
171
|
|
|
{ |
172
|
|
|
$this->translator->addDomain($domain, $path); |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Return the list of domain |
179
|
|
|
* @return array<string, string> |
180
|
|
|
*/ |
181
|
|
|
public function getDomains(): array |
182
|
|
|
{ |
183
|
|
|
return $this->translator->getDomains(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inhereitdoc} |
188
|
|
|
* @see TranslatorInterface::locales |
189
|
|
|
* @return array<int, string> |
190
|
|
|
*/ |
191
|
|
|
public function locales(): array |
192
|
|
|
{ |
193
|
|
|
return $this->translator->locales(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inhereitdoc} |
198
|
|
|
* @see TranslatorInterface::isLocaleSupported |
199
|
|
|
*/ |
200
|
|
|
public function isLocaleSupported(string $locale): bool |
201
|
|
|
{ |
202
|
|
|
return $this->translator->isLocaleSupported($locale); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inhereitdoc} |
207
|
|
|
* @see TranslatorInterface::tr |
208
|
|
|
* @param string $message |
209
|
|
|
* @param mixed $args |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
|
|
public function tr(string $message, mixed $args = []): string |
213
|
|
|
{ |
214
|
|
|
if (!empty($args) && !is_array($args)) { |
215
|
|
|
$args = array_slice(func_get_args(), 1); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $this->translator->tr($message, $args); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* {@inhereitdoc} |
223
|
|
|
* @see TranslatorInterface::trp |
224
|
|
|
* @param string $singular |
225
|
|
|
* @param string $plural |
226
|
|
|
* @param int $count |
227
|
|
|
* @param mixed $args |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
public function trp( |
231
|
|
|
string $singular, |
232
|
|
|
string $plural, |
233
|
|
|
int $count, |
234
|
|
|
mixed $args = [] |
235
|
|
|
): string { |
236
|
|
|
if (!empty($args) && !is_array($args)) { |
237
|
|
|
$args = array_slice(func_get_args(), 3); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $this->translator->trp( |
241
|
|
|
$singular, |
242
|
|
|
$plural, |
243
|
|
|
$count, |
244
|
|
|
$args |
245
|
|
|
); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* {@inhereitdoc} |
250
|
|
|
* @see TranslatorInterface::trd |
251
|
|
|
* @param string $message |
252
|
|
|
* @param string $domain |
253
|
|
|
* @param mixed $args |
254
|
|
|
* @return string |
255
|
|
|
*/ |
256
|
|
|
public function trd( |
257
|
|
|
string $message, |
258
|
|
|
string $domain, |
259
|
|
|
mixed $args = [] |
260
|
|
|
): string { |
261
|
|
|
return $this->translator->trd( |
262
|
|
|
$message, |
263
|
|
|
$domain, |
264
|
|
|
$args |
265
|
|
|
); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* {@inhereitdoc} |
270
|
|
|
* @see TranslatorInterface::trdp |
271
|
|
|
* @param string $singular |
272
|
|
|
* @param string $plural |
273
|
|
|
* @param int $count |
274
|
|
|
* @param string $domain |
275
|
|
|
* @param mixed $args |
276
|
|
|
* @return string |
277
|
|
|
*/ |
278
|
|
|
public function trdp( |
279
|
|
|
string $singular, |
280
|
|
|
string $plural, |
281
|
|
|
int $count, |
282
|
|
|
string $domain, |
283
|
|
|
mixed $args = [] |
284
|
|
|
): string { |
285
|
|
|
return $this->translator->trdp( |
286
|
|
|
$singular, |
287
|
|
|
$plural, |
288
|
|
|
$count, |
289
|
|
|
$domain, |
290
|
|
|
$args |
291
|
|
|
); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|