00001 <?php
00024 require_once 'Zend/Locale.php';
00025
00027 require_once 'Zend/Translate/Adapter.php';
00028
00029
00036 class Zend_Translate_Adapter_Xliff 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 = false;
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 $encoding = $this->_findEncoding($filename);
00083 $this->_target = $locale;
00084 $this->_file = xml_parser_create($encoding);
00085 xml_set_object($this->_file, $this);
00086 xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
00087 xml_set_element_handler($this->_file, "_startElement", "_endElement");
00088 xml_set_character_data_handler($this->_file, "_contentElement");
00089
00090 if (!xml_parse($this->_file, file_get_contents($filename))) {
00091 $ex = sprintf('XML error: %s at line %d',
00092 xml_error_string(xml_get_error_code($this->_file)),
00093 xml_get_current_line_number($this->_file));
00094 xml_parser_free($this->_file);
00095 require_once 'Zend/Translate/Exception.php';
00096 throw new Zend_Translate_Exception($ex);
00097 }
00098
00099 return $this->_data;
00100 }
00101
00102 private function _startElement($file, $name, $attrib)
00103 {
00104 if ($this->_stag === true) {
00105 $this->_scontent .= "<".$name;
00106 foreach($attrib as $key => $value) {
00107 $this->_scontent .= " $key=\"$value\"";
00108 }
00109 $this->_scontent .= ">";
00110 } else if ($this->_ttag === true) {
00111 $this->_tcontent .= "<".$name;
00112 foreach($attrib as $key => $value) {
00113 $this->_tcontent .= " $key=\"$value\"";
00114 }
00115 $this->_tcontent .= ">";
00116 } else {
00117 switch(strtolower($name)) {
00118 case 'file':
00119 $this->_source = $attrib['source-language'];
00120 if (isset($attrib['target-language'])) {
00121 $this->_target = $attrib['target-language'];
00122 }
00123
00124 if (!isset($this->_data[$this->_source])) {
00125 $this->_data[$this->_source] = array();
00126 }
00127
00128 if (!isset($this->_data[$this->_target])) {
00129 $this->_data[$this->_target] = array();
00130 }
00131
00132 break;
00133 case 'trans-unit':
00134 $this->_transunit = true;
00135 break;
00136 case 'source':
00137 if ($this->_transunit === true) {
00138 $this->_scontent = null;
00139 $this->_stag = true;
00140 $this->_ttag = false;
00141 }
00142 break;
00143 case 'target':
00144 if ($this->_transunit === true) {
00145 $this->_tcontent = null;
00146 $this->_ttag = true;
00147 $this->_stag = false;
00148 }
00149 break;
00150 default:
00151 break;
00152 }
00153 }
00154 }
00155
00156 private function _endElement($file, $name)
00157 {
00158 if (($this->_stag === true) and ($name !== 'source')) {
00159 $this->_scontent .= "</".$name.">";
00160 } else if (($this->_ttag === true) and ($name !== 'target')) {
00161 $this->_tcontent .= "</".$name.">";
00162 } else {
00163 switch (strtolower($name)) {
00164 case 'trans-unit':
00165 $this->_transunit = null;
00166 $this->_scontent = null;
00167 $this->_tcontent = null;
00168 break;
00169 case 'source':
00170 if (!empty($this->_scontent) and !empty($this->_tcontent) or
00171 (isset($this->_data[$this->_source][$this->_scontent]) === false)) {
00172 $this->_data[$this->_source][$this->_scontent] = $this->_scontent;
00173 }
00174 $this->_stag = false;
00175 break;
00176 case 'target':
00177 if (!empty($this->_scontent) and !empty($this->_tcontent) or
00178 (isset($this->_data[$this->_source][$this->_scontent]) === false)) {
00179 $this->_data[$this->_target][$this->_scontent] = $this->_tcontent;
00180 }
00181 $this->_ttag = false;
00182 break;
00183 default:
00184 break;
00185 }
00186 }
00187 }
00188
00189 private function _contentElement($file, $data)
00190 {
00191 if (($this->_transunit !== null) and ($this->_source !== null) and ($this->_stag === true)) {
00192 $this->_scontent .= $data;
00193 }
00194
00195 if (($this->_transunit !== null) and ($this->_target !== null) and ($this->_ttag === true)) {
00196 $this->_tcontent .= $data;
00197 }
00198 }
00199
00200 private function _findEncoding($filename)
00201 {
00202 $file = file_get_contents($filename, null, null, 0, 100);
00203 if (strpos($file, "encoding") !== false) {
00204 $encoding = substr($file, strpos($file, "encoding") + 9);
00205 $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
00206 return $encoding;
00207 }
00208 return 'UTF-8';
00209 }
00210
00216 public function toString()
00217 {
00218 return "Xliff";
00219 }
00220 }