Hoa central
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
Hoa\Translate\Gettext Class Reference
Inheritance diagram for Hoa\Translate\Gettext:

Public Member Functions

 __construct (Stream\IStream\In $stream)
 
 _ ($message)
 
 _n ($message, $n)
 
 isBigEndian ()
 
 getRevision ()
 
 getHeaders ()
 
- Public Member Functions inherited from Hoa\Translate\Translate
 __construct (Stream\IStream\In $stream)
 
 getStream ()
 
 getData ()
 
 getIterator ()
 
 _ ($message)
 
 _n ($message, $n)
 

Static Public Member Functions

static unpackHeaders ($headers)
 

Protected Member Functions

 setEndianness ()
 
 unpack ()
 
 _read ($bytes, $pointer=1)
 
 setRevision ($revision)
 
 setHeaders (Array $headers)
 
- Protected Member Functions inherited from Hoa\Translate\Translate
 setStream (Stream\IStream\In $stream)
 
 setData (Array $data)
 

Protected Attributes

 $_bigEndian = false
 
 $_revision = 0
 
 $_headers = []
 
 $_plural = null
 
- Protected Attributes inherited from Hoa\Translate\Translate
 $_stream = null
 
 $_data = []
 

Static Protected Attributes

static $_pluralFunctions = []
 

Detailed Description

Class .

GetText format reader.

Definition at line 49 of file Gettext.php.

Constructor & Destructor Documentation

Hoa\Translate\Gettext::__construct ( Stream\IStream\In  $stream)

Constructor.

Parameters
\Hoa\Stream\IStream\In$streamStream.
Returns
void

Definition at line 94 of file Gettext.php.

95  {
96  parent::__construct($stream);
97 
98  if (!($stream instanceof Stream\IStream\Pointable)) {
99  throw new Exception(
100  'Stream %s must also be pointable.',
101  0,
102  $stream->getStreamName()
103  );
104  }
105 
106  $stream->seek(0);
107  $this->setEndianness();
108  $this->unpack();
109 
110  return;
111  }

Here is the call graph for this function:

Member Function Documentation

Hoa\Translate\Gettext::_ (   $message)

Get translation for regular message.

Parameters
string$messageMessage.
mixed… Parameters.
Returns
string

Definition at line 279 of file Gettext.php.

280  {
281  if (!isset($this->_data[$message])) {
282  return $message;
283  }
284 
285  $parameters = func_get_args();
286  array_shift($parameters);
287 
288  if (false === $out = @vsprintf($this->_data[$message], $parameters)) {
289  return $message;
290  }
291 
292  return $out;
293  }
Hoa\Translate\Gettext::_n (   $message,
  $n 
)

Get translation for plural messages. Messages are concatenated by NUL (\0), or \0 or ^ They can be escaped by .

Parameters
string$messageMessage.
int$nn (to select the plural).
mixed… Parameters.
Returns
string
Exceptions

Definition at line 306 of file Gettext.php.

307  {
308  if (false === $this->_plural) {
309  return $message;
310  }
311 
312  $n = max(1, $n);
313  $parameters = array_slice(func_get_args(), 2);
314  $message = preg_replace('#(?<!\\\)(\\\0|\^@)#', "\0", $message);
315  $message = preg_replace('#\\\(\\\0|\^@)#', '\1', $message);
316 
317  if (!isset($this->_data[$message])) {
318  return $message;
319  }
320 
321  $plurals = explode("\0", $this->_data[$message]);
322 
323  if (!isset($this->_headers['Plural-Forms'])) {
324  $this->_plural = false;
325 
326  return $message;
327  }
328 
329  if (null === $this->_plural) {
330  $pluralForms = $this->_headers['Plural-Forms'];
331 
332  if (false === preg_match('#^nplurals=(\d+);\s*plural=(.+?);?$#s', $pluralForms, $matches)) {
333  return $plurals[0];
334  }
335 
336  list(, $nplurals, $plural) = $matches;
337  $_plural = preg_replace('#n#', '$n', $plural);
338  $_plural = preg_replace('#\s+#', '', $_plural);
339  $id = '__hoa_translate_gettext_' . md5($_plural);
340 
341  if (!isset(static::$_pluralFunctions[$id])) {
342  $handle = @create_function(
343  '$n',
344  'return (int) (' . $_plural . ');'
345  );
346 
347  if (false === $handle) {
348  throw new Exception(
349  'Something is wrong with your plurial form %s.',
350  2,
351  $plural
352  );
353  }
354 
355  static::$_pluralFunctions[$id] = $handle;
356  }
357 
358  $this->_plural = $id;
359  }
360 
361  $pluralFunction = static::$_pluralFunctions[$this->_plural];
362  $i = $pluralFunction($n);
363 
364  if (!isset($plurals[$i])) {
365  return $plurals[0];
366  }
367 
368  if (false === $out = @vsprintf($plurals[$i], $parameters)) {
369  return $plurals[$i];
370  }
371 
372  return $out;
373  }
Hoa\Translate\Gettext::_read (   $bytes,
  $pointer = 1 
)
protected

