File indexing completed on 2025-03-09 05:26:23

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Translate
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @version    $Id$
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  */
0021 
0022 
0023 /** Zend_Locale */
0024 // require_once 'Zend/Locale.php';
0025 
0026 /** Zend_Translate_Adapter */
0027 // require_once 'Zend/Translate/Adapter.php';
0028 
0029 /** @see Zend_Xml_Security */
0030 // require_once 'Zend/Xml/Security.php';
0031 
0032 /** @See Zend_Xml_Exception */
0033 // require_once 'Zend/Xml/Exception.php';
0034 
0035 /**
0036  * @category   Zend
0037  * @package    Zend_Translate
0038  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0039  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0040  */
0041 class Zend_Translate_Adapter_Tmx extends Zend_Translate_Adapter {
0042     // Internal variables
0043     private $_file    = false;
0044     private $_useId   = true;
0045     private $_srclang = null;
0046     private $_tu      = null;
0047     private $_tuv     = null;
0048     private $_seg     = null;
0049     private $_content = null;
0050     private $_data    = array();
0051 
0052     /**
0053      * Load translation data (TMX file reader)
0054      *
0055      * @param  string  $filename  TMX file to add, full path must be given for access
0056      * @param  string  $locale    Locale has no effect for TMX because TMX defines all languages within
0057      *                            the source file
0058      * @param  array   $option    OPTIONAL Options to use
0059      * @throws Zend_Translation_Exception
0060      * @return array
0061      */
0062     protected function _loadTranslationData($filename, $locale, array $options = array())
0063     {
0064         $this->_data = array();
0065         if (!is_readable($filename)) {
0066             // require_once 'Zend/Translate/Exception.php';
0067             throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
0068         }
0069 
0070         if (isset($options['useId'])) {
0071             $this->_useId = (boolean) $options['useId'];
0072         }
0073 
0074         $encoding = $this->_findEncoding($filename);
0075         $this->_file = xml_parser_create($encoding);
0076         xml_set_object($this->_file, $this);
0077         xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
0078         xml_set_element_handler($this->_file, "_startElement", "_endElement");
0079         xml_set_character_data_handler($this->_file, "_contentElement");
0080 
0081         try {
0082             Zend_Xml_Security::scanFile($filename);
0083         } catch (Zend_Xml_Exception $e) {
0084             // require_once 'Zend/Translate/Exception.php';
0085             throw new Zend_Translate_Exception(
0086                 $e->getMessage()
0087             );
0088         }
0089  
0090         if (!xml_parse($this->_file, file_get_contents($filename))) {
0091             $ex = sprintf('XML error: %s at line %d of file %s',
0092                           xml_error_string(xml_get_error_code($this->_file)),
0093                           xml_get_current_line_number($this->_file),
0094                           $filename);
0095             xml_parser_free($this->_file);
0096             // require_once 'Zend/Translate/Exception.php';
0097             throw new Zend_Translate_Exception($ex);
0098         }
0099 
0100         return $this->_data;
0101     }
0102 
0103     /**
0104      * Internal method, called by xml element handler at start
0105      *
0106      * @param resource $file   File handler
0107      * @param string   $name   Elements name
0108      * @param array    $attrib Attributes for this element
0109      */
0110     protected function _startElement($file, $name, $attrib)
0111     {
0112         if ($this->_seg !== null) {
0113             $this->_content .= "<".$name;
0114             foreach($attrib as $key => $value) {
0115                 $this->_content .= " $key=\"$value\"";
0116             }
0117             $this->_content .= ">";
0118         } else {
0119             switch(strtolower($name)) {
0120                 case 'header':
0121                     if (empty($this->_useId) && isset($attrib['srclang'])) {
0122                         if (Zend_Locale::isLocale($attrib['srclang'])) {
0123                             $this->_srclang = Zend_Locale::findLocale($attrib['srclang']);
0124                         } else {
0125                             if (!$this->_options['disableNotices']) {
0126                                 if ($this->_options['log']) {
0127                                     $this->_options['log']->notice("The language '{$attrib['srclang']}' can not be set because it does not exist.");
0128                                 } else {
0129                                     trigger_error("The language '{$attrib['srclang']}' can not be set because it does not exist.", E_USER_NOTICE);
0130                                 }
0131                             }
0132 
0133                             $this->_srclang = $attrib['srclang'];
0134                         }
0135                     }
0136                     break;
0137                 case 'tu':
0138                     if (isset($attrib['tuid'])) {
0139                         $this->_tu = $attrib['tuid'];
0140                     }
0141                     break;
0142                 case 'tuv':
0143                     if (isset($attrib['xml:lang'])) {
0144                         if (Zend_Locale::isLocale($attrib['xml:lang'])) {
0145                             $this->_tuv = Zend_Locale::findLocale($attrib['xml:lang']);
0146                         } else {
0147                             if (!$this->_options['disableNotices']) {
0148                                 if ($this->_options['log']) {
0149                                     $this->_options['log']->notice("The language '{$attrib['xml:lang']}' can not be set because it does not exist.");
0150                                 } else {
0151                                     trigger_error("The language '{$attrib['xml:lang']}' can not be set because it does not exist.", E_USER_NOTICE);
0152                                 }
0153                             }
0154 
0155                             $this->_tuv = $attrib['xml:lang'];
0156                         }
0157 
0158                         if (!isset($this->_data[$this->_tuv])) {
0159                             $this->_data[$this->_tuv] = array();
0160                         }
0161                     }
0162                     break;
0163                 case 'seg':
0164                     $this->_seg     = true;
0165                     $this->_content = null;
0166                     break;
0167                 default:
0168                     break;
0169             }
0170         }
0171     }
0172 
0173 
0174     /**
0175      * Internal method, called by xml element handler at end
0176      *
0177      * @param resource $file   File handler
0178      * @param string   $name   Elements name
0179      */
0180     protected function _endElement($file, $name)
0181     {
0182         if (($this->_seg !== null) and ($name !== 'seg')) {
0183             $this->_content .= "</".$name.">";
0184         } else {
0185             switch (strtolower($name)) {
0186                 case 'tu':
0187                     $this->_tu = null;
0188                     break;
0189                 case 'tuv':
0190                     $this->_tuv = null;
0191                     break;
0192                 case 'seg':
0193                     $this->_seg = null;
0194                     if (!empty($this->_srclang) && ($this->_srclang == $this->_tuv)) {
0195                         $this->_tu = $this->_content;
0196                     }
0197 
0198                     if (!empty($this->_content) or (!isset($this->_data[$this->_tuv][$this->_tu]))) {
0199                         $this->_data[$this->_tuv][$this->_tu] = $this->_content;
0200                     }
0201                     break;
0202                 default:
0203                     break;
0204             }
0205         }
0206     }
0207 
0208     /**
0209      * Internal method, called by xml element handler for content
0210      *
0211      * @param resource $file File handler
0212      * @param string   $data Elements content
0213      */
0214     protected function _contentElement($file, $data)
0215     {
0216         if (($this->_seg !== null) and ($this->_tu !== null) and ($this->_tuv !== null)) {
0217             $this->_content .= $data;
0218         }
0219     }
0220 
0221 
0222     /**
0223      * Internal method, detects the encoding of the xml file
0224      *
0225      * @param string $name Filename
0226      * @return string Encoding
0227      */
0228     protected function _findEncoding($filename)
0229     {
0230         $file = file_get_contents($filename, null, null, 0, 100);
0231         if (strpos($file, "encoding") !== false) {
0232             $encoding = substr($file, strpos($file, "encoding") + 9);
0233             $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
0234             return $encoding;
0235         }
0236         return 'UTF-8';
0237     }
0238 
0239     /**
0240      * Returns the adapter name
0241      *
0242      * @return string
0243      */
0244     public function toString()
0245     {
0246         return "Tmx";
0247     }
0248 }