-
Notifications
You must be signed in to change notification settings - Fork 26
/
Translator.php
374 lines (317 loc) · 9.76 KB
/
Translator.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
declare(strict_types=1);
/*
Copyright (c) 2003, 2009 Danilo Segan <danilo@kvota.net>.
Copyright (c) 2005 Nico Kaiser <nico@siriux.net>
Copyright (c) 2016 Michal Čihař <michal@cihar.com>
This file is part of MoTranslator.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
namespace PhpMyAdmin\MoTranslator;
use PhpMyAdmin\MoTranslator\Cache\CacheInterface;
use PhpMyAdmin\MoTranslator\Cache\GetAllInterface;
use PhpMyAdmin\MoTranslator\Cache\InMemoryCache;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Throwable;
use function array_key_exists;
use function count;
use function explode;
use function is_numeric;
use function ltrim;
use function preg_replace;
use function rtrim;
use function sprintf;
use function str_contains;
use function str_starts_with;
use function stripos;
use function strtolower;
use function substr;
use function trim;
/**
* Provides a simple gettext replacement that works independently from
* the system's gettext abilities.
* It can read MO files and use them for translating strings.
*
* It caches ll strings and translations to speed up the string lookup.
*/
class Translator
{
/**
* None error.
*/
public const ERROR_NONE = 0;
/**
* File does not exist.
*/
public const ERROR_DOES_NOT_EXIST = 1;
/**
* File has bad magic number.
*/
public const ERROR_BAD_MAGIC = 2;
/**
* Error while reading file, probably too short.
*/
public const ERROR_READING = 3;
/**
* Big endian mo file magic bytes.
*/
public const MAGIC_BE = "\x95\x04\x12\xde";
/**
* Little endian mo file magic bytes.
*/
public const MAGIC_LE = "\xde\x12\x04\x95";
/**
* Parse error code (0 if no error).
*/
public int $error = self::ERROR_NONE;
/**
* Cache header field for plural forms.
*/
private string|null $pluralEquation = null;
/**
* Evaluator for plurals
*/
private ExpressionLanguage|null $pluralExpression = null;
/**
* number of plurals
*/
private int|null $pluralCount = null;
private CacheInterface $cache;
/** @param CacheInterface|string|null $cache Mo file to load (null for no file) or a CacheInterface implementation */
public function __construct(CacheInterface|string|null $cache)
{
if (! $cache instanceof CacheInterface) {
$cache = new InMemoryCache(new MoParser($cache));
}
$this->cache = $cache;
}
/**
* Translates a string.
*
* @param string $msgid String to be translated
*
* @return string translated string (or original, if not found)
*/
public function gettext(string $msgid): string
{
return $this->cache->get($msgid);
}
/**
* Check if a string is translated.
*
* @param string $msgid String to be checked
*/
public function exists(string $msgid): bool
{
return $this->cache->has($msgid);
}
/**
* Sanitize plural form expression for use in ExpressionLanguage.
*
* @param string $expr Expression to sanitize
*
* @return string sanitized plural form expression
*/
public static function sanitizePluralExpression(string $expr): string
{
// Parse equation
$expr = explode(';', $expr);
$expr = count($expr) >= 2 ? $expr[1] : $expr[0];
$expr = trim(strtolower($expr));
// Strip plural prefix
if (str_starts_with($expr, 'plural')) {
$expr = ltrim(substr($expr, 6));
}
// Strip equals
if (str_starts_with($expr, '=')) {
$expr = ltrim(substr($expr, 1));
}
// Cleanup from unwanted chars
$expr = preg_replace('@[^n0-9:\(\)\?=!<>/%&| ]@', '', $expr);
return (string) $expr;
}
/**
* Extracts number of plurals from plurals form expression.
*
* @param string $expr Expression to process
*
* @return int Total number of plurals
*/
public static function extractPluralCount(string $expr): int
{
$parts = explode(';', $expr, 2);
$nplurals = explode('=', trim($parts[0]), 2);
if (strtolower(rtrim($nplurals[0])) !== 'nplurals') {
return 1;
}
if (count($nplurals) === 1) {
return 1;
}
return (int) $nplurals[1];
}
/**
* Parse full PO header and extract only plural forms line.
*
* @param string $header Gettext header
*
* @return string verbatim plural form header field
*/
public static function extractPluralsForms(string $header): string
{
$headers = explode("\n", $header);
$expr = 'nplurals=2; plural=n == 1 ? 0 : 1;';
foreach ($headers as $header) {
if (stripos($header, 'Plural-Forms:') !== 0) {
continue;
}
$expr = substr($header, 13);
}
return $expr;
}
/**
* Get possible plural forms from MO header.
*
* @return string plural form header
*/
private function getPluralForms(): string
{
// lets assume message number 0 is header
// this is true, right?
// cache header field for plural forms
if ($this->pluralEquation === null) {
$header = $this->cache->get('');
$expr = self::extractPluralsForms($header);
$this->pluralEquation = self::sanitizePluralExpression($expr);
$this->pluralCount = self::extractPluralCount($expr);
}
return $this->pluralEquation;
}
/**
* Detects which plural form to take.
*
* @param int $n count of objects
*
* @return int array index of the right plural form
*/
private function selectString(int $n): int
{
if ($this->pluralExpression === null) {
$this->pluralExpression = new ExpressionLanguage();
}
try {
$evaluatedPlural = $this->pluralExpression->evaluate($this->getPluralForms(), ['n' => $n]);
$plural = is_numeric($evaluatedPlural) ? (int) $evaluatedPlural : 0;
} catch (Throwable) {
$plural = 0;
}
if ($plural >= $this->pluralCount) {
$plural = $this->pluralCount - 1;
}
return $plural;
}
/**
* Plural version of gettext.
*
* @param string $msgid Single form
* @param string $msgidPlural Plural form
* @param int $number Number of objects
*
* @return string translated plural form
*/
public function ngettext(string $msgid, string $msgidPlural, int $number): string
{
// this should contains all strings separated by NULLs
$key = $msgid . "\u{0}" . $msgidPlural;
if (! $this->cache->has($key)) {
return $number !== 1 ? $msgidPlural : $msgid;
}
$result = $this->cache->get($key);
// find out the appropriate form
$select = $this->selectString($number);
$list = explode("\u{0}", $result);
if (array_key_exists($select, $list)) {
return $list[$select];
}
return $list[0];
}
/**
* Translate with context.
*
* @param string $msgctxt Context
* @param string $msgid String to be translated
*
* @return string translated plural form
*/
public function pgettext(string $msgctxt, string $msgid): string
{
$key = $msgctxt . "\u{4}" . $msgid;
$ret = $this->gettext($key);
if ($ret === $key) {
return $msgid;
}
return $ret;
}
/**
* Plural version of pgettext.
*
* @param string $msgctxt Context
* @param string $msgid Single form
* @param string $msgidPlural Plural form
* @param int $number Number of objects
*
* @return string translated plural form
*/
public function npgettext(string $msgctxt, string $msgid, string $msgidPlural, int $number): string
{
$key = $msgctxt . "\u{4}" . $msgid;
$ret = $this->ngettext($key, $msgidPlural, $number);
if (str_contains($ret, "\u{4}")) {
return $msgid;
}
return $ret;
}
/**
* Set translation in place
*
* @param string $msgid String to be set
* @param string $msgstr Translation
*/
public function setTranslation(string $msgid, string $msgstr): void
{
$this->cache->set($msgid, $msgstr);
}
/**
* Set the translations
*
* @param array<string,string> $translations The translations "key => value" array
*/
public function setTranslations(array $translations): void
{
$this->cache->setAll($translations);
}
/**
* Get the translations
*
* @return array<string,string> The translations "key => value" array
*/
public function getTranslations(): array
{
if ($this->cache instanceof GetAllInterface) {
return $this->cache->getAll();
}
throw new CacheException(sprintf(
"Cache '%s' does not support getting translations",
$this->cache::class,
));
}
}