3
* @link https://www.yiiframework.com/
4
* @copyright Copyright (c) 2008 Yii Software LLC
5
* @license https://www.yiiframework.com/license/
10
use yii\base\Exception;
13
* GettextMoFile represents an MO Gettext message file.
15
* This class is written by adapting Michael's Gettext_MO class in PEAR.
16
* Please refer to the following license terms.
18
* Copyright (c) 2004-2005, Michael Wallner <mike@iworks.at>.
19
* All rights reserved.
21
* Redistribution and use in source and binary forms, with or without
22
* modification, are permitted provided that the following conditions are met:
24
* * Redistributions of source code must retain the above copyright notice,
25
* this list of conditions and the following disclaimer.
26
* * Redistributions in binary form must reproduce the above copyright
27
* notice, this list of conditions and the following disclaimer in the
28
* documentation and/or other materials provided with the distribution.
30
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
34
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
38
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41
* @author Qiang Xue <qiang.xue@gmail.com>
44
class GettextMoFile extends GettextFile
47
* @var bool whether to use big-endian when reading and writing an integer.
49
public $useBigEndian = false;
53
* Loads messages from an MO file.
54
* @param string $filePath file path
55
* @param string $context message context
56
* @return array message translations. Array keys are source messages and array values are translated messages:
57
* source message => translated message.
58
* @throws Exception if unable to read the MO file
60
public function load($filePath, $context)
62
if (false === ($fileHandle = @fopen($filePath, 'rb'))) {
63
throw new Exception('Unable to read file "' . $filePath . '".');
65
if (false === @flock($fileHandle, LOCK_SH)) {
66
throw new Exception('Unable to lock file "' . $filePath . '" for reading.');
70
$array = unpack('c', $this->readBytes($fileHandle, 4));
71
$magic = current($array);
73
$this->useBigEndian = false;
74
} elseif ($magic == -107) {
75
$this->useBigEndian = true;
77
throw new Exception('Invalid MO file: ' . $filePath . ' (magic: ' . $magic . ').');
81
$revision = $this->readInteger($fileHandle);
82
if ($revision !== 0) {
83
throw new Exception('Invalid MO file revision: ' . $revision . '.');
86
$count = $this->readInteger($fileHandle);
87
$sourceOffset = $this->readInteger($fileHandle);
88
$targetOffset = $this->readInteger($fileHandle);
92
fseek($fileHandle, $sourceOffset);
93
for ($i = 0; $i < $count; ++$i) {
94
$sourceLengths[] = $this->readInteger($fileHandle);
95
$sourceOffsets[] = $this->readInteger($fileHandle);
100
fseek($fileHandle, $targetOffset);
101
for ($i = 0; $i < $count; ++$i) {
102
$targetLengths[] = $this->readInteger($fileHandle);
103
$targetOffsets[] = $this->readInteger($fileHandle);
107
for ($i = 0; $i < $count; ++$i) {
108
$id = $this->readString($fileHandle, $sourceLengths[$i], $sourceOffsets[$i]);
109
$separatorPosition = strpos((string)$id, chr(4));
112
if ((!$context && $separatorPosition === false) || ($context && $separatorPosition !== false && strncmp($id, $context, $separatorPosition) === 0)) {
113
if ($separatorPosition !== false) {
114
$id = substr($id, $separatorPosition + 1);
117
$message = $this->readString($fileHandle, $targetLengths[$i], $targetOffsets[$i]);
118
$messages[$id] = $message;
122
@flock($fileHandle, LOCK_UN);
123
@fclose($fileHandle);
129
* Saves messages to an MO file.
130
* @param string $filePath file path
131
* @param array $messages message translations. Array keys are source messages and array values are
132
* translated messages: source message => translated message. Note if the message has a context,
133
* the message ID must be prefixed with the context with chr(4) as the separator.
134
* @throws Exception if unable to save the MO file
136
public function save($filePath, $messages)
138
if (false === ($fileHandle = @fopen($filePath, 'wb'))) {
139
throw new Exception('Unable to write file "' . $filePath . '".');
141
if (false === @flock($fileHandle, LOCK_EX)) {
142
throw new Exception('Unable to lock file "' . $filePath . '" for reading.');
146
if ($this->useBigEndian) {
147
$this->writeBytes($fileHandle, pack('c*', 0x95, 0x04, 0x12, 0xde)); // -107
149
$this->writeBytes($fileHandle, pack('c*', 0xde, 0x12, 0x04, 0x95)); // -34
153
$this->writeInteger($fileHandle, 0);
156
$messageCount = count($messages);
157
$this->writeInteger($fileHandle, $messageCount);
159
// offset of source message table
161
$this->writeInteger($fileHandle, $offset);
162
$offset += $messageCount * 8;
163
$this->writeInteger($fileHandle, $offset);
165
// hashtable size, omitted
166
$this->writeInteger($fileHandle, 0);
167
$offset += $messageCount * 8;
168
$this->writeInteger($fileHandle, $offset);
170
// length and offsets for source messages
171
foreach (array_keys($messages) as $id) {
172
$length = strlen($id);
173
$this->writeInteger($fileHandle, $length);
174
$this->writeInteger($fileHandle, $offset);
175
$offset += $length + 1;
178
// length and offsets for target messages
179
foreach ($messages as $message) {
180
$length = strlen($message);
181
$this->writeInteger($fileHandle, $length);
182
$this->writeInteger($fileHandle, $offset);
183
$offset += $length + 1;
187
foreach (array_keys($messages) as $id) {
188
$this->writeString($fileHandle, $id);
192
foreach ($messages as $message) {
193
$this->writeString($fileHandle, $message);
196
@flock($fileHandle, LOCK_UN);
197
@fclose($fileHandle);
201
* Reads one or several bytes.
202
* @param resource $fileHandle to read from
203
* @param int $byteCount to be read
204
* @return string bytes
206
protected function readBytes($fileHandle, $byteCount = 1)
208
if ($byteCount > 0) {
209
return fread($fileHandle, $byteCount);
217
* @param resource $fileHandle to write to
218
* @param string $bytes to be written
219
* @return int how many bytes are written
221
protected function writeBytes($fileHandle, $bytes)
223
return fwrite($fileHandle, $bytes);
227
* Reads a 4-byte integer.
228
* @param resource $fileHandle to read from
229
* @return int the result
231
protected function readInteger($fileHandle)
233
$array = unpack($this->useBigEndian ? 'N' : 'V', $this->readBytes($fileHandle, 4));
235
return current($array);
239
* Writes a 4-byte integer.
240
* @param resource $fileHandle to write to
241
* @param int $integer to be written
242
* @return int how many bytes are written
244
protected function writeInteger($fileHandle, $integer)
246
return $this->writeBytes($fileHandle, pack($this->useBigEndian ? 'N' : 'V', (int) $integer));
251
* @param resource $fileHandle file handle
252
* @param int $length of the string
253
* @param int|null $offset of the string in the file. If null, it reads from the current position.
254
* @return string the result
256
protected function readString($fileHandle, $length, $offset = null)
258
if ($offset !== null) {
259
fseek($fileHandle, $offset);
262
return $this->readBytes($fileHandle, $length);
267
* @param resource $fileHandle to write to
268
* @param string $string to be written
269
* @return int how many bytes are written
271
protected function writeString($fileHandle, $string)
273
return $this->writeBytes($fileHandle, $string . "\0");