|
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: Tmx.php 21049 2010-02-13 22:52:52Z thomas $ |
|
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_Tmx extends Zend_Translate_Adapter { |
|
37 // Internal variables |
|
38 private $_file = false; |
|
39 private $_useId = true; |
|
40 private $_srclang = null; |
|
41 private $_tu = null; |
|
42 private $_tuv = null; |
|
43 private $_seg = null; |
|
44 private $_content = null; |
|
45 private $_data = array(); |
|
46 |
|
47 /** |
|
48 * Load translation data (TMX file reader) |
|
49 * |
|
50 * @param string $filename TMX file to add, full path must be given for access |
|
51 * @param string $locale Locale has no effect for TMX because TMX defines all languages within |
|
52 * the source file |
|
53 * @param array $option OPTIONAL Options to use |
|
54 * @throws Zend_Translation_Exception |
|
55 * @return array |
|
56 */ |
|
57 protected function _loadTranslationData($filename, $locale, array $options = array()) |
|
58 { |
|
59 $this->_data = array(); |
|
60 if (!is_readable($filename)) { |
|
61 require_once 'Zend/Translate/Exception.php'; |
|
62 throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.'); |
|
63 } |
|
64 |
|
65 if (isset($options['useId'])) { |
|
66 $this->_useId = (boolean) $options['useId']; |
|
67 } |
|
68 |
|
69 $encoding = $this->_findEncoding($filename); |
|
70 $this->_file = xml_parser_create($encoding); |
|
71 xml_set_object($this->_file, $this); |
|
72 xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0); |
|
73 xml_set_element_handler($this->_file, "_startElement", "_endElement"); |
|
74 xml_set_character_data_handler($this->_file, "_contentElement"); |
|
75 |
|
76 if (!xml_parse($this->_file, file_get_contents($filename))) { |
|
77 $ex = sprintf('XML error: %s at line %d', |
|
78 xml_error_string(xml_get_error_code($this->_file)), |
|
79 xml_get_current_line_number($this->_file)); |
|
80 xml_parser_free($this->_file); |
|
81 require_once 'Zend/Translate/Exception.php'; |
|
82 throw new Zend_Translate_Exception($ex); |
|
83 } |
|
84 |
|
85 return $this->_data; |
|
86 } |
|
87 |
|
88 /** |
|
89 * Internal method, called by xml element handler at start |
|
90 * |
|
91 * @param resource $file File handler |
|
92 * @param string $name Elements name |
|
93 * @param array $attrib Attributes for this element |
|
94 */ |
|
95 protected function _startElement($file, $name, $attrib) |
|
96 { |
|
97 if ($this->_seg !== null) { |
|
98 $this->_content .= "<".$name; |
|
99 foreach($attrib as $key => $value) { |
|
100 $this->_content .= " $key=\"$value\""; |
|
101 } |
|
102 $this->_content .= ">"; |
|
103 } else { |
|
104 switch(strtolower($name)) { |
|
105 case 'header': |
|
106 if (empty($this->_useId) && isset($attrib['srclang'])) { |
|
107 if (Zend_Locale::isLocale($attrib['srclang'])) { |
|
108 $this->_srclang = Zend_Locale::findLocale($attrib['srclang']); |
|
109 } else { |
|
110 if (!$this->_options['disableNotices']) { |
|
111 if ($this->_options['log']) { |
|
112 $this->_options['log']->notice("The language '{$attrib['srclang']}' can not be set because it does not exist."); |
|
113 } else { |
|
114 trigger_error("The language '{$attrib['srclang']}' can not be set because it does not exist.", E_USER_NOTICE); |
|
115 } |
|
116 } |
|
117 |
|
118 $this->_srclang = $attrib['srclang']; |
|
119 } |
|
120 } |
|
121 break; |
|
122 case 'tu': |
|
123 if (isset($attrib['tuid'])) { |
|
124 $this->_tu = $attrib['tuid']; |
|
125 } |
|
126 break; |
|
127 case 'tuv': |
|
128 if (isset($attrib['xml:lang'])) { |
|
129 if (Zend_Locale::isLocale($attrib['xml:lang'])) { |
|
130 $this->_tuv = Zend_Locale::findLocale($attrib['xml:lang']); |
|
131 } else { |
|
132 if (!$this->_options['disableNotices']) { |
|
133 if ($this->_options['log']) { |
|
134 $this->_options['log']->notice("The language '{$attrib['xml:lang']}' can not be set because it does not exist."); |
|
135 } else { |
|
136 trigger_error("The language '{$attrib['xml:lang']}' can not be set because it does not exist.", E_USER_NOTICE); |
|
137 } |
|
138 } |
|
139 |
|
140 $this->_tuv = $attrib['xml:lang']; |
|
141 } |
|
142 |
|
143 if (!isset($this->_data[$this->_tuv])) { |
|
144 $this->_data[$this->_tuv] = array(); |
|
145 } |
|
146 } |
|
147 break; |
|
148 case 'seg': |
|
149 $this->_seg = true; |
|
150 $this->_content = null; |
|
151 break; |
|
152 default: |
|
153 break; |
|
154 } |
|
155 } |
|
156 } |
|
157 |
|
158 |
|
159 /** |
|
160 * Internal method, called by xml element handler at end |
|
161 * |
|
162 * @param resource $file File handler |
|
163 * @param string $name Elements name |
|
164 */ |
|
165 protected function _endElement($file, $name) |
|
166 { |
|
167 if (($this->_seg !== null) and ($name !== 'seg')) { |
|
168 $this->_content .= "</".$name.">"; |
|
169 } else { |
|
170 switch (strtolower($name)) { |
|
171 case 'tu': |
|
172 $this->_tu = null; |
|
173 break; |
|
174 case 'tuv': |
|
175 $this->_tuv = null; |
|
176 break; |
|
177 case 'seg': |
|
178 $this->_seg = null; |
|
179 if (!empty($this->_srclang) && ($this->_srclang == $this->_tuv)) { |
|
180 $this->_tu = $this->_content; |
|
181 } |
|
182 |
|
183 if (!empty($this->_content) or (!isset($this->_data[$this->_tuv][$this->_tu]))) { |
|
184 $this->_data[$this->_tuv][$this->_tu] = $this->_content; |
|
185 } |
|
186 break; |
|
187 default: |
|
188 break; |
|
189 } |
|
190 } |
|
191 } |
|
192 |
|
193 /** |
|
194 * Internal method, called by xml element handler for content |
|
195 * |
|
196 * @param resource $file File handler |
|
197 * @param string $data Elements content |
|
198 */ |
|
199 protected function _contentElement($file, $data) |
|
200 { |
|
201 if (($this->_seg !== null) and ($this->_tu !== null) and ($this->_tuv !== null)) { |
|
202 $this->_content .= $data; |
|
203 } |
|
204 } |
|
205 |
|
206 |
|
207 /** |
|
208 * Internal method, detects the encoding of the xml file |
|
209 * |
|
210 * @param string $name Filename |
|
211 * @return string Encoding |
|
212 */ |
|
213 protected function _findEncoding($filename) |
|
214 { |
|
215 $file = file_get_contents($filename, null, null, 0, 100); |
|
216 if (strpos($file, "encoding") !== false) { |
|
217 $encoding = substr($file, strpos($file, "encoding") + 9); |
|
218 $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1); |
|
219 return $encoding; |
|
220 } |
|
221 return 'UTF-8'; |
|
222 } |
|
223 |
|
224 /** |
|
225 * Returns the adapter name |
|
226 * |
|
227 * @return string |
|
228 */ |
|
229 public function toString() |
|
230 { |
|
231 return "Tmx"; |
|
232 } |
|
233 } |