Exemple #1
0
 /**
  * Load PO file
  *
  * @access  public
  * @return  mixed   Returns true on success or PEAR_Error on failure.
  * @param   string  $file
  */
 function load($file = null)
 {
     $this->strings = array();
     if (!isset($file)) {
         $file = $this->file;
     }
     // load file
     if (!($contents = @file($file))) {
         return parent::raiseError($php_errormsg . ' ' . $file);
     }
     $contents = implode('', $contents);
     // match all msgid/msgstr entries
     $matched = preg_match_all('/(msgid\\s+("([^"]|\\\\")*?"\\s*)+)\\s+' . '(msgstr\\s+(\\s*"([^"]|\\\\")*?"\\s*)+)\\s*(?=$|#)/m', $contents, $matches);
     unset($contents);
     if (!$matched) {
         return parent::raiseError('No msgid/msgstr entries found');
     }
     // 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;
 }
Exemple #2
0
 /**
  * Load PO file
  *
  * @param string $file File path to load
  *
  * @access  public
  * @return  mixed   Returns true on success or PEAR_Error on failure.
  */
 function load($file = null)
 {
     $this->strings = array();
     if (!isset($file)) {
         $file = $this->file;
     }
     // load file
     if (!($contents = @file($file))) {
         return parent::raiseError($php_errormsg . ' ' . $file);
     }
     $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 parent::raiseError('No msgid/msgstr entries found');
     }
     // get all msgids and msgtrs
     for ($i = 0; $i < $matched; $i++) {
         $msgid = substr(rtrim($matches[1][$i]), 1, -1);
         $msgstr = substr(rtrim($matches[2][$i]), 1, -1);
         $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;
 }
Exemple #3
0
 /**
  * Load MO file
  *
  * @param string $file File path to load
  *
  * @access   public
  * @return   mixed   Returns true on success or PEAR_Error on failure.
  */
 function load($file = null)
 {
     $this->strings = array();
     if (!isset($file)) {
         $file = $this->file;
     }
     // open MO file
     if (!is_resource($this->_handle = @fopen($file, 'rb'))) {
         return parent::raiseError($php_errormsg . ' ' . $file);
     }
     // lock MO file shared
     if (!@flock($this->_handle, LOCK_SH)) {
         @fclose($this->_handle);
         return parent::raiseError($php_errormsg . ' ' . $file);
     }
     // read (part of) magic number from MO file header and define endianess
     switch ($magic = current($array = unpack('c', $this->_read(4)))) {
         case -34:
             $be = false;
             break;
         case -107:
             $be = true;
             break;
         default:
             return parent::raiseError("No GNU mo file: {$file} (magic: {$magic})");
     }
     // check file format revision - we currently only support 0
     if (0 !== ($_rev = $this->_readInt($be))) {
         return parent::raiseError('Invalid file format revision: ' . $_rev);
     }
     // 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++) {
         $pairs = array_combine($this->_readStrings($original[$i]), $this->_readStrings($translat[$i]));
         foreach ($pairs as $origStr => $translatedStr) {
             $this->strings[$origStr] = $translatedStr;
         }
     }
     // 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;
 }
Exemple #4
0
 /**
  * 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 parent::raiseError($php_errormsg . ' ' . $file);
     }
     $msgid = null;
     $aMatches = array();
     foreach ($contents as $line) {
         /*
         Replaced the regular expressions to get translations working on windows.
         */
         if (preg_match('/^msgid(.*)$/', $line, $aMatches)) {
             if ($msgid) {
                 $this->strings[parent::prepare($msgid)] = parent::prepare($msgstr);
             }
             $msgid = trim($aMatches[1]);
             $msgid = substr($msgid, 1, strlen($msgid) - 2);
             $msgstr = "";
             $msgstr_started = false;
         }
         //#^msgstr "(.*)"$#
         if (preg_match('/^msgstr(.*)$/', $line, $aMatches)) {
             $msgstr = trim($aMatches[1]);
             $msgstr = substr($msgstr, 1, strlen($msgstr) - 2);
             $msgstr_started = true;
         }
         //#^"(.*)"$#
         if (preg_match('/^"(.*)"$/', $line, $aMatches)) {
             if ($msgstr_started) {
                 $tmp = trim($aMatches[1]);
                 $msgstr .= substr($tmp, 1, strlen($tmp) - 2);
             } else {
                 $tmp = trim($aMatches[1]);
                 $msgid .= substr($tmp, 1, strlen($tmp) - 2);
             }
         }
         /* Original code
            if (preg_match('#^msgid "(.*)"$#', $line, $aMatches)) {
                if ($msgid) {
                    $this->strings[parent::prepare($msgid)] = parent::prepare($msgstr);
                }
                $msgid = $aMatches[1];
                $msgstr = "";
                $msgstr_started = false;
            }
            if (preg_match('#^msgstr "(.*)"$#', $line, $aMatches)) {
                $msgstr = $aMatches[1];
                $msgstr_started = true;
            }
            if (preg_match('#^"(.*)"$#', $line, $aMatches)) {
                if ($msgstr_started) {
                    $msgstr .= $aMatches[1];
                } else {
                    $msgid .= $aMatches[1];
                }
            }
            */
     }
     if ($msgid) {
         $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;
 }
Exemple #5
0
 /**
  * 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 parent::raiseError($php_errormsg . ' ' . $file);
     }
     $msgid = null;
     $aMatches = array();
     foreach ($contents as $line) {
         if (preg_match('#^msgid "(.*)"$#', $line, $aMatches)) {
             if ($msgid) {
                 $this->strings[parent::prepare($msgid)] = parent::prepare($msgstr);
             }
             $msgid = $aMatches[1];
             $msgstr = "";
             $msgstr_started = false;
         }
         if (preg_match('#^msgstr "(.*)"$#', $line, $aMatches)) {
             $msgstr = $aMatches[1];
             $msgstr_started = true;
         }
         if (preg_match('#^"(.*)"$#', $line, $aMatches)) {
             if ($msgstr_started) {
                 $msgstr .= $aMatches[1];
             } else {
                 $msgid .= $aMatches[1];
             }
         }
     }
     if ($msgid) {
         $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;
 }