~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Translate/Adapter/Xliff.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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-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
 
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-2008 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 $_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;
 
47
 
 
48
    /**
 
49
     * Generates the xliff adapter
 
50
     * This adapter reads with php's xml_parser
 
51
     *
 
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
 
56
     */
 
57
    public function __construct($data, $locale = null, array $options = array())
 
58
    {
 
59
        parent::__construct($data, $locale, $options);
 
60
    }
 
61
 
 
62
 
 
63
    /**
 
64
     * Load translation data (XLIFF file reader)
 
65
     *
 
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
 
71
     */
 
72
    protected function _loadTranslationData($filename, $locale, array $options = array())
 
73
    {
 
74
        $options = $options + $this->_options;
 
75
 
 
76
        if ($options['clear']) {
 
77
            $this->_translate = array();
 
78
        }
 
79
 
 
80
        if (!is_readable($filename)) {
 
81
            require_once 'Zend/Translate/Exception.php';
 
82
            throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
 
83
        }
 
84
 
 
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");
 
92
 
 
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);
 
100
        }
 
101
    }
 
102
 
 
103
    private function _startElement($file, $name, $attrib)
 
104
    {
 
105
        if ($this->_stag === true) {
 
106
            $this->_scontent .= "<".$name;
 
107
            foreach($attrib as $key => $value) {
 
108
                $this->_scontent .= " $key=\"$value\"";
 
109
            }
 
110
            $this->_scontent .= ">";
 
111
        } else if ($this->_ttag === true) {
 
112
            $this->_tcontent .= "<".$name;
 
113
            foreach($attrib as $key => $value) {
 
114
                $this->_tcontent .= " $key=\"$value\"";
 
115
            }
 
116
            $this->_tcontent .= ">";
 
117
        } else {
 
118
            switch(strtolower($name)) {
 
119
                case 'file':
 
120
                    $this->_source = $attrib['source-language'];
 
121
                    if (isset($attrib['target-language'])) {
 
122
                        $this->_target = $attrib['target-language'];
 
123
                    }
 
124
 
 
125
                    $this->_translate[$this->_source] = array();
 
126
                    $this->_translate[$this->_target] = array();
 
127
                    break;
 
128
                case 'trans-unit':
 
129
                    $this->_transunit = true;
 
130
                    break;
 
131
                case 'source':
 
132
                    if ($this->_transunit === true) {
 
133
                        $this->_scontent = null;
 
134
                        $this->_stag = true;
 
135
                        $this->_ttag = false;
 
136
                    }
 
137
                    break;
 
138
                case 'target':
 
139
                    if ($this->_transunit === true) {
 
140
                        $this->_tcontent = null;
 
141
                        $this->_ttag = true;
 
142
                        $this->_stag = false;
 
143
                    }
 
144
                    break;
 
145
                default:
 
146
                    break;
 
147
            }
 
148
        }
 
149
    }
 
150
 
 
151
    private function _endElement($file, $name)
 
152
    {
 
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.">";
 
157
        } else {
 
158
            switch (strtolower($name)) {
 
159
                case 'trans-unit':
 
160
                    $this->_transunit = null;
 
161
                    $this->_scontent = null;
 
162
                    $this->_tcontent = null;
 
163
                    break;
 
164
                case 'source':
 
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;
 
168
                    }
 
169
                    $this->_stag = false;
 
170
                    break;
 
171
                case 'target':
 
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;
 
175
                    }
 
176
                    $this->_ttag = false;
 
177
                    break;
 
178
                default:
 
179
                    break;
 
180
            }
 
181
        }
 
182
    }
 
183
 
 
184
    private function _contentElement($file, $data)
 
185
    {
 
186
        if (($this->_transunit !== null) and ($this->_source !== null) and ($this->_stag === true)) {
 
187
            $this->_scontent .= $data;
 
188
        }
 
189
 
 
190
        if (($this->_transunit !== null) and ($this->_target !== null) and ($this->_ttag === true)) {
 
191
            $this->_tcontent .= $data;
 
192
        }
 
193
    }
 
194
 
 
195
    private function _findEncoding($filename)
 
196
    {
 
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);
 
201
            return $encoding;
 
202
        }
 
203
        return 'UTF-8';
 
204
    }
 
205
 
 
206
    /**
 
207
     * Returns the adapter name
 
208
     *
 
209
     * @return string
 
210
     */
 
211
    public function toString()
 
212
    {
 
213
        return "Xliff";
 
214
    }
 
215
}