yii\i18n\GettextMoFile::load PHP Method

load() public method

Loads messages from an MO file.
public load ( string $filePath, string $context ) : array
$filePath string file path
$context string message context
return array message translations. Array keys are source messages and array values are translated messages: source message => translated message.
    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.');
        }
        // magic
        $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
        $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;
    }

Usage Example

Beispiel #1
0
 public function testSave()
 {
     // initial data
     $s = chr(4);
     $messages = ['Hello!' => 'Привет!', "context1{$s}Hello?" => 'Привет?', 'Hello!?' => '', "context1{$s}Hello!?!" => '', "context2{$s}\"Quotes\"" => '"Кавычки"', "context2{$s}\nNew lines\n" => "\nПереносы строк\n", "context2{$s}\tTabs\t" => "\tТабы\t", "context2{$s}\rCarriage returns\r" => "\rВозвраты кареток\r"];
     // create temporary directory and dump messages
     $poFileDirectory = __DIR__ . '/../../runtime/i18n';
     if (!is_dir($poFileDirectory)) {
         mkdir($poFileDirectory);
     }
     if (is_file($poFileDirectory . '/test.mo')) {
         unlink($poFileDirectory . '/test.mo');
     }
     $moFile = new GettextMoFile();
     $moFile->save($poFileDirectory . '/test.mo', $messages);
     // load messages
     $context1 = $moFile->load($poFileDirectory . '/test.mo', 'context1');
     $context2 = $moFile->load($poFileDirectory . '/test.mo', 'context2');
     // context1
     $this->assertCount(2, $context1);
     $this->assertArrayHasKey('Hello?', $context1);
     $this->assertTrue(in_array('Привет?', $context1));
     $this->assertArrayHasKey('Hello!?!', $context1);
     $this->assertTrue(in_array('', $context1));
     // context2
     $this->assertCount(4, $context2);
     $this->assertArrayHasKey("\"Quotes\"", $context2);
     $this->assertTrue(in_array('"Кавычки"', $context2));
     $this->assertArrayHasKey("\nNew lines\n", $context2);
     $this->assertTrue(in_array("\nПереносы строк\n", $context2));
     $this->assertArrayHasKey("\tTabs\t", $context2);
     $this->assertTrue(in_array("\tТабы\t", $context2));
     $this->assertArrayHasKey("\rCarriage returns\r", $context2);
     $this->assertTrue(in_array("\rВозвраты кареток\r", $context2));
 }