File indexing completed on 2025-05-04 05:28:28
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_XmlTm extends Zend_Translate_Adapter { 0042 // Internal variables 0043 private $_file = false; 0044 private $_cleared = array(); 0045 private $_lang = null; 0046 private $_content = null; 0047 private $_tag = null; 0048 private $_data = array(); 0049 0050 /** 0051 * Load translation data (XMLTM file reader) 0052 * 0053 * @param string $locale Locale/Language to add data for, identical with locale identifier, 0054 * see Zend_Locale for more information 0055 * @param string $filename XMLTM file to add, full path must be given for access 0056 * @param array $option OPTIONAL Options to use 0057 * @throws Zend_Translation_Exception 0058 * @return array 0059 */ 0060 protected function _loadTranslationData($filename, $locale, array $options = array()) 0061 { 0062 $this->_data = array(); 0063 $this->_lang = $locale; 0064 if (!is_readable($filename)) { 0065 // require_once 'Zend/Translate/Exception.php'; 0066 throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.'); 0067 } 0068 0069 $encoding = $this->_findEncoding($filename); 0070 $this->_file = xml_parser_create($encoding); 0071 xml_set_object($this->_file, $this); 0072 xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0); 0073 xml_set_element_handler($this->_file, "_startElement", "_endElement"); 0074 xml_set_character_data_handler($this->_file, "_contentElement"); 0075 0076 try { 0077 Zend_Xml_Security::scanFile($filename); 0078 } catch (Zend_Xml_Exception $e) { 0079 // require_once 'Zend/Translate/Exception.php'; 0080 throw new Zend_Translate_Exception( 0081 $e->getMessage() 0082 ); 0083 } 0084 0085 if (!xml_parse($this->_file, file_get_contents($filename))) { 0086 $ex = sprintf('XML error: %s at line %d of file %s', 0087 xml_error_string(xml_get_error_code($this->_file)), 0088 xml_get_current_line_number($this->_file), 0089 $filename); 0090 xml_parser_free($this->_file); 0091 // require_once 'Zend/Translate/Exception.php'; 0092 throw new Zend_Translate_Exception($ex); 0093 } 0094 0095 return $this->_data; 0096 } 0097 0098 private function _startElement($file, $name, $attrib) 0099 { 0100 switch(strtolower($name)) { 0101 case 'tm:tu': 0102 $this->_tag = $attrib['id']; 0103 $this->_content = null; 0104 break; 0105 default: 0106 break; 0107 } 0108 } 0109 0110 private function _endElement($file, $name) 0111 { 0112 switch (strtolower($name)) { 0113 case 'tm:tu': 0114 if (!empty($this->_tag) and !empty($this->_content) or 0115 (isset($this->_data[$this->_lang][$this->_tag]) === false)) { 0116 $this->_data[$this->_lang][$this->_tag] = $this->_content; 0117 } 0118 $this->_tag = null; 0119 $this->_content = null; 0120 break; 0121 0122 default: 0123 break; 0124 } 0125 } 0126 0127 private function _contentElement($file, $data) 0128 { 0129 if (($this->_tag !== null)) { 0130 $this->_content .= $data; 0131 } 0132 } 0133 0134 private function _findEncoding($filename) 0135 { 0136 $file = file_get_contents($filename, null, null, 0, 100); 0137 if (strpos($file, "encoding") !== false) { 0138 $encoding = substr($file, strpos($file, "encoding") + 9); 0139 $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1); 0140 return $encoding; 0141 } 0142 return 'UTF-8'; 0143 } 0144 0145 /** 0146 * Returns the adapter name 0147 * 0148 * @return string 0149 */ 0150 public function toString() 0151 { 0152 return "XmlTm"; 0153 } 0154 }