/
Gettext.php
532 lines (479 loc) · 15.4 KB
/
Gettext.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2014, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace lithium\g11n\catalog\adapter;
use RangeException;
use lithium\core\ConfigException;
use lithium\core\Libraries;
/**
* The `Gettext` class is an adapter for reading and writing PO and MO files without the
* requirement of having the gettext extension enabled or installed. Moreover it doesn't
* require the usage of the non thread safe `setlocale()`.
*
* The adapter works with the directory structure below. The example shows the structure
* for the directory as given by the `'path'` configuration setting. It closely ressembles
* the standard gettext directory structure with a few slight adjustments to the way
* templates are being named.
*
* {{{
* resources/g11n/po
* ├── <locale>
* | ├── LC_MESSAGES
* | | ├── default.po
* | | ├── default.mo
* | | ├── <scope>.po
* | | └── <scope>.mo
* | ├── LC_VALIDATION
* | | └── ...
* | └── ...
* ├── <locale>
* | └── ...
* ├── message_default.pot
* ├── message_<scope>.pot
* ├── validation_default.pot
* ├── validation_<scope>.pot
* └── ...
* }}}
*
* @see lithium\g11n\Locale
* @link http://php.net/setlocale PHP Manual: setlocale()
* @link http://www.gnu.org/software/gettext/manual/gettext.html GNU Gettext Utilities
*/
class Gettext extends \lithium\g11n\catalog\Adapter {
/**
* Magic used for validating the format of a MO file as well as
* detecting if the machine used to create that file was little endian.
*
* @see lithium\g11n\catalog\adapter\Gettext::_parseMo()
* @var float
*/
const MO_LITTLE_ENDIAN_MAGIC = 0x950412de;
/**
* Magic used for validating the format of a MO file as well as
* detecting if the machine used to create that file was big endian.
*
* @see lithium\g11n\catalog\adapter\Gettext::_parseMo()
* @var float
*/
const MO_BIG_ENDIAN_MAGIC = 0xde120495;
/**
* The size of the header of a MO file in bytes.
*
* @see lithium\g11n\catalog\adapter\Gettext::_parseMo()
* @var integer Number of bytes.
*/
const MO_HEADER_SIZE = 28;
/**
* Constructor.
*
* @param array $config Available configuration options are:
* - `'path'`: The path to the directory holding the data.
*/
public function __construct(array $config = array()) {
$defaults = array('path' => null);
parent::__construct($config + $defaults);
}
/**
* Initializer. Checks if the configured path exists.
*
* @return void
* @throws ConfigException
*/
protected function _init() {
parent::_init();
if (!is_dir($this->_config['path'])) {
$message = "Gettext directory does not exist at path `{$this->_config['path']}`.";
throw new ConfigException($message);
}
}
/**
* Reads data.
*
* @param string $category A category.
* @param string $locale A locale identifier.
* @param string $scope The scope for the current operation.
* @return array
*/
public function read($category, $locale, $scope) {
$files = $this->_files($category, $locale, $scope);
foreach ($files as $file) {
$method = '_parse' . ucfirst(pathinfo($file, PATHINFO_EXTENSION));
if (!file_exists($file) || !is_readable($file)) {
continue;
}
$stream = fopen($file, 'rb');
$data = $this->invokeMethod($method, array($stream));
fclose($stream);
if ($data) {
$data['pluralRule'] = array(
'id' => 'pluralRule',
'translated' => function($count) {
return $count !== 1;
}
);
return $data;
}
}
}
/**
* Writes data.
*
* @param string $category A category.
* @param string $locale A locale identifier.
* @param string $scope The scope for the current operation.
* @param array $data The data to write.
* @return boolean
*/
public function write($category, $locale, $scope, array $data) {
$files = $this->_files($category, $locale, $scope);
foreach ($files as $file) {
$method = '_compile' . ucfirst(pathinfo($file, PATHINFO_EXTENSION));
if (!$stream = fopen($file, 'wb')) {
return false;
}
$this->invokeMethod($method, array($stream, $data));
fclose($stream);
}
return true;
}
/**
* Returns absolute paths to files according to configuration.
*
* @param string $category
* @param string $locale
* @param string $scope
* @return array
*/
protected function _files($category, $locale, $scope) {
$path = $this->_config['path'];
$scope = $scope ?: 'default';
if (($pos = strpos($category, 'Template')) !== false) {
$category = substr($category, 0, $pos);
return array("{$path}/{$category}_{$scope}.pot");
}
if ($category === 'message') {
$category = 'messages';
}
$category = strtoupper($category);
return array(
"{$path}/{$locale}/LC_{$category}/{$scope}.mo",
"{$path}/{$locale}/LC_{$category}/{$scope}.po"
);
}
/**
* Parses portable object (PO) format.
*
* This parser sacrifices some features of the reference implementation the
* differences to that implementation are as follows.
* - No support for comments spanning multiple lines.
* - Translator and extracted comments are treated as being the same type.
* - Message IDs are allowed to have other encodings as just US-ASCII.
*
* Items with an empty id are ignored. For more information see `_merge()`.
*
* @param resource $stream
* @return array
*/
protected function _parsePo($stream) {
$defaults = array(
'ids' => array(),
'translated' => null,
'flags' => array(),
'comments' => array(),
'occurrences' => array(),
'context' => null
);
$data = array();
$item = $defaults;
while ($line = fgets($stream)) {
$line = trim($line);
if ($line === '') {
$data = $this->_merge($data, $item);
$item = $defaults;
} elseif (substr($line, 0, 3) === '#~ ') {
$item['flags']['obsolete'] = true;
} elseif (substr($line, 0, 3) === '#, ') {
$item['flags'][substr($line, 3)] = true;
} elseif (substr($line, 0, 3) === '#: ') {
$item['occurrences'][] = array(
'file' => strtok(substr($line, 3), ':'),
'line' => strtok(':')
);
} elseif (substr($line, 0, 3) === '#. ') {
$item['comments'][] = substr($line, 3);
} elseif ($line[0] === '#') {
$item['comments'][] = ltrim(substr($line, 1));
} elseif (substr($line, 0, 7) === 'msgid "') {
$item['ids']['singular'] = substr($line, 7, -1);
} elseif (substr($line, 0, 9) === 'msgctxt "') {
$item['context'] = substr($line, 9, -1);
} elseif (substr($line, 0, 8) === 'msgstr "') {
$item['translated'] = substr($line, 8, -1);
} elseif ($line[0] === '"') {
$continues = isset($item['translated']) ? 'translated' : 'ids';
if (is_array($item[$continues])) {
end($item[$continues]);
$item[$continues][key($item[$continues])] .= substr($line, 1, -1);
} else {
$item[$continues] .= substr($line, 1, -1);
}
} elseif (substr($line, 0, 14) === 'msgid_plural "') {
$item['ids']['plural'] = substr($line, 14, -1);
} elseif (substr($line, 0, 7) === 'msgstr[') {
$item['translated'][(integer) substr($line, 7, 1)] = substr($line, 11, -1);
}
}
return $this->_merge($data, $item);
}
/**
* Parses portable object template (POT) format.
*
* @param resource $stream
* @return array
*/
protected function _parsePot($stream) {
return $this->_parsePo($stream);
}
/**
* Parses machine object (MO) format, independent of the machine's endian it
* was created on. Both 32bit and 64bit systems are supported.
*
* @param resource $stream
* @return array
* @throws RangeException If stream content has an invalid format.
*/
protected function _parseMo($stream) {
$stat = fstat($stream);
if ($stat['size'] < self::MO_HEADER_SIZE) {
throw new RangeException("MO stream content has an invalid format.");
}
$magic = unpack('V1', fread($stream, 4));
$magic = hexdec(substr(dechex(current($magic)), -8));
if ($magic == self::MO_LITTLE_ENDIAN_MAGIC) {
$isBigEndian = false;
} elseif ($magic == self::MO_BIG_ENDIAN_MAGIC) {
$isBigEndian = true;
} else {
throw new RangeException("MO stream content has an invalid format.");
}
$header = array(
'formatRevision' => null,
'count' => null,
'offsetId' => null,
'offsetTranslated' => null,
'sizeHashes' => null,
'offsetHashes' => null
);
foreach ($header as &$value) {
$value = $this->_readLong($stream, $isBigEndian);
}
extract($header);
$data = array();
for ($i = 0; $i < $count; $i++) {
$singularId = $pluralId = null;
$translated = null;
$context = null;
fseek($stream, $offsetId + $i * 8);
$length = $this->_readLong($stream, $isBigEndian);
$offset = $this->_readLong($stream, $isBigEndian);
if ($length < 1) {
continue;
}
fseek($stream, $offset);
$singularId = fread($stream, $length);
if (strpos($singularId, "\000") !== false) {
list($singularId, $pluralId) = explode("\000", $singularId);
}
if (strpos($singularId, "\004") !== false) {
list($context, $singularId) = explode("\004", $singularId);
}
fseek($stream, $offsetTranslated + $i * 8);
$length = $this->_readLong($stream, $isBigEndian);
$offset = $this->_readLong($stream, $isBigEndian);
fseek($stream, $offset);
$translated = fread($stream, $length);
if (strpos($translated, "\000") !== false) {
$translated = explode("\000", $translated);
}
$ids = array('singular' => $singularId, 'plural' => $pluralId);
$data = $this->_merge($data, compact('ids', 'translated', 'context'));
}
return $data;
}
/**
* Reads an unsigned long from stream respecting endianess.
*
* @param resource $stream
* @param boolean $isBigEndian
* @return integer
*/
protected function _readLong($stream, $isBigEndian) {
$result = unpack($isBigEndian ? 'N1' : 'V1', fread($stream, 4));
$result = current($result);
return (integer) substr($result, -8);
}
/**
* Compiles data into portable object (PO) format.
*
* To improve portability accross libraries the header is generated according
* to the format of the output of `xgettext`. This means using the same names for
* placeholders as well as including an empty entry. The empty entry at the
* beginning aids in parsing the file as it _attracts_ the preceding comments and
* following metadata when parsed which could otherwise be mistaken as a continued
* translation. The only difference in the header format is the initial header which
* just features one line of text.
*
* @param resource $stream
* @param array $data
* @return boolean
*/
protected function _compilePo($stream, array $data) {
$output[] = '# This file is distributed under the same license as the PACKAGE package.';
$output[] = '#';
$output[] = 'msgctxt ""';
$output[] = 'msgid ""';
$output[] = 'msgstr ""';
$output[] = '"Project-Id-Version: PACKAGE VERSION\n"';
$output[] = '"POT-Creation-Date: YEAR-MO-DA HO:MI+ZONE\n"';
$output[] = '"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"';
$output[] = '"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"';
$output[] = '"Language-Team: LANGUAGE <EMAIL@ADDRESS>\n"';
$output[] = '"MIME-Version: 1.0\n"';
$output[] = '"Content-Type: text/plain; charset=UTF-8\n"';
$output[] = '"Content-Transfer-Encoding: 8bit\n"';
$output[] = '"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"';
$output[] = '';
$output = implode("\n", $output) . "\n";
fwrite($stream, $output);
foreach ($data as $key => $item) {
$output = array();
$item = $this->_prepareForWrite($item);
foreach ($item['occurrences'] as $occurrence) {
$output[] = "#: {$occurrence['file']}:{$occurrence['line']}";
}
foreach ($item['comments'] as $comment) {
$output[] = "#. {$comment}";
}
foreach ($item['flags'] as $flag => $value) {
$output[] = "#, {$flag}";
}
if (isset($item['context'])) {
$output[] = "msgctxt \"{$item['context']}\"";
}
$output[] = "msgid \"{$item['ids']['singular']}\"";
if (isset($item['ids']['plural'])) {
$output[] = "msgid_plural \"{$item['ids']['plural']}\"";
foreach ((array) $item['translated'] ?: array(null, null) as $key => $value) {
$output[] = "msgstr[{$key}] \"{$value}\"";
}
} else {
if (is_array($item['translated'])) {
$item['translated'] = array_pop($item['translated']);
}
$output[] = "msgstr \"{$item['translated']}\"";
}
$output[] = '';
$output = implode("\n", $output) . "\n";
fwrite($stream, $output);
}
return true;
}
/**
* Compiles data into portable object template (POT) format.
*
* @param resource $stream
* @param array $data
* @return boolean Success.
*/
protected function _compilePot($stream, array $data) {
return $this->_compilePo($stream, $data);
}
/**
* Compiles data into machine object (MO) format.
*
* @param resource $stream
* @param array $data
* @return void
* @todo Determine if needed and implement compiler.
*/
protected function _compileMo($stream, array $data) {}
/**
* Prepares an item before it is being written and escapes fields.
*
* All characters from \000 to \037 (this includes new line and tab characters)
* as well as the backslash (`\`) and the double quote (`"`) are escaped.
*
* Literal Windows CRLFs (`\r\n`) are converted to LFs (`\n`) to improve cross platform
* compatibility. Escaped single quotes (`'`) are unescaped as they should not need to be.
* Double escaped characters are maintained and not escaped once again.
*
* @link http://www.asciitable.com
* @see lithium\g11n\catalog\Adapter::_prepareForWrite()
* @param array $item
* @return array
*/
protected function _prepareForWrite(array $item) {
$filter = function ($value) use (&$filter) {
if (is_array($value)) {
return array_map($filter, $value);
}
$value = strtr($value, array("\\'" => "'", "\\\\" => "\\", "\r\n" => "\n"));
$value = addcslashes($value, "\0..\37\\\"");
return $value;
};
$fields = array('id', 'ids', 'translated', 'context');
foreach ($fields as $field) {
if (isset($item[$field])) {
$item[$field] = $filter($item[$field]);
}
}
if (!isset($item['ids']['singular'])) {
$item['ids']['singular'] = $item['id'];
}
$path = Libraries::get(true, 'path');
if (isset($item['occurrences'])) {
foreach ($item['occurrences'] as &$occurrence) {
$occurrence['file'] = str_replace($path, '', $occurrence['file']);
}
}
return parent::_prepareForWrite($item);
}
/**
* Merges an item into given data and unescapes fields.
*
* Please note that items with an id containing exclusively whitespace characters
* or are empty are **not** being merged. Whitespace characters are space, tab, vertical
* tab, line feed, carriage return and form feed.
*
* @see lithium\g11n\catalog\Adapter::_merge()
* @param array $data Data to merge item into.
* @param array $item Item to merge into $data.
* @return array The merged data.
*/
protected function _merge(array $data, array $item) {
$filter = function ($value) use (&$filter) {
if (is_array($value)) {
return array_map($filter, $value);
}
return stripcslashes($value);
};
$fields = array('id', 'ids', 'translated', 'context');
foreach ($fields as $field) {
if (isset($item[$field])) {
$item[$field] = $filter($item[$field]);
}
}
if (isset($item['ids']['singular'])) {
$item['id'] = $item['ids']['singular'];
}
if (empty($item['id']) || ctype_space($item['id'])) {
return $data;
}
return parent::_merge($data, $item);
}
}
?>