24 #require_once 'Zend/Locale.php'; 27 #require_once 'Zend/Translate/Adapter.php'; 30 #require_once 'Zend/Xml/Security.php'; 33 #require_once 'Zend/Xml/Exception.php'; 43 private $_file =
false;
44 private $_useId =
true;
45 private $_cleared = array();
46 private $_transunit =
null;
47 private $_source =
null;
48 private $_target =
null;
49 private $_langId =
null;
50 private $_scontent =
null;
51 private $_tcontent =
null;
52 private $_stag =
false;
53 private $_ttag =
false;
54 private $_data = array();
68 $this->_data = array();
70 #require_once 'Zend/Translate/Exception.php'; 74 if (empty($options['useId
'])) { 75 $this->_useId = false; 80 $encoding = $this->_findEncoding($filename); 81 $this->_target = $locale; 82 $this->_file = xml_parser_create($encoding); 83 xml_set_object($this->_file, $this); 84 xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0); 85 xml_set_element_handler($this->_file, "_startElement", "_endElement"); 86 xml_set_character_data_handler($this->_file, "_contentElement"); 89 Zend_Xml_Security::scanFile($filename); 90 } catch (Zend_Xml_Exception $e) { 91 #require_once 'Zend/Translate/Exception.php
'; 92 throw new Zend_Translate_Exception( 97 if (!xml_parse($this->_file, file_get_contents($filename))) { 98 $ex = sprintf('XML error: %s at line %d of file %s
', 99 xml_error_string(xml_get_error_code($this->_file)), 100 xml_get_current_line_number($this->_file), 102 xml_parser_free($this->_file); 103 #require_once 'Zend/Translate/Exception.php
'; 104 throw new Zend_Translate_Exception($ex); 110 private function _startElement($file, $name, $attrib) 112 if ($this->_stag === true) { 113 $this->_scontent .= "<".$name; 114 foreach($attrib as $key => $value) { 115 $this->_scontent .= " $key=\"$value\""; 117 $this->_scontent .= ">"; 118 } else if ($this->_ttag === true) { 119 $this->_tcontent .= "<".$name; 120 foreach($attrib as $key => $value) { 121 $this->_tcontent .= " $key=\"$value\""; 123 $this->_tcontent .= ">"; 125 switch(strtolower($name)) { 127 $this->_source = $attrib['source-language
']; 128 if (isset($attrib['target-language
'])) { 129 $this->_target = $attrib['target-language
']; 132 if (!isset($this->_data[$this->_source])) { 133 $this->_data[$this->_source] = array(); 136 if (!isset($this->_data[$this->_target])) { 137 $this->_data[$this->_target] = array(); 142 $this->_transunit = true; 143 $this->_langId = $attrib['id']; 146 if ($this->_transunit === true) { 147 $this->_scontent = null; 149 $this->_ttag = false; 153 if ($this->_transunit === true) { 154 $this->_tcontent = null; 156 $this->_stag = false; 165 private function _endElement($file, $name) 167 if (($this->_stag === true) and ($name !== 'source
')) { 168 $this->_scontent .= "</".$name.">"; 169 } else if (($this->_ttag === true) and ($name !== 'target
')) { 170 $this->_tcontent .= "</".$name.">"; 172 switch (strtolower($name)) { 174 $this->_transunit = null; 175 $this->_langId = null; 176 $this->_scontent = null; 177 $this->_tcontent = null; 181 if (!empty($this->_scontent) && !empty($this->_langId) && 182 !isset($this->_data[$this->_source][$this->_langId])) { 183 $this->_data[$this->_source][$this->_langId] = $this->_scontent; 186 if (!empty($this->_scontent) && 187 !isset($this->_data[$this->_source][$this->_scontent])) { 188 $this->_data[$this->_source][$this->_scontent] = $this->_scontent; 191 $this->_stag = false; 195 if (!empty($this->_tcontent) && !empty($this->_langId) && 196 !isset($this->_data[$this->_target][$this->_langId])) { 197 $this->_data[$this->_target][$this->_langId] = $this->_tcontent; 200 if (!empty($this->_tcontent) && !empty($this->_scontent) && 201 !isset($this->_data[$this->_target][$this->_scontent])) { 202 $this->_data[$this->_target][$this->_scontent] = $this->_tcontent; 205 $this->_ttag = false; 213 private function _contentElement($file, $data) 215 if (($this->_transunit !== null) and ($this->_source !== null) and ($this->_stag === true)) { 216 $this->_scontent .= $data; 219 if (($this->_transunit !== null) and ($this->_target !== null) and ($this->_ttag === true)) { 220 $this->_tcontent .= $data; 224 private function _findEncoding($filename) 226 $file = file_get_contents($filename, null, null, 0, 100); 227 if (strpos($file, "encoding") !== false) { 228 $encoding = substr($file, strpos($file, "encoding") + 9); 229 $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1); 240 public function toString()
_loadTranslationData($filename, $locale, array $options=array())