示例#1
0
文件: PO.php 项目: cuongnv540/jobeet
 /**
  * Load PO file
  *
  * @access  public
  * @return  mixed   Returns true on success or PEAR_Error on failure.
  * @param   string  $file
  */
 function load($file = null)
 {
     if (!isset($file)) {
         $file = $this->file;
     }
     // load file
     if (!($contents = @file($file))) {
         return false;
     }
     $contents = implode('', $contents);
     // match all msgid/msgstr entries
     $matched = preg_match_all('/(msgid\\s+("([^"]|\\\\")*?"\\s*)+)\\s+' . '(msgstr\\s+("([^"]|\\\\")*?"\\s*)+)/', $contents, $matches);
     unset($contents);
     if (!$matched) {
         return false;
     }
     // get all msgids and msgtrs
     for ($i = 0; $i < $matched; $i++) {
         $msgid = preg_replace('/\\s*msgid\\s*"(.*)"\\s*/s', '\\1', $matches[1][$i]);
         $msgstr = preg_replace('/\\s*msgstr\\s*"(.*)"\\s*/s', '\\1', $matches[4][$i]);
         $this->strings[parent::prepare($msgid)] = parent::prepare($msgstr);
     }
     // check for meta info
     if (isset($this->strings[''])) {
         $this->meta = parent::meta2array($this->strings['']);
         unset($this->strings['']);
     }
     return true;
 }
示例#2
0
 /**
  * Load MO file
  *
  * @access   public
  * @return   mixed   Returns true on success or PEAR_Error on failure.
  * @param    string  $file
  */
 function load($file = null)
 {
     if (!isset($file)) {
         $file = $this->file;
     }
     // open MO file
     if (!is_resource($this->_handle = @fopen($file, 'rb'))) {
         return false;
     }
     // lock MO file shared
     if (!@flock($this->_handle, LOCK_SH)) {
         @fclose($this->_handle);
         return false;
     }
     // read (part of) magic number from MO file header and define endianess
     //unpack returns a reference????
     $unpacked = unpack('c', $this->_read(4));
     switch ($magic = array_shift($unpacked)) {
         case -34:
             $be = false;
             break;
         case -107:
             $be = true;
             break;
         default:
             return false;
     }
     // check file format revision - we currently only support 0
     if (0 !== ($_rev = $this->_readInt($be))) {
         return false;
     }
     // count of strings in this file
     $count = $this->_readInt($be);
     // offset of hashing table of the msgids
     $offset_original = $this->_readInt($be);
     // offset of hashing table of the msgstrs
     $offset_translat = $this->_readInt($be);
     // move to msgid hash table
     fseek($this->_handle, $offset_original);
     // read lengths and offsets of msgids
     $original = array();
     for ($i = 0; $i < $count; $i++) {
         $original[$i] = array('length' => $this->_readInt($be), 'offset' => $this->_readInt($be));
     }
     // move to msgstr hash table
     fseek($this->_handle, $offset_translat);
     // read lengths and offsets of msgstrs
     $translat = array();
     for ($i = 0; $i < $count; $i++) {
         $translat[$i] = array('length' => $this->_readInt($be), 'offset' => $this->_readInt($be));
     }
     // read all
     for ($i = 0; $i < $count; $i++) {
         $this->strings[$this->_readStr($original[$i])] = $this->_readStr($translat[$i]);
     }
     // done
     @flock($this->_handle, LOCK_UN);
     @fclose($this->_handle);
     $this->_handle = null;
     // check for meta info
     if (isset($this->strings[''])) {
         $this->meta = parent::meta2array($this->strings['']);
         unset($this->strings['']);
     }
     return true;
 }