web/lib/Zend/Translate/Adapter/Gettext.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Translate
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @version    $Id: Gettext.php 22653 2010-07-22 18:41:39Z mabe $
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  */
       
    21 
       
    22 /** Zend_Locale */
       
    23 require_once 'Zend/Locale.php';
       
    24 
       
    25 /** Zend_Translate_Adapter */
       
    26 require_once 'Zend/Translate/Adapter.php';
       
    27 
       
    28 /**
       
    29  * @category   Zend
       
    30  * @package    Zend_Translate
       
    31  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    32  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    33  */
       
    34 class Zend_Translate_Adapter_Gettext extends Zend_Translate_Adapter {
       
    35     // Internal variables
       
    36     private $_bigEndian   = false;
       
    37     private $_file        = false;
       
    38     private $_adapterInfo = array();
       
    39     private $_data        = array();
       
    40 
       
    41     /**
       
    42      * Read values from the MO file
       
    43      *
       
    44      * @param  string  $bytes
       
    45      */
       
    46     private function _readMOData($bytes)
       
    47     {
       
    48         if ($this->_bigEndian === false) {
       
    49             return unpack('V' . $bytes, fread($this->_file, 4 * $bytes));
       
    50         } else {
       
    51             return unpack('N' . $bytes, fread($this->_file, 4 * $bytes));
       
    52         }
       
    53     }
       
    54 
       
    55     /**
       
    56      * Load translation data (MO file reader)
       
    57      *
       
    58      * @param  string  $filename  MO file to add, full path must be given for access
       
    59      * @param  string  $locale    New Locale/Language to set, identical with locale identifier,
       
    60      *                            see Zend_Locale for more information
       
    61      * @param  array   $option    OPTIONAL Options to use
       
    62      * @throws Zend_Translation_Exception
       
    63      * @return array
       
    64      */
       
    65     protected function _loadTranslationData($filename, $locale, array $options = array())
       
    66     {
       
    67         $this->_data      = array();
       
    68         $this->_bigEndian = false;
       
    69         $this->_file      = @fopen($filename, 'rb');
       
    70         if (!$this->_file) {
       
    71             require_once 'Zend/Translate/Exception.php';
       
    72             throw new Zend_Translate_Exception('Error opening translation file \'' . $filename . '\'.');
       
    73         }
       
    74         if (@filesize($filename) < 10) {
       
    75             require_once 'Zend/Translate/Exception.php';
       
    76             throw new Zend_Translate_Exception('\'' . $filename . '\' is not a gettext file');
       
    77         }
       
    78 
       
    79         // get Endian
       
    80         $input = $this->_readMOData(1);
       
    81         if (strtolower(substr(dechex($input[1]), -8)) == "950412de") {
       
    82             $this->_bigEndian = false;
       
    83         } else if (strtolower(substr(dechex($input[1]), -8)) == "de120495") {
       
    84             $this->_bigEndian = true;
       
    85         } else {
       
    86             require_once 'Zend/Translate/Exception.php';
       
    87             throw new Zend_Translate_Exception('\'' . $filename . '\' is not a gettext file');
       
    88         }
       
    89         // read revision - not supported for now
       
    90         $input = $this->_readMOData(1);
       
    91 
       
    92         // number of bytes
       
    93         $input = $this->_readMOData(1);
       
    94         $total = $input[1];
       
    95 
       
    96         // number of original strings
       
    97         $input = $this->_readMOData(1);
       
    98         $OOffset = $input[1];
       
    99 
       
   100         // number of translation strings
       
   101         $input = $this->_readMOData(1);
       
   102         $TOffset = $input[1];
       
   103 
       
   104         // fill the original table
       
   105         fseek($this->_file, $OOffset);
       
   106         $origtemp = $this->_readMOData(2 * $total);
       
   107         fseek($this->_file, $TOffset);
       
   108         $transtemp = $this->_readMOData(2 * $total);
       
   109 
       
   110         for($count = 0; $count < $total; ++$count) {
       
   111             if ($origtemp[$count * 2 + 1] != 0) {
       
   112                 fseek($this->_file, $origtemp[$count * 2 + 2]);
       
   113                 $original = @fread($this->_file, $origtemp[$count * 2 + 1]);
       
   114                 $original = explode("\0", $original);
       
   115             } else {
       
   116                 $original[0] = '';
       
   117             }
       
   118 
       
   119             if ($transtemp[$count * 2 + 1] != 0) {
       
   120                 fseek($this->_file, $transtemp[$count * 2 + 2]);
       
   121                 $translate = fread($this->_file, $transtemp[$count * 2 + 1]);
       
   122                 $translate = explode("\0", $translate);
       
   123                 if ((count($original) > 1) && (count($translate) > 1)) {
       
   124                     $this->_data[$locale][$original[0]] = $translate;
       
   125                     array_shift($original);
       
   126                     foreach ($original as $orig) {
       
   127                         $this->_data[$locale][$orig] = '';
       
   128                     }
       
   129                 } else {
       
   130                     $this->_data[$locale][$original[0]] = $translate[0];
       
   131                 }
       
   132             }
       
   133         }
       
   134 
       
   135         $this->_data[$locale][''] = trim($this->_data[$locale]['']);
       
   136         if (empty($this->_data[$locale][''])) {
       
   137             $this->_adapterInfo[$filename] = 'No adapter information available';
       
   138         } else {
       
   139             $this->_adapterInfo[$filename] = $this->_data[$locale][''];
       
   140         }
       
   141 
       
   142         unset($this->_data[$locale]['']);
       
   143         return $this->_data;
       
   144     }
       
   145 
       
   146     /**
       
   147      * Returns the adapter informations
       
   148      *
       
   149      * @return array Each loaded adapter information as array value
       
   150      */
       
   151     public function getAdapterInfo()
       
   152     {
       
   153         return $this->_adapterInfo;
       
   154     }
       
   155 
       
   156     /**
       
   157      * Returns the adapter name
       
   158      *
       
   159      * @return string
       
   160      */
       
   161     public function toString()
       
   162     {
       
   163         return "Gettext";
       
   164     }
       
   165 }