File indexing completed on 2024-06-16 05:30:32

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Translate
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @version    $Id$
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  */
0021 
0022 
0023 /** Zend_Locale */
0024 // require_once 'Zend/Locale.php';
0025 
0026 /** Zend_Translate_Adapter */
0027 // require_once 'Zend/Translate/Adapter.php';
0028 
0029 /** @see Zend_Xml_Security */
0030 // require_once 'Zend/Xml/Security.php';
0031 
0032 /** @See Zend_Xml_Exception */
0033 // require_once 'Zend/Xml/Exception.php';
0034 
0035 /**
0036  * @category   Zend
0037  * @package    Zend_Translate
0038  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0039  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0040  */
0041 class Zend_Translate_Adapter_Qt extends Zend_Translate_Adapter {
0042     // Internal variables
0043     private $_file        = false;
0044     private $_cleared     = array();
0045     private $_transunit   = null;
0046     private $_source      = null;
0047     private $_target      = null;
0048     private $_scontent    = null;
0049     private $_tcontent    = null;
0050     private $_stag        = false;
0051     private $_ttag        = true;
0052     private $_data        = array();
0053 
0054     /**
0055      * Load translation data (QT file reader)
0056      *
0057      * @param  string  $locale    Locale/Language to add data for, identical with locale identifier,
0058      *                            see Zend_Locale for more information
0059      * @param  string  $filename  QT file to add, full path must be given for access
0060      * @param  array   $option    OPTIONAL Options to use
0061      * @throws Zend_Translation_Exception
0062      * @return array
0063      */
0064     protected function _loadTranslationData($filename, $locale, array $options = array())
0065     {
0066         $this->_data = array();
0067         if (!is_readable($filename)) {
0068             // require_once 'Zend/Translate/Exception.php';
0069             throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
0070         }
0071 
0072         $this->_target = $locale;
0073 
0074         $encoding = $this->_findEncoding($filename);
0075         $this->_file = xml_parser_create($encoding);
0076         xml_set_object($this->_file, $this);
0077         xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
0078         xml_set_element_handler($this->_file, "_startElement", "_endElement");
0079         xml_set_character_data_handler($this->_file, "_contentElement");
0080         
0081         try {
0082             Zend_Xml_Security::scanFile($filename);
0083         } catch (Zend_Xml_Exception $e) {
0084             // require_once 'Zend/Translate/Exception.php';
0085             throw new Zend_Translate_Exception(
0086                 $e->getMessage()
0087             );
0088         }
0089 
0090         if (!xml_parse($this->_file, file_get_contents($filename))) {
0091             $ex = sprintf('XML error: %s at line %d of file %s',
0092                           xml_error_string(xml_get_error_code($this->_file)),
0093                           xml_get_current_line_number($this->_file),
0094                           $filename);
0095             xml_parser_free($this->_file);
0096             // require_once 'Zend/Translate/Exception.php';
0097             throw new Zend_Translate_Exception($ex);
0098         }
0099 
0100         return $this->_data;
0101     }
0102 
0103     private function _startElement($file, $name, $attrib)
0104     {
0105         switch(strtolower($name)) {
0106             case 'message':
0107                 $this->_source = null;
0108                 $this->_stag = false;
0109                 $this->_ttag = false;
0110                 $this->_scontent = null;
0111                 $this->_tcontent = null;
0112                 break;
0113             case 'source':
0114                 $this->_stag = true;
0115                 break;
0116             case 'translation':
0117                 $this->_ttag = true;
0118                 break;
0119             default:
0120                 break;
0121         }
0122     }
0123 
0124     private function _endElement($file, $name)
0125     {
0126         switch (strtolower($name)) {
0127             case 'source':
0128                 $this->_stag = false;
0129                 break;
0130 
0131             case 'translation':
0132                 if (!empty($this->_scontent) and !empty($this->_tcontent) or
0133                     (isset($this->_data[$this->_target][$this->_scontent]) === false)) {
0134                     $this->_data[$this->_target][$this->_scontent] = $this->_tcontent;
0135                 }
0136                 $this->_ttag = false;
0137                 break;
0138 
0139             default:
0140                 break;
0141         }
0142     }
0143 
0144     private function _contentElement($file, $data)
0145     {
0146         if ($this->_stag === true) {
0147             $this->_scontent .= $data;
0148         }
0149 
0150         if ($this->_ttag === true) {
0151             $this->_tcontent .= $data;
0152         }
0153     }
0154 
0155     private function _findEncoding($filename)
0156     {
0157         $file = file_get_contents($filename, null, null, 0, 100);
0158         if (strpos($file, "encoding") !== false) {
0159             $encoding = substr($file, strpos($file, "encoding") + 9);
0160             $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
0161             return $encoding;
0162         }
0163         return 'UTF-8';
0164     }
0165 
0166     /**
0167      * Returns the adapter name
0168      *
0169      * @return string
0170      */
0171     public function toString()
0172     {
0173         return "Qt";
0174     }
0175 }