|
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: Tbx.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_Tbx extends Zend_Translate_Adapter { |
|
37 // Internal variables |
|
38 private $_file = false; |
|
39 private $_cleared = array(); |
|
40 private $_langset = null; |
|
41 private $_termentry = null; |
|
42 private $_content = null; |
|
43 private $_term = null; |
|
44 private $_data = array(); |
|
45 |
|
46 /** |
|
47 * Load translation data (TBX file reader) |
|
48 * |
|
49 * @param string $filename TBX file to add, full path must be given for access |
|
50 * @param string $locale Locale has no effect for TBX because TBX defines all languages within |
|
51 * the source file |
|
52 * @param array $option OPTIONAL Options to use |
|
53 * @throws Zend_Translation_Exception |
|
54 * @return array |
|
55 */ |
|
56 protected function _loadTranslationData($filename, $locale, array $options = array()) |
|
57 { |
|
58 $this->_data = array(); |
|
59 if (!is_readable($filename)) { |
|
60 require_once 'Zend/Translate/Exception.php'; |
|
61 throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.'); |
|
62 } |
|
63 |
|
64 $encoding = $this->_findEncoding($filename); |
|
65 $this->_file = xml_parser_create($encoding); |
|
66 xml_set_object($this->_file, $this); |
|
67 xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0); |
|
68 xml_set_element_handler($this->_file, "_startElement", "_endElement"); |
|
69 xml_set_character_data_handler($this->_file, "_contentElement"); |
|
70 |
|
71 if (!xml_parse($this->_file, file_get_contents($filename))) { |
|
72 $ex = sprintf('XML error: %s at line %d', |
|
73 xml_error_string(xml_get_error_code($this->_file)), |
|
74 xml_get_current_line_number($this->_file)); |
|
75 xml_parser_free($this->_file); |
|
76 require_once 'Zend/Translate/Exception.php'; |
|
77 throw new Zend_Translate_Exception($ex); |
|
78 } |
|
79 |
|
80 return $this->_data; |
|
81 } |
|
82 |
|
83 private function _startElement($file, $name, $attrib) |
|
84 { |
|
85 if ($this->_term !== null) { |
|
86 $this->_content .= "<".$name; |
|
87 foreach($attrib as $key => $value) { |
|
88 $this->_content .= " $key=\"$value\""; |
|
89 } |
|
90 $this->_content .= ">"; |
|
91 } else { |
|
92 switch(strtolower($name)) { |
|
93 case 'termentry': |
|
94 $this->_termentry = null; |
|
95 break; |
|
96 case 'langset': |
|
97 if (isset($attrib['xml:lang']) === true) { |
|
98 $this->_langset = $attrib['xml:lang']; |
|
99 if (isset($this->_data[$this->_langset]) === false) { |
|
100 $this->_data[$this->_langset] = array(); |
|
101 } |
|
102 } |
|
103 break; |
|
104 case 'term': |
|
105 $this->_term = true; |
|
106 $this->_content = null; |
|
107 break; |
|
108 default: |
|
109 break; |
|
110 } |
|
111 } |
|
112 } |
|
113 |
|
114 private function _endElement($file, $name) |
|
115 { |
|
116 if (($this->_term !== null) and ($name != "term")) { |
|
117 $this->_content .= "</".$name.">"; |
|
118 } else { |
|
119 switch (strtolower($name)) { |
|
120 case 'langset': |
|
121 $this->_langset = null; |
|
122 break; |
|
123 case 'term': |
|
124 $this->_term = null; |
|
125 if (empty($this->_termentry)) { |
|
126 $this->_termentry = $this->_content; |
|
127 } |
|
128 if (!empty($this->_content) or (isset($this->_data[$this->_langset][$this->_termentry]) === false)) { |
|
129 $this->_data[$this->_langset][$this->_termentry] = $this->_content; |
|
130 } |
|
131 break; |
|
132 default: |
|
133 break; |
|
134 } |
|
135 } |
|
136 } |
|
137 |
|
138 private function _contentElement($file, $data) |
|
139 { |
|
140 if ($this->_term !== null) { |
|
141 $this->_content .= $data; |
|
142 } |
|
143 } |
|
144 |
|
145 private function _findEncoding($filename) |
|
146 { |
|
147 $file = file_get_contents($filename, null, null, 0, 100); |
|
148 if (strpos($file, "encoding") !== false) { |
|
149 $encoding = substr($file, strpos($file, "encoding") + 9); |
|
150 $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1); |
|
151 return $encoding; |
|
152 } |
|
153 return 'UTF-8'; |
|
154 } |
|
155 |
|
156 /** |
|
157 * Returns the adapter name |
|
158 * |
|
159 * @return string |
|
160 */ |
|
161 public function toString() |
|
162 { |
|
163 return "Tbx"; |
|
164 } |
|
165 } |