Read bytes on the stream.

Parameters
int$bytesNumber of bytes to read.
int$pointerPointer/index if read an array.
Returns
mixed

Definition at line 255 of file Gettext.php.

256  {
257  $buffer = $this->getStream()->read(4 * $bytes);
258 
259  if (false === $this->isBigEndian()) {
260  $out = unpack('V' . $bytes, $buffer);
261  } else {
262  $out = unpack('N' . $bytes, $buffer);
263  }
264 
265  if (isset($out[$pointer])) {
266  return $out[$pointer];
267  }
268 
269  return $out;
270  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Translate\Gettext::getHeaders ( )

Get headers.

Returns
array

Definition at line 450 of file Gettext.php.

451  {
452  return $this->_headers;
453  }

Here is the caller graph for this function:

Hoa\Translate\Gettext::getRevision ( )

Get file format revision.

Returns
int

Definition at line 404 of file Gettext.php.

405  {
406  return $this->_revision;
407  }
Hoa\Translate\Gettext::isBigEndian ( )

Check if the file is in big endian.

Returns
bool

Definition at line 380 of file Gettext.php.

381  {
382  return $this->_bigEndian;
383  }

Here is the caller graph for this function:

Hoa\Translate\Gettext::setEndianness ( )
protected

Compute endianness.

Returns
void

Definition at line 118 of file Gettext.php.

119  {
120  $magicNumber = unpack('V1', $this->getStream()->read(4));
121 
122  switch (dechex($magicNumber[1])) {
123  case '950412de':
124  $this->_bigEndian = false;
125 
126  break;
127 
128  case 'de120495':
129  $this->_bigEndian = true;
130 
131  break;
132 
133  default:
134  throw new Exception(
135  '%s is not a GNU MO file.',
136  1,
137  $this->getStream()->getStreamName()
138  );
139  }
140 
141  return;
142  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Translate\Gettext::setHeaders ( Array  $headers)
protected

Set headers.

Parameters
array$headersHeaders.
Returns
array

Definition at line 437 of file Gettext.php.

438  {
439  $old = $this->_headers;
440  $this->_headers = $headers;
441 
442  return $old;
443  }

Here is the caller graph for this function:

Hoa\Translate\Gettext::setRevision (   $revision)
protected

Set file format revision.

Parameters
int$revisionFile format revision.
Returns
int

Definition at line 391 of file Gettext.php.

392  {
393  $old = $this->_revision;
394  $this->_revision = $revision;
395 
396  return $old;
397  }

Here is the caller graph for this function:

Hoa\Translate\Gettext::unpack ( )
protected

Unpack data/messages.

Returns
void

Schema:

    byte˼
          +------------------------------------------+
       0  | magic number = 0x950412de                |
          |                                          |
       4  | file format revision = 0                 |
          |                                          |
       8  | number of strings                        |  == N
          |                                          |
      12  | offset of table with original strings    |  == O
          |                                          |
      16  | offset of table with translation strings |  == T
          |                                          |
      20  | size of hashing table                    |  == S
          |                                          |
      24  | offset of hashing table                  |  == H
          |                                          ˼
          .                                          .
          .    (possibly more entries later)         .
          .                                          .
          |                                          |
       O  | length & offset 0th string  ----------------.
   O + 8  | length & offset 1st string  ------------------.
           ...                                    ...   | |

O + ((N-1)*8)| length & offset (N-1)th string | | | | | | | T | length & offset 0th translation ------------—.˼ T + 8 | length & offset 1st translation --------------—. ... ... | | | | T + ((N-1)*8)| length & offset (N-1)th translation | | | | | | | | | | | H | start hash table | | | | | ... ... | | | | H + S * 4 | end hash table | | | | | | | | | | | | NUL terminated 0th string <-------------—' | | | | | | | | | NUL terminated 1st string <---------------—' | | | | | | ... ... | | | | | | | NUL terminated 0th translation <------------—' | | | | | NUL terminated 1st translation <--------------—' | | ... ... | | +---------------------------------------—+

Definition at line 149 of file Gettext.php.

150  {
203  $stream = $this->getStream();
204  $out = &$this->_data;
205  $this->setRevision($this->_read(1));
206  $notsh = [
207  'N' => $this->_read(1),
208  'O' => $this->_read(1),
209  'T' => $this->_read(1),
210  'S' => $this->_read(1),
211  'H' => $this->_read(1)
212  ];
213 
214  $stream->seek($notsh['O']);
215  $originalStringOffset = $this->_read(2 * $notsh['N'], null);
216 
217  $stream->seek($notsh['T']);
218  $translateStringOffset = $this->_read(2 * $notsh['N'], null);
219 
220  for ($e = 0, $max = $notsh['N']; $e < $max; ++$e) {
221  if (0 === $originalStringOffset[$e * 2 + 1]) {
222  $_headers = $this->getHeaders();
223 
224  if (!empty($_headers)) {
225  continue;
226  }
227 
228  $stream->seek($translateStringOffset[$e * 2 + 2]);
229  $this->setHeaders(
230  $this->unpackHeaders(
231  $stream->read($translateStringOffset[$e * 2 + 1])
232  )
233  );
234 
235  continue;
236  }
237 
238  $stream->seek($originalStringOffset[$e * 2 + 2]);
239  $key = $stream->read($originalStringOffset[$e * 2 + 1]);
240 
241  $stream->seek($translateStringOffset[$e * 2 + 2]);
242  $out[$key] = $stream->read($translateStringOffset[$e * 2 + 1]);
243  }
244 
245  return;
246  }
_read($bytes, $pointer=1)
Definition: Gettext.php:255
setHeaders(Array $headers)
Definition: Gettext.php:437
setRevision($revision)
Definition: Gettext.php:391
static unpackHeaders($headers)
Definition: Gettext.php:415

Here is the call graph for this function:

Here is the caller graph for this function:

static Hoa\Translate\Gettext::unpackHeaders (   $headers)
static

Unpack headers from a string.

Parameters
string$headersHeaders.
Returns
array

Definition at line 415 of file Gettext.php.

416  {
417  $out = [];
418 
419  foreach (explode("\n", $headers) as $line) {
420  if (empty($line)) {
421  continue;
422  }
423 
424  list($type, $value) = explode(':', $line, 2);
425  $out[trim($type)] = trim($value);
426  }
427 
428  return $out;
429  }

Here is the caller graph for this function:

Member Data Documentation

Hoa\Translate\Gettext::$_bigEndian = false
protected

Definition at line 56 of file Gettext.php.

Hoa\Translate\Gettext::$_headers = []
protected

Definition at line 70 of file Gettext.php.

Hoa\Translate\Gettext::$_plural = null
protected

Definition at line 77 of file Gettext.php.

Hoa\Translate\Gettext::$_pluralFunctions = []
staticprotected

Definition at line 84 of file Gettext.php.

Hoa\Translate\Gettext::$_revision = 0
protected

Definition at line 63 of file Gettext.php.


The documentation for this class was generated from the following file: