00001 <?php
00024 require_once 'Zend/Locale.php';
00025
00027 require_once 'Zend/Translate/Adapter.php';
00028
00029
00036 class Zend_Translate_Adapter_Tmx extends Zend_Translate_Adapter {
00037
00038 private $_file = false;
00039 private $_tu = null;
00040 private $_tuv = null;
00041 private $_seg = null;
00042 private $_content = null;
00043 private $_data = array();
00044
00054 public function __construct($data, $locale = null, array $options = array())
00055 {
00056 parent::__construct($data, $locale, $options);
00057 }
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->_seg !== 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 'tu':
00108 if (isset($attrib['tuid']) === true) {
00109 $this->_tu = $attrib['tuid'];
00110 }
00111 break;
00112 case 'tuv':
00113 if (isset($attrib['xml:lang']) === true) {
00114 $this->_tuv = $attrib['xml:lang'];
00115 if (isset($this->_data[$this->_tuv]) === false) {
00116 $this->_data[$this->_tuv] = array();
00117 }
00118 }
00119 break;
00120 case 'seg':
00121 $this->_seg = true;
00122 $this->_content = null;
00123 break;
00124 default:
00125 break;
00126 }
00127 }
00128 }
00129
00130 private function _endElement($file, $name)
00131 {
00132 if (($this->_seg !== null) and ($name !== 'seg')) {
00133 $this->_content .= "</".$name.">";
00134 } else {
00135 switch (strtolower($name)) {
00136 case 'tu':
00137 $this->_tu = null;
00138 break;
00139 case 'tuv':
00140 $this->_tuv = null;
00141 break;
00142 case 'seg':
00143 $this->_seg = null;
00144 if (!empty($this->_content) or (!isset($this->_data[$this->_tuv][$this->_tu]))) {
00145 $this->_data[$this->_tuv][$this->_tu] = $this->_content;
00146 }
00147 break;
00148 default:
00149 break;
00150 }
00151 }
00152 }
00153
00154 private function _contentElement($file, $data)
00155 {
00156 if (($this->_seg !== null) and ($this->_tu !== null) and ($this->_tuv !== null)) {
00157 $this->_content .= $data;
00158 }
00159 }
00160
00161 private function _findEncoding($filename)
00162 {
00163 $file = file_get_contents($filename, null, null, 0, 100);
00164 if (strpos($file, "encoding") !== false) {
00165 $encoding = substr($file, strpos($file, "encoding") + 9);
00166 $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
00167 return $encoding;
00168 }
00169 return 'UTF-8';
00170 }
00171
00177 public function toString()
00178 {
00179 return "Tmx";
00180 }
00181 }