ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Gettext\Extractors\Mo Class Reference

Class to get gettext strings from .mo files. More...

+ Inheritance diagram for Gettext\Extractors\Mo:
+ Collaboration diagram for Gettext\Extractors\Mo:

Static Public Member Functions

static fromString ($string, Translations $translations=null, $file='')
 {Parses a string and append the translations found in the Translations instance.
Parameters
string$string
Translations | null$translations
string$fileThe file path to insert the reference
Returns
Translations
} More...
 
- Static Public Member Functions inherited from Gettext\Extractors\Extractor
static fromFile ($file, Translations $translations=null)
 Extract the translations from a file. More...
 
- Static Public Member Functions inherited from Gettext\Extractors\ExtractorInterface
static fromFile ($file, Translations $translations=null)
 Extract the translations from a file. More...
 

Data Fields

const MAGIC1 = -1794895138
 
const MAGIC2 = -569244523
 
const MAGIC3 = 2500072158
 

Static Private Member Functions

static readInt (StringReader $stream, $byteOrder)
 
static readIntArray (StringReader $stream, $byteOrder, $count)
 

Additional Inherited Members

- Static Protected Member Functions inherited from Gettext\Extractors\Extractor
static getFiles ($file)
 Checks and returns all files. More...
 
static readFile ($file)
 Reads and returns the content of a file. More...
 

Detailed Description

Class to get gettext strings from .mo files.

Definition at line 12 of file Mo.php.

Member Function Documentation

◆ fromString()

static Gettext\Extractors\Mo::fromString (   $string,
Translations  $translations = null,
  $file = '' 
)
static

{Parses a string and append the translations found in the Translations instance.

Parameters
string$string
Translations | null$translations
string$fileThe file path to insert the reference
Returns
Translations
}

Implements Gettext\Extractors\ExtractorInterface.

Definition at line 21 of file Mo.php.

References $i, GuzzleHttp\Psr7\$stream, and $total.

22  {
23  if ($translations === null) {
24  $translations = new Translations();
25  }
26 
27  $stream = new StringReader($string);
28  $magic = self::readInt($stream, 'V');
29 
30  if (($magic === self::MAGIC1) || ($magic === self::MAGIC3)) { //to make sure it works for 64-bit platforms
31  $byteOrder = 'V'; //low endian
32  } elseif ($magic === (self::MAGIC2 & 0xFFFFFFFF)) {
33  $byteOrder = 'N'; //big endian
34  } else {
35  throw new Exception('Not MO file');
36  }
37 
38  self::readInt($stream, $byteOrder);
39 
40  $total = self::readInt($stream, $byteOrder); //total string count
41  $originals = self::readInt($stream, $byteOrder); //offset of original table
42  $tran = self::readInt($stream, $byteOrder); //offset of translation table
43 
44  $stream->seekto($originals);
45  $table_originals = self::readIntArray($stream, $byteOrder, $total * 2);
46 
47  $stream->seekto($tran);
48  $table_translations = self::readIntArray($stream, $byteOrder, $total * 2);
49 
50  for ($i = 0; $i < $total; ++$i) {
51  $next = $i * 2;
52 
53  $stream->seekto($table_originals[$next + 2]);
54  $original = $stream->read($table_originals[$next + 1]);
55 
56  $stream->seekto($table_translations[$next + 2]);
57  $translated = $stream->read($table_translations[$next + 1]);
58 
59  if ($original === '') {
60  // Headers
61  foreach (explode("\n", $translated) as $headerLine) {
62  if ($headerLine === '') {
63  continue;
64  }
65 
66  $headerChunks = preg_split('/:\s*/', $headerLine, 2);
67  $translations->setHeader($headerChunks[0], isset($headerChunks[1]) ? $headerChunks[1] : '');
68  }
69 
70  continue;
71  }
72 
73  $chunks = explode("\x04", $original, 2);
74 
75  if (isset($chunks[1])) {
76  $context = $chunks[0];
77  $original = $chunks[1];
78  } else {
79  $context = '';
80  }
81 
82  $chunks = explode("\x00", $original, 2);
83 
84  if (isset($chunks[1])) {
85  $original = $chunks[0];
86  $plural = $chunks[1];
87  } else {
88  $plural = '';
89  }
90 
91  $translation = $translations->insert($context, $original, $plural);
92 
93  if ($translated === '') {
94  continue;
95  }
96 
97  if ($plural === '') {
98  $translation->setTranslation($translated);
99  continue;
100  }
101 
102  foreach (explode("\x00", $translated) as $pluralIndex => $pluralValue) {
103  if ($pluralIndex === 0) {
104  $translation->setTranslation($pluralValue);
105  } else {
106  $translation->setPluralTranslation($pluralValue, $pluralIndex - 1);
107  }
108  }
109  }
110 
111  return $translations;
112  }
$stream
PHP stream implementation.
$total
Definition: Utf8Test.php:87
$i
Definition: disco.tpl.php:19

◆ readInt()

static Gettext\Extractors\Mo::readInt ( StringReader  $stream,
  $byteOrder 
)
staticprivate
Parameters
StringReader$stream
string$byteOrder

Definition at line 118 of file Mo.php.

References Gettext\Utils\StringReader\read().

119  {
120  if (($read = $stream->read(4)) === false) {
121  return false;
122  }
123 
124  $read = unpack($byteOrder, $read);
125 
126  return array_shift($read);
127  }
$stream
PHP stream implementation.
+ Here is the call graph for this function:

◆ readIntArray()

static Gettext\Extractors\Mo::readIntArray ( StringReader  $stream,
  $byteOrder,
  $count 
)
staticprivate
Parameters
StringReader$stream
string$byteOrder
int$count

Definition at line 134 of file Mo.php.

References Gettext\Utils\StringReader\read().

135  {
136  return unpack($byteOrder.$count, $stream->read(4 * $count));
137  }
$stream
PHP stream implementation.
+ Here is the call graph for this function:

Field Documentation

◆ MAGIC1

const Gettext\Extractors\Mo::MAGIC1 = -1794895138

Definition at line 14 of file Mo.php.

◆ MAGIC2

const Gettext\Extractors\Mo::MAGIC2 = -569244523

Definition at line 15 of file Mo.php.

◆ MAGIC3

const Gettext\Extractors\Mo::MAGIC3 = 2500072158

Definition at line 16 of file Mo.php.


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