Пример #1
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;
 }
Пример #2
0
 /**
  * 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 false;
     }
     // lock PO file exclusively
     if (!flock($fh, LOCK_EX)) {
         fclose($fh);
         return false;
     }
     // 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;
 }
 protected function createMessageTemplate($catalogue)
 {
     if (null === $catalogue) {
         $catalogue = 'messages';
     }
     $variants = $this->getCatalogueList($catalogue);
     $variant = array_shift($variants);
     $mo_file = $this->getSource($variant);
     $po_file = $this->getPOFile($mo_file);
     $dir = dirname($mo_file);
     if (!is_dir($dir)) {
         @mkdir($dir);
         @chmod($dir, 0777);
     }
     if (!is_dir($dir)) {
         throw new sfException(sprintf("Unable to create directory %s.", $dir));
     }
     $po = TGettext::factory('PO', $po_file);
     $result['meta']['PO-Revision-Date'] = date('Y-m-d H:i:s');
     $result['strings'] = array();
     $po->fromArray($result);
     $mo = $po->toMO();
     if ($po->save() && $mo->save($mo_file)) {
         return array($variant, $mo_file, $po_file);
     } else {
         throw new sfException(sprintf("Unable to create file %s and %s.", $po_file, $mo_file));
     }
 }
 /**
  * Updates the translation.
  *
  * @param string the source string.
  * @param string the new translation string.
  * @param string comments
  * @param string the catalogue of the translation.
  * @return boolean true if translation was updated, false otherwise.
  */
 function update($text, $target, $comments, $catalogue = 'messages')
 {
     $variants = $this->getVariants($catalogue);
     if ($variants) {
         list($variant, $MOFile, $POFile) = $variants;
     } else {
         return false;
     }
     if (is_writable($MOFile) == false) {
         throw new sfException(sprintf("Unable to update file %s, file must be writable.", $MOFile));
     }
     if (is_writable($POFile) == false) {
         throw new sfException(sprintf("Unable to update file %s, file must be writable.", $POFile));
     }
     $po = TGettext::factory('PO', $POFile);
     $po->load();
     $result = $po->toArray();
     foreach ($result['strings'] as $string => $value) {
         if ($string == $text) {
             $result['strings'][$string] = $target;
             $result['meta']['PO-Revision-Date'] = @date('Y-m-d H:i:s');
             $po->fromArray($result);
             $mo = $po->toMO();
             if ($po->save() && $mo->save($MOFile)) {
                 if (!empty($this->cache)) {
                     $this->cache->clean($variant, $this->culture);
                 }
                 return true;
             } else {
                 return false;
             }
         }
     }
     return false;
 }