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 GettextTranslator.php |
33
|
|
|
* |
34
|
|
|
* The gettext translator class |
35
|
|
|
* |
36
|
|
|
* @package Platine\Lang\Translator |
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\Translator; |
48
|
|
|
|
49
|
|
|
use Platine\Lang\Configuration; |
50
|
|
|
use Platine\Lang\Exception\LocaleNotSupportedException; |
51
|
|
|
use Platine\Lang\Storage\StorageInterface; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @class GettextTranslator |
55
|
|
|
* @package Platine\Lang\Translator |
56
|
|
|
*/ |
57
|
|
|
class GettextTranslator extends BaseTranslator |
58
|
|
|
{ |
59
|
|
|
/** |
60
|
|
|
* The default domain |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected string $domain; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The current locale |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
protected string $locale; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The locale encoding |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
protected string $encoding; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inhereitdoc} |
79
|
|
|
*/ |
80
|
|
|
public function __construct( |
81
|
|
|
?Configuration $config = null, |
82
|
|
|
?StorageInterface $storage = null |
83
|
|
|
) { |
84
|
|
|
parent::__construct($config, $storage); |
85
|
|
|
|
86
|
|
|
$this->domain = $this->storage->getDomain(); |
87
|
|
|
$this->encoding = $this->storage->getEncoding(); |
88
|
|
|
|
89
|
|
|
$locale = $this->storage->getLocale(); |
90
|
|
|
$this->setLocale($locale); |
91
|
|
|
$this->setDomain($this->config->get('domain')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inhereitdoc} |
96
|
|
|
*/ |
97
|
|
|
public function setLocale(string $locale): self |
98
|
|
|
{ |
99
|
|
|
if ($this->isLocaleSupported($locale) === false) { |
100
|
|
|
throw new LocaleNotSupportedException(sprintf( |
101
|
|
|
'Locale [%s] is not supported', |
102
|
|
|
$locale |
103
|
|
|
)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$localeText = $locale . '.' . $this->getEncoding(); |
107
|
|
|
putenv('LC_ALL=' . $localeText); |
108
|
|
|
setlocale(LC_ALL, $localeText); |
109
|
|
|
|
110
|
|
|
parent::setLocale($locale); |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inhereitdoc} |
117
|
|
|
*/ |
118
|
|
|
public function isLocaleSupported(string $locale): bool |
119
|
|
|
{ |
120
|
|
|
return in_array($locale, $this->locales()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inhereitdoc} |
125
|
|
|
*/ |
126
|
|
|
public function __toString(): string |
127
|
|
|
{ |
128
|
|
|
return $this->getLocale(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inhereitdoc} |
133
|
|
|
*/ |
134
|
|
|
public function getEncoding(): string |
135
|
|
|
{ |
136
|
|
|
return $this->encoding; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inhereitdoc} |
141
|
|
|
*/ |
142
|
|
|
public function setEncoding(string $encoding): self |
143
|
|
|
{ |
144
|
|
|
$this->encoding = $encoding; |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inhereitdoc} |
151
|
|
|
*/ |
152
|
|
|
public function setDomain(string $domain): self |
153
|
|
|
{ |
154
|
|
|
parent::setDomain($domain); |
155
|
|
|
|
156
|
|
|
bindtextdomain($domain, $this->config->get('translation_path')); |
157
|
|
|
bind_textdomain_codeset($domain, $this->getEncoding()); |
158
|
|
|
|
159
|
|
|
$this->domain = textdomain($domain); |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inhereitdoc} |
166
|
|
|
*/ |
167
|
|
|
public function addDomain(string $domain, ?string $path = null): self |
168
|
|
|
{ |
169
|
|
|
parent::addDomain($domain, $path); |
170
|
|
|
|
171
|
|
|
$domains = $this->storage->getDomains(); |
172
|
|
|
|
173
|
|
|
bindtextdomain($domain, $domains[$domain]); |
174
|
|
|
bind_textdomain_codeset($domain, $this->getEncoding()); |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inhereitdoc} |
181
|
|
|
*/ |
182
|
|
|
public function tr(string $message, mixed $args = []): string |
183
|
|
|
{ |
184
|
|
|
$translation = gettext($message); |
185
|
|
|
|
186
|
|
|
if (!empty($args)) { |
187
|
|
|
if (!is_array($args)) { |
188
|
|
|
$args = array_slice(func_get_args(), 1); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$translation = vsprintf($translation, $args); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $translation; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inhereitdoc} |
199
|
|
|
*/ |
200
|
|
|
public function trd( |
201
|
|
|
string $message, |
202
|
|
|
string $domain, |
203
|
|
|
mixed $args = [] |
204
|
|
|
): string { |
205
|
|
|
$translation = dgettext($domain, $message); |
206
|
|
|
|
207
|
|
|
if (!empty($args)) { |
208
|
|
|
if (!is_array($args)) { |
209
|
|
|
$args = array_slice(func_get_args(), 2); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$translation = vsprintf($translation, $args); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $translation; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* {@inhereitdoc} |
220
|
|
|
*/ |
221
|
|
|
public function trdp( |
222
|
|
|
string $singular, |
223
|
|
|
string $plural, |
224
|
|
|
int $count, |
225
|
|
|
string $domain, |
226
|
|
|
mixed $args = [] |
227
|
|
|
): string { |
228
|
|
|
$translation = dngettext($domain, $singular, $plural, $count); |
229
|
|
|
|
230
|
|
|
if (!empty($args)) { |
231
|
|
|
if (!is_array($args)) { |
232
|
|
|
$args = array_slice(func_get_args(), 4); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$translation = vsprintf($translation, $args); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $translation; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* {@inhereitdoc} |
243
|
|
|
*/ |
244
|
|
|
public function trp( |
245
|
|
|
string $singular, |
246
|
|
|
string $plural, |
247
|
|
|
int $count, |
248
|
|
|
mixed $args = [] |
249
|
|
|
): string { |
250
|
|
|
|
251
|
|
|
$translation = ngettext($singular, $plural, $count); |
252
|
|
|
|
253
|
|
|
if (!empty($args)) { |
254
|
|
|
if (!is_array($args)) { |
255
|
|
|
$args = array_slice(func_get_args(), 3); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$translation = vsprintf($translation, $args); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return $translation; |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|