|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
-
-
- namespace yii\i18n;
-
- use yii\base\Exception;
-
-
- class GettextMoFile extends GettextFile
- {
-
-
- public $useBigEndian = false;
-
-
-
-
- public function load($filePath, $context)
- {
- if (false === ($fileHandle = @fopen($filePath, 'rb'))) {
- throw new Exception('Unable to read file "' . $filePath . '".');
- }
- if (false === @flock($fileHandle, LOCK_SH)) {
- throw new Exception('Unable to lock file "' . $filePath . '" for reading.');
- }
-
-
- $array = unpack('c', $this->readBytes($fileHandle, 4));
- $magic = current($array);
- if ($magic == -34) {
- $this->useBigEndian = false;
- } elseif ($magic == -107) {
- $this->useBigEndian = true;
- } else {
- throw new Exception('Invalid MO file: ' . $filePath . ' (magic: ' . $magic . ').');
- }
-
-
- $revision = $this->readInteger($fileHandle);
- if ($revision !== 0) {
- throw new Exception('Invalid MO file revision: ' . $revision . '.');
- }
-
- $count = $this->readInteger($fileHandle);
- $sourceOffset = $this->readInteger($fileHandle);
- $targetOffset = $this->readInteger($fileHandle);
-
- $sourceLengths = [];
- $sourceOffsets = [];
- fseek($fileHandle, $sourceOffset);
- for ($i = 0; $i < $count; ++$i) {
- $sourceLengths[] = $this->readInteger($fileHandle);
- $sourceOffsets[] = $this->readInteger($fileHandle);
- }
-
- $targetLengths = [];
- $targetOffsets = [];
- fseek($fileHandle, $targetOffset);
- for ($i = 0; $i < $count; ++$i) {
- $targetLengths[] = $this->readInteger($fileHandle);
- $targetOffsets[] = $this->readInteger($fileHandle);
- }
-
- $messages = [];
- for ($i = 0; $i < $count; ++$i) {
- $id = $this->readString($fileHandle, $sourceLengths[$i], $sourceOffsets[$i]);
- $separatorPosition = strpos($id, chr(4));
-
-
- if ((!$context && $separatorPosition === false) || ($context && $separatorPosition !== false && strncmp($id, $context, $separatorPosition) === 0)) {
- if ($separatorPosition !== false) {
- $id = substr($id, $separatorPosition+1);
- }
-
- $message = $this->readString($fileHandle, $targetLengths[$i], $targetOffsets[$i]);
- $messages[$id] = $message;
- }
- }
-
- @flock($fileHandle, LOCK_UN);
- @fclose($fileHandle);
-
- return $messages;
- }
-
-
-
- public function save($filePath, $messages)
- {
- if (false === ($fileHandle = @fopen($filePath, 'wb'))) {
- throw new Exception('Unable to write file "' . $filePath . '".');
- }
- if (false === @flock($fileHandle, LOCK_EX)) {
- throw new Exception('Unable to lock file "' . $filePath . '" for reading.');
- }
-
-
- if ($this->useBigEndian) {
- $this->writeBytes($fileHandle, pack('c*', 0x95, 0x04, 0x12, 0xde));
- } else {
- $this->writeBytes($fileHandle, pack('c*', 0xde, 0x12, 0x04, 0x95));
- }
-
-
- $this->writeInteger($fileHandle, 0);
-
-
- $messageCount = count($messages);
- $this->writeInteger($fileHandle, $messageCount);
-
-
- $offset = 28;
- $this->writeInteger($fileHandle, $offset);
- $offset += $messageCount * 8;
- $this->writeInteger($fileHandle, $offset);
-
-
- $this->writeInteger($fileHandle, 0);
- $offset += $messageCount * 8;
- $this->writeInteger($fileHandle, $offset);
-
-
- foreach (array_keys($messages) as $id) {
- $length = strlen($id);
- $this->writeInteger($fileHandle, $length);
- $this->writeInteger($fileHandle, $offset);
- $offset += $length + 1;
- }
-
-
- foreach ($messages as $message) {
- $length = strlen($message);
- $this->writeInteger($fileHandle, $length);
- $this->writeInteger($fileHandle, $offset);
- $offset += $length + 1;
- }
-
-
- foreach (array_keys($messages) as $id) {
- $this->writeString($fileHandle, $id);
- }
-
-
- foreach ($messages as $message) {
- $this->writeString($fileHandle, $message);
- }
-
- @flock($fileHandle, LOCK_UN);
- @fclose($fileHandle);
- }
-
-
-
- protected function readBytes($fileHandle, $byteCount = 1)
- {
- if ($byteCount > 0) {
- return fread($fileHandle, $byteCount);
- } else {
- return null;
- }
- }
-
-
-
- protected function writeBytes($fileHandle, $bytes)
- {
- return fwrite($fileHandle, $bytes);
- }
-
-
-
- protected function readInteger($fileHandle)
- {
- $array = unpack($this->useBigEndian ? 'N' : 'V', $this->readBytes($fileHandle, 4));
-
- return current($array);
- }
-
-
-
- protected function writeInteger($fileHandle, $integer)
- {
- return $this->writeBytes($fileHandle, pack($this->useBigEndian ? 'N' : 'V', (int) $integer));
- }
-
-
-
- protected function readString($fileHandle, $length, $offset = null)
- {
- if ($offset !== null) {
- fseek($fileHandle, $offset);
- }
-
- return $this->readBytes($fileHandle, $length);
- }
-
-
-
- protected function writeString($fileHandle, $string)
- {
- return $this->writeBytes($fileHandle, $string. "\0");
- }
- }
|