|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Translate |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @version $Id: Xliff.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 */ |
|
21 |
|
22 |
|
23 /** Zend_Locale */ |
|
24 require_once 'Zend/Locale.php'; |
|
25 |
|
26 /** Zend_Translate_Adapter */ |
|
27 require_once 'Zend/Translate/Adapter.php'; |
|
28 |
|
29 |
|
30 /** |
|
31 * @category Zend |
|
32 * @package Zend_Translate |
|
33 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
34 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
35 */ |
|
36 class Zend_Translate_Adapter_Xliff extends Zend_Translate_Adapter { |
|
37 // Internal variables |
|
38 private $_file = false; |
|
39 private $_useId = true; |
|
40 private $_cleared = array(); |
|
41 private $_transunit = null; |
|
42 private $_source = null; |
|
43 private $_target = null; |
|
44 private $_langId = null; |
|
45 private $_scontent = null; |
|
46 private $_tcontent = null; |
|
47 private $_stag = false; |
|
48 private $_ttag = false; |
|
49 private $_data = array(); |
|
50 |
|
51 /** |
|
52 * Load translation data (XLIFF file reader) |
|
53 * |
|
54 * @param string $locale Locale/Language to add data for, identical with locale identifier, |
|
55 * see Zend_Locale for more information |
|
56 * @param string $filename XLIFF file to add, full path must be given for access |
|
57 * @param array $option OPTIONAL Options to use |
|
58 * @throws Zend_Translation_Exception |
|
59 * @return array |
|
60 */ |
|
61 protected function _loadTranslationData($filename, $locale, array $options = array()) |
|
62 { |
|
63 $this->_data = array(); |
|
64 if (!is_readable($filename)) { |
|
65 require_once 'Zend/Translate/Exception.php'; |
|
66 throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.'); |
|
67 } |
|
68 |
|
69 if (empty($options['useId'])) { |
|
70 $this->_useId = false; |
|
71 } else { |
|
72 $this->_useId = true; |
|
73 } |
|
74 |
|
75 $encoding = $this->_findEncoding($filename); |
|
76 $this->_target = $locale; |
|
77 $this->_file = xml_parser_create($encoding); |
|
78 xml_set_object($this->_file, $this); |
|
79 xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0); |
|
80 xml_set_element_handler($this->_file, "_startElement", "_endElement"); |
|
81 xml_set_character_data_handler($this->_file, "_contentElement"); |
|
82 |
|
83 if (!xml_parse($this->_file, file_get_contents($filename))) { |
|
84 $ex = sprintf('XML error: %s at line %d', |
|
85 xml_error_string(xml_get_error_code($this->_file)), |
|
86 xml_get_current_line_number($this->_file)); |
|
87 xml_parser_free($this->_file); |
|
88 require_once 'Zend/Translate/Exception.php'; |
|
89 throw new Zend_Translate_Exception($ex); |
|
90 } |
|
91 |
|
92 return $this->_data; |
|
93 } |
|
94 |
|
95 private function _startElement($file, $name, $attrib) |
|
96 { |
|
97 if ($this->_stag === true) { |
|
98 $this->_scontent .= "<".$name; |
|
99 foreach($attrib as $key => $value) { |
|
100 $this->_scontent .= " $key=\"$value\""; |
|
101 } |
|
102 $this->_scontent .= ">"; |
|
103 } else if ($this->_ttag === true) { |
|
104 $this->_tcontent .= "<".$name; |
|
105 foreach($attrib as $key => $value) { |
|
106 $this->_tcontent .= " $key=\"$value\""; |
|
107 } |
|
108 $this->_tcontent .= ">"; |
|
109 } else { |
|
110 switch(strtolower($name)) { |
|
111 case 'file': |
|
112 $this->_source = $attrib['source-language']; |
|
113 if (isset($attrib['target-language'])) { |
|
114 $this->_target = $attrib['target-language']; |
|
115 } |
|
116 |
|
117 if (!isset($this->_data[$this->_source])) { |
|
118 $this->_data[$this->_source] = array(); |
|
119 } |
|
120 |
|
121 if (!isset($this->_data[$this->_target])) { |
|
122 $this->_data[$this->_target] = array(); |
|
123 } |
|
124 |
|
125 break; |
|
126 case 'trans-unit': |
|
127 $this->_transunit = true; |
|
128 $this->_langId = $attrib['id']; |
|
129 break; |
|
130 case 'source': |
|
131 if ($this->_transunit === true) { |
|
132 $this->_scontent = null; |
|
133 $this->_stag = true; |
|
134 $this->_ttag = false; |
|
135 } |
|
136 break; |
|
137 case 'target': |
|
138 if ($this->_transunit === true) { |
|
139 $this->_tcontent = null; |
|
140 $this->_ttag = true; |
|
141 $this->_stag = false; |
|
142 } |
|
143 break; |
|
144 default: |
|
145 break; |
|
146 } |
|
147 } |
|
148 } |
|
149 |
|
150 private function _endElement($file, $name) |
|
151 { |
|
152 if (($this->_stag === true) and ($name !== 'source')) { |
|
153 $this->_scontent .= "</".$name.">"; |
|
154 } else if (($this->_ttag === true) and ($name !== 'target')) { |
|
155 $this->_tcontent .= "</".$name.">"; |
|
156 } else { |
|
157 switch (strtolower($name)) { |
|
158 case 'trans-unit': |
|
159 $this->_transunit = null; |
|
160 $this->_langId = null; |
|
161 $this->_scontent = null; |
|
162 $this->_tcontent = null; |
|
163 break; |
|
164 case 'source': |
|
165 if ($this->_useId) { |
|
166 if (!empty($this->_scontent) && !empty($this->_langId) && |
|
167 !isset($this->_data[$this->_source][$this->_langId])) { |
|
168 $this->_data[$this->_source][$this->_langId] = $this->_scontent; |
|
169 } |
|
170 } else { |
|
171 if (!empty($this->_scontent) && |
|
172 !isset($this->_data[$this->_source][$this->_scontent])) { |
|
173 $this->_data[$this->_source][$this->_scontent] = $this->_scontent; |
|
174 } |
|
175 } |
|
176 $this->_stag = false; |
|
177 break; |
|
178 case 'target': |
|
179 if ($this->_useId) { |
|
180 if (!empty($this->_tcontent) && !empty($this->_langId) && |
|
181 !isset($this->_data[$this->_target][$this->_langId])) { |
|
182 $this->_data[$this->_target][$this->_langId] = $this->_tcontent; |
|
183 } |
|
184 } else { |
|
185 if (!empty($this->_tcontent) && !empty($this->_scontent) && |
|
186 !isset($this->_data[$this->_target][$this->_scontent])) { |
|
187 $this->_data[$this->_target][$this->_scontent] = $this->_tcontent; |
|
188 } |
|
189 } |
|
190 $this->_ttag = false; |
|
191 break; |
|
192 default: |
|
193 break; |
|
194 } |
|
195 } |
|
196 } |
|
197 |
|
198 private function _contentElement($file, $data) |
|
199 { |
|
200 if (($this->_transunit !== null) and ($this->_source !== null) and ($this->_stag === true)) { |
|
201 $this->_scontent .= $data; |
|
202 } |
|
203 |
|
204 if (($this->_transunit !== null) and ($this->_target !== null) and ($this->_ttag === true)) { |
|
205 $this->_tcontent .= $data; |
|
206 } |
|
207 } |
|
208 |
|
209 private function _findEncoding($filename) |
|
210 { |
|
211 $file = file_get_contents($filename, null, null, 0, 100); |
|
212 if (strpos($file, "encoding") !== false) { |
|
213 $encoding = substr($file, strpos($file, "encoding") + 9); |
|
214 $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1); |
|
215 return $encoding; |
|
216 } |
|
217 return 'UTF-8'; |
|
218 } |
|
219 |
|
220 /** |
|
221 * Returns the adapter name |
|
222 * |
|
223 * @return string |
|
224 */ |
|
225 public function toString() |
|
226 { |
|
227 return "Xliff"; |
|
228 } |
|
229 } |