-
-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathMoLoader.php
More file actions
156 lines (119 loc) · 4.08 KB
/
MoLoader.php
File metadata and controls
156 lines (119 loc) · 4.08 KB
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
<?php
declare(strict_types = 1);
namespace Gettext\Loader;
use Exception;
use Gettext\Translations;
/**
* Class to load a MO file.
*/
final class MoLoader extends Loader
{
/**
* @var string
*/
private $string;
/**
* @var int<0, max>
*/
private $position;
/**
* @var int<0, max>
*/
private $length;
private const MAGIC1 = -1794895138;
private const MAGIC2 = -569244523;
private const MAGIC3 = 2500072158;
public function loadString(string $string, ?Translations $translations = null): Translations
{
$translations = parent::loadString($string, $translations);
$this->init($string);
$magic = $this->readInt('V');
if (($magic === self::MAGIC1) || ($magic === self::MAGIC3)) { //to make sure it works for 64-bit platforms
$byteOrder = 'V'; //low endian
} elseif ($magic === (self::MAGIC2 & 0xFFFFFFFF)) {
$byteOrder = 'N'; //big endian
} else {
throw new Exception('Not MO file');
}
$this->readInt($byteOrder);
$total = $this->readInt($byteOrder); //total string count
$originals = $this->readInt($byteOrder); //offset of original table
$tran = $this->readInt($byteOrder); //offset of translation table
$this->seekto($originals);
$table_originals = $this->readIntArray($byteOrder, $total * 2);
$this->seekto($tran);
$table_translations = $this->readIntArray($byteOrder, $total * 2);
for ($i = 0; $i < $total; ++$i) {
$next = $i * 2;
$this->seekto($table_originals[$next + 2]);
$original = $this->read($table_originals[$next + 1]);
$this->seekto($table_translations[$next + 2]);
$translated = $this->read($table_translations[$next + 1]);
// Headers
if ($original === '') {
foreach (explode("\n", $translated) as $headerLine) {
if ($headerLine === '') {
continue;
}
$headerChunks = preg_split('/:\s*/', $headerLine, 2);
$translations->getHeaders()->set($headerChunks[0], $headerChunks[1] ?? '');
}
continue;
}
$context = $plural = null;
$chunks = explode("\x04", $original, 2);
if (isset($chunks[1])) {
[$context, $original] = $chunks;
}
$chunks = explode("\x00", $original, 2);
if (isset($chunks[1])) {
[$original, $plural] = $chunks;
}
$translation = $this->createTranslation($context, $original, $plural);
$translations->add($translation);
if ($translated === '') {
continue;
}
if ($plural === null) {
$translation->translate($translated);
continue;
}
$v = explode("\x00", $translated);
$translation->translate(array_shift($v));
$translation->translatePlural(...array_filter($v));
}
return $translations;
}
private function init(string $string): void
{
$this->string = $string;
$this->position = 0;
$this->length = strlen($string);
}
private function read(int $bytes): string
{
$data = substr($this->string, $this->position, $bytes);
$this->seekTo($this->position + $bytes);
return $data;
}
private function seekTo(int $position): void
{
$this->position = ($this->length < $position) ? $this->length : $position;
}
private function readInt(string $byteOrder): int
{
$read = $this->read(4);
$read = (array) unpack($byteOrder, $read);
return (int) array_shift($read);
}
/**
* @param string $byteOrder
* @param int $count
*
* @return array<int, int>
*/
private function readIntArray(string $byteOrder, int $count): array
{
return unpack($byteOrder.$count, $this->read(4 * $count)) ?: [];
}
}