web/enmi/Zend/Translate/Adapter/Qt.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     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: Qt.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  */
       
    21 
       
    22 
       
    23 /** Zend_Locale */
       
    24 require_once 'Zend/Locale.php';
       
    25 
       
    26 /** Zend_Translate_Adapter */
       
    27 require_once 'Zend/Translate/Adapter.php';
       
    28 
       
    29 
       
    30 /**
       
    31  * @category   Zend
       
    32  * @package    Zend_Translate
       
    33  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    34  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    35  */
       
    36 class Zend_Translate_Adapter_Qt extends Zend_Translate_Adapter {
       
    37     // Internal variables
       
    38     private $_file        = false;
       
    39     private $_cleared     = array();
       
    40     private $_transunit   = null;
       
    41     private $_source      = null;
       
    42     private $_target      = null;
       
    43     private $_scontent    = null;
       
    44     private $_tcontent    = null;
       
    45     private $_stag        = false;
       
    46     private $_ttag        = true;
       
    47     private $_data        = array();
       
    48 
       
    49     /**
       
    50      * Load translation data (QT file reader)
       
    51      *
       
    52      * @param  string  $locale    Locale/Language to add data for, identical with locale identifier,
       
    53      *                            see Zend_Locale for more information
       
    54      * @param  string  $filename  QT file to add, full path must be given for access
       
    55      * @param  array   $option    OPTIONAL Options to use
       
    56      * @throws Zend_Translation_Exception
       
    57      * @return array
       
    58      */
       
    59     protected function _loadTranslationData($filename, $locale, array $options = array())
       
    60     {
       
    61         $this->_data = array();
       
    62         if (!is_readable($filename)) {
       
    63             require_once 'Zend/Translate/Exception.php';
       
    64             throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
       
    65         }
       
    66 
       
    67         $this->_target = $locale;
       
    68 
       
    69         $encoding = $this->_findEncoding($filename);
       
    70         $this->_file = xml_parser_create($encoding);
       
    71         xml_set_object($this->_file, $this);
       
    72         xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
       
    73         xml_set_element_handler($this->_file, "_startElement", "_endElement");
       
    74         xml_set_character_data_handler($this->_file, "_contentElement");
       
    75 
       
    76         if (!xml_parse($this->_file, file_get_contents($filename))) {
       
    77             $ex = sprintf('XML error: %s at line %d',
       
    78                           xml_error_string(xml_get_error_code($this->_file)),
       
    79                           xml_get_current_line_number($this->_file));
       
    80             xml_parser_free($this->_file);
       
    81             require_once 'Zend/Translate/Exception.php';
       
    82             throw new Zend_Translate_Exception($ex);
       
    83         }
       
    84 
       
    85         return $this->_data;
       
    86     }
       
    87 
       
    88     private function _startElement($file, $name, $attrib)
       
    89     {
       
    90         switch(strtolower($name)) {
       
    91             case 'message':
       
    92                 $this->_source = null;
       
    93                 $this->_stag = false;
       
    94                 $this->_ttag = false;
       
    95                 $this->_scontent = null;
       
    96                 $this->_tcontent = null;
       
    97                 break;
       
    98             case 'source':
       
    99                 $this->_stag = true;
       
   100                 break;
       
   101             case 'translation':
       
   102                 $this->_ttag = true;
       
   103                 break;
       
   104             default:
       
   105                 break;
       
   106         }
       
   107     }
       
   108 
       
   109     private function _endElement($file, $name)
       
   110     {
       
   111         switch (strtolower($name)) {
       
   112             case 'source':
       
   113                 $this->_stag = false;
       
   114                 break;
       
   115 
       
   116             case 'translation':
       
   117                 if (!empty($this->_scontent) and !empty($this->_tcontent) or
       
   118                     (isset($this->_data[$this->_target][$this->_scontent]) === false)) {
       
   119                     $this->_data[$this->_target][$this->_scontent] = $this->_tcontent;
       
   120                 }
       
   121                 $this->_ttag = false;
       
   122                 break;
       
   123 
       
   124             default:
       
   125                 break;
       
   126         }
       
   127     }
       
   128 
       
   129     private function _contentElement($file, $data)
       
   130     {
       
   131         if ($this->_stag === true) {
       
   132             $this->_scontent .= $data;
       
   133         }
       
   134 
       
   135         if ($this->_ttag === true) {
       
   136             $this->_tcontent .= $data;
       
   137         }
       
   138     }
       
   139 
       
   140     private function _findEncoding($filename)
       
   141     {
       
   142         $file = file_get_contents($filename, null, null, 0, 100);
       
   143         if (strpos($file, "encoding") !== false) {
       
   144             $encoding = substr($file, strpos($file, "encoding") + 9);
       
   145             $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
       
   146             return $encoding;
       
   147         }
       
   148         return 'UTF-8';
       
   149     }
       
   150 
       
   151     /**
       
   152      * Returns the adapter name
       
   153      *
       
   154      * @return string
       
   155      */
       
   156     public function toString()
       
   157     {
       
   158         return "Qt";
       
   159     }
       
   160 }