/** * Save MO file * * @param string $file File path to write to * * @access public * @return mixed Returns true on success or PEAR_Error on failure. */ function save($file = null) { if (!isset($file)) { $file = $this->file; } $tmpfile = $file . "." . getmypid(); // open MO file if (!is_resource($this->_handle = @fopen($tmpfile, 'wb'))) { return parent::raiseError($php_errormsg . ' ' . $file); } // lock MO file exclusively if (!@flock($this->_handle, LOCK_EX)) { @fclose($this->_handle); return parent::raiseError($php_errormsg . ' ' . $file); } // write magic number if ($this->writeBigEndian) { $this->_write(pack('c*', 0x95, 0x4, 0x12, 0xde)); } else { $this->_write(pack('c*', 0xde, 0x12, 0x4, 0x95)); } // write file format revision $this->_writeInt(0); $count = count($this->strings) + ($meta = count($this->meta) ? 1 : 0); // write count of strings $this->_writeInt($count); $hash_tab_size = self::_nextPrime($count * 4 / 3); /* Ensure M > 2. */ if ($hash_tab_size <= 2) { $hash_tab_size = 3; } $offset = 28; // write offset of orig. strings hash table $this->_writeInt($offset); $offset += $count * 8; // write offset transl. strings hash table $this->_writeInt($offset); // write size of hash table (we currently ommit the hash table) $this->_writeInt($hash_tab_size); // orig: 0 $offset += $count * 8; // write offset of hash table $this->_writeInt($offset); $offset += $hash_tab_size * 4; // unshift meta info if ($meta) { $meta = ''; foreach ($this->meta as $key => $val) { $meta .= $key . ': ' . $val . "\n"; } $strings = array('' => $meta) + $this->strings; } else { $strings = $this->strings; } $hash_tab = array(); $j = 0; foreach ($strings as $key => $value) { $hash_val = self::_hashpjw($key); $idx = $hash_val % $hash_tab_size; if (!empty($hash_tab[$idx])) { $incr = 1 + $hash_val % ($hash_tab_size - 2); do { if ($idx >= $hash_tab_size - $incr) { $idx -= $hash_tab_size - $incr; } else { $idx += $incr; } } while (!empty($hash_tab[$idx])); } $hash_tab[$idx] = $j + 1; $j++; } // write offsets for original strings foreach (array_keys($strings) as $o) { $len = strlen($o); $this->_writeInt($len); $this->_writeInt($offset); $offset += $len + 1; } // write offsets for translated strings foreach ($strings as $t) { $len = strlen($t); $this->_writeInt($len); $this->_writeInt($offset); $offset += $len + 1; } for ($j = 0; $j < $hash_tab_size; $j++) { if (empty($hash_tab[$j])) { $this->_writeInt(0); } else { $this->_writeInt($hash_tab[$j]); } } // write original strings foreach (array_keys($strings) as $o) { $this->_writeStr($o); } // write translated strings foreach ($strings as $t) { $this->_writeStr($t); } // done @flock($this->_handle, LOCK_UN); @fclose($this->_handle); @rename($tmpfile, $file); @unlink($tmpfile); return true; }
/** * Save MO file * * @access public * @return mixed Returns true on success or PEAR_Error on failure. * @param string $file */ function save($file = null) { if (!isset($file)) { $file = $this->file; } // open MO file if (!is_resource($this->_handle = @fopen($file, 'wb'))) { return parent::raiseError($php_errormsg . ' ' . $file); } // lock MO file exclusively if (!@flock($this->_handle, LOCK_EX)) { @fclose($this->_handle); return parent::raiseError($php_errormsg . ' ' . $file); } // write magic number if ($this->writeBigEndian) { $this->_write(pack('c*', 0x95, 0x4, 0x12, 0xde)); } else { $this->_write(pack('c*', 0xde, 0x12, 0x4, 0x95)); } // write file format revision $this->_writeInt(0); $count = count($this->strings) + ($meta = count($this->meta) ? 1 : 0); // write count of strings $this->_writeInt($count); $offset = 28; // write offset of orig. strings hash table $this->_writeInt($offset); $offset += $count * 8; // write offset transl. strings hash table $this->_writeInt($offset); // write size of hash table (we currently ommit the hash table) $this->_writeInt(0); $offset += $count * 8; // write offset of hash table $this->_writeInt($offset); // unshift meta info if ($meta) { $meta = ''; foreach ($this->meta as $key => $val) { $meta .= $key . ': ' . $val . "\n"; } $strings = array('' => $meta) + $this->strings; } else { $strings = $this->strings; } // write offsets for original strings foreach (array_keys($strings) as $o) { $len = strlen($o); $this->_writeInt($len); $this->_writeInt($offset); $offset += $len + 1; } // write offsets for translated strings foreach ($strings as $t) { $len = strlen($t); $this->_writeInt($len); $this->_writeInt($offset); $offset += $len + 1; } // write original strings foreach (array_keys($strings) as $o) { $this->_writeStr($o); } // write translated strings foreach ($strings as $t) { $this->_writeStr($t); } // done @flock($this->_handle, LOCK_UN); @fclose($this->_handle); return true; }
/** * Save PO file * * @access public * @return mixed Returns true on success or PEAR_Error on failure. * @param string $file */ function save($file = null) { if (!isset($file)) { $file = $this->file; } // open PO file if (!is_resource($fh = @fopen($file, 'w'))) { return parent::raiseError($php_errormsg . ' ' . $file); } // lock PO file exclusively if (!@flock($fh, LOCK_EX)) { @fclose($fh); return parent::raiseError($php_errormsg . ' ' . $file); } // write meta info if (count($this->meta)) { $meta = 'msgid ""' . "\nmsgstr " . '""' . "\n"; foreach ($this->meta as $k => $v) { $meta .= '"' . $k . ': ' . $v . '\\n"' . "\n"; } fwrite($fh, $meta . "\n"); } // write strings foreach ($this->strings as $o => $t) { fwrite($fh, 'msgid "' . parent::prepare($o, true) . '"' . "\n" . 'msgstr "' . parent::prepare($t, true) . '"' . "\n\n"); } //done @flock($fh, LOCK_UN); @fclose($fh); return true; }
/** * poFile2moFile * * That's a simple fake of the 'msgfmt' console command. It reads the * contents of a GNU PO file and saves them to a GNU MO file. * * @param string $pofile path to GNU PO file * @param string $mofile path to GNU MO file * * @static * @access public * @return mixed Returns true on success or PEAR_Error on failure. */ function poFile2moFile($pofile, $mofile) { if (!is_file($pofile)) { return File_Gettext::raiseError("File {$pofile} doesn't exist."); } include_once 'File/Gettext/PO.php'; $PO = new File_Gettext_PO($pofile); if (true !== ($e = $PO->load())) { return $e; } $MO =& $PO->toMO(); if (true !== ($e = $MO->save($mofile))) { return $e; } unset($PO, $MO); return true; }