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.
16
* @package Zend_Translate
17
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
18
* @version $Id: Date.php 2498 2006-12-23 22:13:38Z thomas $
19
* @license http://framework.zend.com/license/new-bsd New BSD License
24
require_once 'Zend/Locale.php';
26
/** Zend_Translate_Adapter */
27
require_once 'Zend/Translate/Adapter.php';
32
* @package Zend_Translate
33
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
34
* @license http://framework.zend.com/license/new-bsd New BSD License
36
class Zend_Translate_Adapter_Xliff extends Zend_Translate_Adapter {
38
private $_file = false;
39
private $_cleared = array();
40
private $_transunit = null;
41
private $_source = null;
42
private $_target = null;
43
private $_scontent = null;
44
private $_tcontent = null;
45
private $_stag = false;
46
private $_ttag = false;
49
* Generates the xliff adapter
50
* This adapter reads with php's xml_parser
52
* @param string $data Translation data
53
* @param string|Zend_Locale $locale OPTIONAL Locale/Language to set, identical with locale identifier,
54
* see Zend_Locale for more information
55
* @param array $options OPTIONAL Options to set
57
public function __construct($data, $locale = null, array $options = array())
59
parent::__construct($data, $locale, $options);
64
* Load translation data (XLIFF file reader)
66
* @param string $locale Locale/Language to add data for, identical with locale identifier,
67
* see Zend_Locale for more information
68
* @param string $filename XLIFF file to add, full path must be given for access
69
* @param array $option OPTIONAL Options to use
70
* @throws Zend_Translation_Exception
72
protected function _loadTranslationData($filename, $locale, array $options = array())
74
$options = $options + $this->_options;
76
if ($options['clear']) {
77
$this->_translate = array();
80
if (!is_readable($filename)) {
81
require_once 'Zend/Translate/Exception.php';
82
throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
85
$encoding = $this->_findEncoding($filename);
86
$this->_target = $locale;
87
$this->_file = xml_parser_create($encoding);
88
xml_set_object($this->_file, $this);
89
xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
90
xml_set_element_handler($this->_file, "_startElement", "_endElement");
91
xml_set_character_data_handler($this->_file, "_contentElement");
93
if (!xml_parse($this->_file, file_get_contents($filename))) {
94
$ex = sprintf('XML error: %s at line %d',
95
xml_error_string(xml_get_error_code($this->_file)),
96
xml_get_current_line_number($this->_file));
97
xml_parser_free($this->_file);
98
require_once 'Zend/Translate/Exception.php';
99
throw new Zend_Translate_Exception($ex);
103
private function _startElement($file, $name, $attrib)
105
if ($this->_stag === true) {
106
$this->_scontent .= "<".$name;
107
foreach($attrib as $key => $value) {
108
$this->_scontent .= " $key=\"$value\"";
110
$this->_scontent .= ">";
111
} else if ($this->_ttag === true) {
112
$this->_tcontent .= "<".$name;
113
foreach($attrib as $key => $value) {
114
$this->_tcontent .= " $key=\"$value\"";
116
$this->_tcontent .= ">";
118
switch(strtolower($name)) {
120
$this->_source = $attrib['source-language'];
121
if (isset($attrib['target-language'])) {
122
$this->_target = $attrib['target-language'];
125
$this->_translate[$this->_source] = array();
126
$this->_translate[$this->_target] = array();
129
$this->_transunit = true;
132
if ($this->_transunit === true) {
133
$this->_scontent = null;
135
$this->_ttag = false;
139
if ($this->_transunit === true) {
140
$this->_tcontent = null;
142
$this->_stag = false;
151
private function _endElement($file, $name)
153
if (($this->_stag === true) and ($name !== 'source')) {
154
$this->_scontent .= "</".$name.">";
155
} else if (($this->_ttag === true) and ($name !== 'target')) {
156
$this->_tcontent .= "</".$name.">";
158
switch (strtolower($name)) {
160
$this->_transunit = null;
161
$this->_scontent = null;
162
$this->_tcontent = null;
165
if (!empty($this->_scontent) and !empty($this->_tcontent) or
166
(isset($this->_translate[$this->_source][$this->_scontent]) === false)) {
167
$this->_translate[$this->_source][$this->_scontent] = $this->_scontent;
169
$this->_stag = false;
172
if (!empty($this->_scontent) and !empty($this->_tcontent) or
173
(isset($this->_translate[$this->_source][$this->_scontent]) === false)) {
174
$this->_translate[$this->_target][$this->_scontent] = $this->_tcontent;
176
$this->_ttag = false;
184
private function _contentElement($file, $data)
186
if (($this->_transunit !== null) and ($this->_source !== null) and ($this->_stag === true)) {
187
$this->_scontent .= $data;
190
if (($this->_transunit !== null) and ($this->_target !== null) and ($this->_ttag === true)) {
191
$this->_tcontent .= $data;
195
private function _findEncoding($filename)
197
$file = file_get_contents($filename, null, null, 0, 100);
198
if (strpos($file, "encoding") !== false) {
199
$encoding = substr($file, strpos($file, "encoding") + 9);
200
$encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
207
* Returns the adapter name
211
public function toString()