PageRenderTime 204ms CodeModel.GetById 81ms app.highlight 6ms RepoModel.GetById 114ms app.codeStats 0ms

/library/Zend/Translate/Adapter/Tmx.php

https://bitbucket.org/hieronim1981/tunethemusic
PHP | 234 lines | 145 code | 21 blank | 68 comment | 29 complexity | 58a9a12f3a33c9d0b4e36d0cb33cdc82 MD5 | raw file
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
 18 * @version    $Id: Tmx.php 24649 2012-02-26 03:37:54Z adamlundrigan $
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 */
 21
 22
 23/** Zend_Locale */
 24require_once 'Zend/Locale.php';
 25
 26/** Zend_Translate_Adapter */
 27require_once 'Zend/Translate/Adapter.php';
 28
 29
 30/**
 31 * @category   Zend
 32 * @package    Zend_Translate
 33 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 34 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 35 */
 36class 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 of file %s',
 78                          xml_error_string(xml_get_error_code($this->_file)),
 79                          xml_get_current_line_number($this->_file),
 80                          $filename);
 81            xml_parser_free($this->_file);
 82            require_once 'Zend/Translate/Exception.php';
 83            throw new Zend_Translate_Exception($ex);
 84        }
 85
 86        return $this->_data;
 87    }
 88
 89    /**
 90     * Internal method, called by xml element handler at start
 91     *
 92     * @param resource $file   File handler
 93     * @param string   $name   Elements name
 94     * @param array    $attrib Attributes for this element
 95     */
 96    protected function _startElement($file, $name, $attrib)
 97    {
 98        if ($this->_seg !== null) {
 99            $this->_content .= "<".$name;
100            foreach($attrib as $key => $value) {
101                $this->_content .= " $key=\"$value\"";
102            }
103            $this->_content .= ">";
104        } else {
105            switch(strtolower($name)) {
106                case 'header':
107                    if (empty($this->_useId) && isset($attrib['srclang'])) {
108                        if (Zend_Locale::isLocale($attrib['srclang'])) {
109                            $this->_srclang = Zend_Locale::findLocale($attrib['srclang']);
110                        } else {
111                            if (!$this->_options['disableNotices']) {
112                                if ($this->_options['log']) {
113                                    $this->_options['log']->notice("The language '{$attrib['srclang']}' can not be set because it does not exist.");
114                                } else {
115                                    trigger_error("The language '{$attrib['srclang']}' can not be set because it does not exist.", E_USER_NOTICE);
116                                }
117                            }
118
119                            $this->_srclang = $attrib['srclang'];
120                        }
121                    }
122                    break;
123                case 'tu':
124                    if (isset($attrib['tuid'])) {
125                        $this->_tu = $attrib['tuid'];
126                    }
127                    break;
128                case 'tuv':
129                    if (isset($attrib['xml:lang'])) {
130                        if (Zend_Locale::isLocale($attrib['xml:lang'])) {
131                            $this->_tuv = Zend_Locale::findLocale($attrib['xml:lang']);
132                        } else {
133                            if (!$this->_options['disableNotices']) {
134                                if ($this->_options['log']) {
135                                    $this->_options['log']->notice("The language '{$attrib['xml:lang']}' can not be set because it does not exist.");
136                                } else {
137                                    trigger_error("The language '{$attrib['xml:lang']}' can not be set because it does not exist.", E_USER_NOTICE);
138                                }
139                            }
140
141                            $this->_tuv = $attrib['xml:lang'];
142                        }
143
144                        if (!isset($this->_data[$this->_tuv])) {
145                            $this->_data[$this->_tuv] = array();
146                        }
147                    }
148                    break;
149                case 'seg':
150                    $this->_seg     = true;
151                    $this->_content = null;
152                    break;
153                default:
154                    break;
155            }
156        }
157    }
158
159
160    /**
161     * Internal method, called by xml element handler at end
162     *
163     * @param resource $file   File handler
164     * @param string   $name   Elements name
165     */
166    protected function _endElement($file, $name)
167    {
168        if (($this->_seg !== null) and ($name !== 'seg')) {
169            $this->_content .= "</".$name.">";
170        } else {
171            switch (strtolower($name)) {
172                case 'tu':
173                    $this->_tu = null;
174                    break;
175                case 'tuv':
176                    $this->_tuv = null;
177                    break;
178                case 'seg':
179                    $this->_seg = null;
180                    if (!empty($this->_srclang) && ($this->_srclang == $this->_tuv)) {
181                        $this->_tu = $this->_content;
182                    }
183
184                    if (!empty($this->_content) or (!isset($this->_data[$this->_tuv][$this->_tu]))) {
185                        $this->_data[$this->_tuv][$this->_tu] = $this->_content;
186                    }
187                    break;
188                default:
189                    break;
190            }
191        }
192    }
193
194    /**
195     * Internal method, called by xml element handler for content
196     *
197     * @param resource $file File handler
198     * @param string   $data Elements content
199     */
200    protected function _contentElement($file, $data)
201    {
202        if (($this->_seg !== null) and ($this->_tu !== null) and ($this->_tuv !== null)) {
203            $this->_content .= $data;
204        }
205    }
206
207
208    /**
209     * Internal method, detects the encoding of the xml file
210     *
211     * @param string $name Filename
212     * @return string Encoding
213     */
214    protected function _findEncoding($filename)
215    {
216        $file = file_get_contents($filename, null, null, 0, 100);
217        if (strpos($file, "encoding") !== false) {
218            $encoding = substr($file, strpos($file, "encoding") + 9);
219            $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
220            return $encoding;
221        }
222        return 'UTF-8';
223    }
224
225    /**
226     * Returns the adapter name
227     *
228     * @return string
229     */
230    public function toString()
231    {
232        return "Tmx";
233    }
234}