File indexing completed on 2025-05-04 05:28:28
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Translate 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @version $Id$ 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 */ 0021 0022 0023 /** Zend_Locale */ 0024 // require_once 'Zend/Locale.php'; 0025 0026 /** Zend_Translate_Adapter */ 0027 // require_once 'Zend/Translate/Adapter.php'; 0028 0029 /** @see Zend_Xml_Security */ 0030 // require_once 'Zend/Xml/Security.php'; 0031 0032 /** @See Zend_Xml_Exception */ 0033 // require_once 'Zend/Xml/Exception.php'; 0034 0035 /** 0036 * @category Zend 0037 * @package Zend_Translate 0038 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0039 * @license http://framework.zend.com/license/new-bsd New BSD License 0040 */ 0041 class Zend_Translate_Adapter_Xliff extends Zend_Translate_Adapter { 0042 // Internal variables 0043 private $_file = false; 0044 private $_useId = true; 0045 private $_cleared = array(); 0046 private $_transunit = null; 0047 private $_source = null; 0048 private $_target = null; 0049 private $_langId = null; 0050 private $_scontent = null; 0051 private $_tcontent = null; 0052 private $_stag = false; 0053 private $_ttag = false; 0054 private $_data = array(); 0055 0056 /** 0057 * Load translation data (XLIFF file reader) 0058 * 0059 * @param string $locale Locale/Language to add data for, identical with locale identifier, 0060 * see Zend_Locale for more information 0061 * @param string $filename XLIFF file to add, full path must be given for access 0062 * @param array $option OPTIONAL Options to use 0063 * @throws Zend_Translation_Exception 0064 * @return array 0065 */ 0066 protected function _loadTranslationData($filename, $locale, array $options = array()) 0067 { 0068 $this->_data = array(); 0069 if (!is_readable($filename)) { 0070 // require_once 'Zend/Translate/Exception.php'; 0071 throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.'); 0072 } 0073 0074 if (empty($options['useId'])) { 0075 $this->_useId = false; 0076 } else { 0077 $this->_useId = true; 0078 } 0079 0080 $encoding = $this->_findEncoding($filename); 0081 $this->_target = $locale; 0082 $this->_file = xml_parser_create($encoding); 0083 xml_set_object($this->_file, $this); 0084 xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0); 0085 xml_set_element_handler($this->_file, "_startElement", "_endElement"); 0086 xml_set_character_data_handler($this->_file, "_contentElement"); 0087 0088 try { 0089 Zend_Xml_Security::scanFile($filename); 0090 } catch (Zend_Xml_Exception $e) { 0091 // require_once 'Zend/Translate/Exception.php'; 0092 throw new Zend_Translate_Exception( 0093 $e->getMessage() 0094 ); 0095 } 0096 0097 if (!xml_parse($this->_file, file_get_contents($filename))) { 0098 $ex = sprintf('XML error: %s at line %d of file %s', 0099 xml_error_string(xml_get_error_code($this->_file)), 0100 xml_get_current_line_number($this->_file), 0101 $filename); 0102 xml_parser_free($this->_file); 0103 // require_once 'Zend/Translate/Exception.php'; 0104 throw new Zend_Translate_Exception($ex); 0105 } 0106 0107 return $this->_data; 0108 } 0109 0110 private function _startElement($file, $name, $attrib) 0111 { 0112 if ($this->_stag === true) { 0113 $this->_scontent .= "<".$name; 0114 foreach($attrib as $key => $value) { 0115 $this->_scontent .= " $key=\"$value\""; 0116 } 0117 $this->_scontent .= ">"; 0118 } else if ($this->_ttag === true) { 0119 $this->_tcontent .= "<".$name; 0120 foreach($attrib as $key => $value) { 0121 $this->_tcontent .= " $key=\"$value\""; 0122 } 0123 $this->_tcontent .= ">"; 0124 } else { 0125 switch(strtolower($name)) { 0126 case 'file': 0127 $this->_source = $attrib['source-language']; 0128 if (isset($attrib['target-language'])) { 0129 $this->_target = $attrib['target-language']; 0130 } 0131 0132 if (!isset($this->_data[$this->_source])) { 0133 $this->_data[$this->_source] = array(); 0134 } 0135 0136 if (!isset($this->_data[$this->_target])) { 0137 $this->_data[$this->_target] = array(); 0138 } 0139 0140 break; 0141 case 'trans-unit': 0142 $this->_transunit = true; 0143 $this->_langId = $attrib['id']; 0144 break; 0145 case 'source': 0146 if ($this->_transunit === true) { 0147 $this->_scontent = null; 0148 $this->_stag = true; 0149 $this->_ttag = false; 0150 } 0151 break; 0152 case 'target': 0153 if ($this->_transunit === true) { 0154 $this->_tcontent = null; 0155 $this->_ttag = true; 0156 $this->_stag = false; 0157 } 0158 break; 0159 default: 0160 break; 0161 } 0162 } 0163 } 0164 0165 private function _endElement($file, $name) 0166 { 0167 if (($this->_stag === true) and ($name !== 'source')) { 0168 $this->_scontent .= "</".$name.">"; 0169 } else if (($this->_ttag === true) and ($name !== 'target')) { 0170 $this->_tcontent .= "</".$name.">"; 0171 } else { 0172 switch (strtolower($name)) { 0173 case 'trans-unit': 0174 $this->_transunit = null; 0175 $this->_langId = null; 0176 $this->_scontent = null; 0177 $this->_tcontent = null; 0178 break; 0179 case 'source': 0180 if ($this->_useId) { 0181 if (!empty($this->_scontent) && !empty($this->_langId) && 0182 !isset($this->_data[$this->_source][$this->_langId])) { 0183 $this->_data[$this->_source][$this->_langId] = $this->_scontent; 0184 } 0185 } else { 0186 if (!empty($this->_scontent) && 0187 !isset($this->_data[$this->_source][$this->_scontent])) { 0188 $this->_data[$this->_source][$this->_scontent] = $this->_scontent; 0189 } 0190 } 0191 $this->_stag = false; 0192 break; 0193 case 'target': 0194 if ($this->_useId) { 0195 if (!empty($this->_tcontent) && !empty($this->_langId) && 0196 !isset($this->_data[$this->_target][$this->_langId])) { 0197 $this->_data[$this->_target][$this->_langId] = $this->_tcontent; 0198 } 0199 } else { 0200 if (!empty($this->_tcontent) && !empty($this->_scontent) && 0201 !isset($this->_data[$this->_target][$this->_scontent])) { 0202 $this->_data[$this->_target][$this->_scontent] = $this->_tcontent; 0203 } 0204 } 0205 $this->_ttag = false; 0206 break; 0207 default: 0208 break; 0209 } 0210 } 0211 } 0212 0213 private function _contentElement($file, $data) 0214 { 0215 if (($this->_transunit !== null) and ($this->_source !== null) and ($this->_stag === true)) { 0216 $this->_scontent .= $data; 0217 } 0218 0219 if (($this->_transunit !== null) and ($this->_target !== null) and ($this->_ttag === true)) { 0220 $this->_tcontent .= $data; 0221 } 0222 } 0223 0224 private function _findEncoding($filename) 0225 { 0226 $file = file_get_contents($filename, null, null, 0, 100); 0227 if (strpos($file, "encoding") !== false) { 0228 $encoding = substr($file, strpos($file, "encoding") + 9); 0229 $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1); 0230 return $encoding; 0231 } 0232 return 'UTF-8'; 0233 } 0234 0235 /** 0236 * Returns the adapter name 0237 * 0238 * @return string 0239 */ 0240 public function toString() 0241 { 0242 return "Xliff"; 0243 } 0244 }