File indexing completed on 2025-03-09 05:26:23
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_Tbx extends Zend_Translate_Adapter { 0042 // Internal variables 0043 private $_file = false; 0044 private $_cleared = array(); 0045 private $_langset = null; 0046 private $_termentry = null; 0047 private $_content = null; 0048 private $_term = null; 0049 private $_data = array(); 0050 0051 /** 0052 * Load translation data (TBX file reader) 0053 * 0054 * @param string $filename TBX file to add, full path must be given for access 0055 * @param string $locale Locale has no effect for TBX because TBX defines all languages within 0056 * the source file 0057 * @param array $option OPTIONAL Options to use 0058 * @throws Zend_Translation_Exception 0059 * @return array 0060 */ 0061 protected function _loadTranslationData($filename, $locale, array $options = array()) 0062 { 0063 $this->_data = array(); 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 if ($this->_term !== null) { 0101 $this->_content .= "<".$name; 0102 foreach($attrib as $key => $value) { 0103 $this->_content .= " $key=\"$value\""; 0104 } 0105 $this->_content .= ">"; 0106 } else { 0107 switch(strtolower($name)) { 0108 case 'termentry': 0109 $this->_termentry = null; 0110 break; 0111 case 'langset': 0112 if (isset($attrib['xml:lang']) === true) { 0113 $this->_langset = $attrib['xml:lang']; 0114 if (isset($this->_data[$this->_langset]) === false) { 0115 $this->_data[$this->_langset] = array(); 0116 } 0117 } 0118 break; 0119 case 'term': 0120 $this->_term = true; 0121 $this->_content = null; 0122 break; 0123 default: 0124 break; 0125 } 0126 } 0127 } 0128 0129 private function _endElement($file, $name) 0130 { 0131 if (($this->_term !== null) and ($name != "term")) { 0132 $this->_content .= "</".$name.">"; 0133 } else { 0134 switch (strtolower($name)) { 0135 case 'langset': 0136 $this->_langset = null; 0137 break; 0138 case 'term': 0139 $this->_term = null; 0140 if (empty($this->_termentry)) { 0141 $this->_termentry = $this->_content; 0142 } 0143 if (!empty($this->_content) or (isset($this->_data[$this->_langset][$this->_termentry]) === false)) { 0144 $this->_data[$this->_langset][$this->_termentry] = $this->_content; 0145 } 0146 break; 0147 default: 0148 break; 0149 } 0150 } 0151 } 0152 0153 private function _contentElement($file, $data) 0154 { 0155 if ($this->_term !== null) { 0156 $this->_content .= $data; 0157 } 0158 } 0159 0160 private function _findEncoding($filename) 0161 { 0162 $file = file_get_contents($filename, null, null, 0, 100); 0163 if (strpos($file, "encoding") !== false) { 0164 $encoding = substr($file, strpos($file, "encoding") + 9); 0165 $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1); 0166 return $encoding; 0167 } 0168 return 'UTF-8'; 0169 } 0170 0171 /** 0172 * Returns the adapter name 0173 * 0174 * @return string 0175 */ 0176 public function toString() 0177 { 0178 return "Tbx"; 0179 } 0180 }