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