00001 <?php
00024 require_once 'Zend/Locale.php';
00025
00027 require_once 'Zend/Translate/Adapter.php';
00028
00029
00036 class Zend_Translate_Adapter_Qt extends Zend_Translate_Adapter {
00037
00038 private $_file = false;
00039 private $_cleared = array();
00040 private $_transunit = null;
00041 private $_source = null;
00042 private $_target = null;
00043 private $_scontent = null;
00044 private $_tcontent = null;
00045 private $_stag = false;
00046 private $_ttag = true;
00047 private $_data = array();
00048
00058 public function __construct($data, $locale = null, array $options = array())
00059 {
00060 parent::__construct($data, $locale, $options);
00061 }
00062
00063
00074 protected function _loadTranslationData($filename, $locale, array $options = array())
00075 {
00076 $this->_data = array();
00077 if (!is_readable($filename)) {
00078 require_once 'Zend/Translate/Exception.php';
00079 throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
00080 }
00081
00082 $this->_target = $locale;
00083
00084 $encoding = $this->_findEncoding($filename);
00085 $this->_file = xml_parser_create($encoding);
00086 xml_set_object($this->_file, $this);
00087 xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
00088 xml_set_element_handler($this->_file, "_startElement", "_endElement");
00089 xml_set_character_data_handler($this->_file, "_contentElement");
00090
00091 if (!xml_parse($this->_file, file_get_contents($filename))) {
00092 $ex = sprintf('XML error: %s at line %d',
00093 xml_error_string(xml_get_error_code($this->_file)),
00094 xml_get_current_line_number($this->_file));
00095 xml_parser_free($this->_file);
00096 require_once 'Zend/Translate/Exception.php';
00097 throw new Zend_Translate_Exception($ex);
00098 }
00099
00100 return $this->_data;
00101 }
00102
00103 private function _startElement($file, $name, $attrib)
00104 {
00105 switch(strtolower($name)) {
00106 case 'message':
00107 $this->_source = null;
00108 $this->_stag = false;
00109 $this->_ttag = false;
00110 $this->_scontent = null;
00111 $this->_tcontent = null;
00112 break;
00113 case 'source':
00114 $this->_stag = true;
00115 break;
00116 case 'translation':
00117 $this->_ttag = true;
00118 break;
00119 default:
00120 break;
00121 }
00122 }
00123
00124 private function _endElement($file, $name)
00125 {
00126 switch (strtolower($name)) {
00127 case 'source':
00128 $this->_stag = false;
00129 break;
00130
00131 case 'translation':
00132 if (!empty($this->_scontent) and !empty($this->_tcontent) or
00133 (isset($this->_data[$this->_target][$this->_scontent]) === false)) {
00134 $this->_data[$this->_target][$this->_scontent] = $this->_tcontent;
00135 }
00136 $this->_ttag = false;
00137 break;
00138
00139 default:
00140 break;
00141 }
00142 }
00143
00144 private function _contentElement($file, $data)
00145 {
00146 if ($this->_stag === true) {
00147 $this->_scontent .= $data;
00148 }
00149
00150 if ($this->_ttag === true) {
00151 $this->_tcontent .= $data;
00152 }
00153 }
00154
00155 private function _findEncoding($filename)
00156 {
00157 $file = file_get_contents($filename, null, null, 0, 100);
00158 if (strpos($file, "encoding") !== false) {
00159 $encoding = substr($file, strpos($file, "encoding") + 9);
00160 $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
00161 return $encoding;
00162 }
00163 return 'UTF-8';
00164 }
00165
00171 public function toString()
00172 {
00173 return "Qt";
00174 }
00175 }