web/lib/Zend/Translate/Adapter/XmlTm.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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: XmlTm.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_XmlTm extends Zend_Translate_Adapter {
       
    37     // Internal variables
       
    38     private $_file        = false;
       
    39     private $_cleared     = array();
       
    40     private $_lang        = null;
       
    41     private $_content     = null;
       
    42     private $_tag         = null;
       
    43     private $_data        = array();
       
    44 
       
    45     /**
       
    46      * Load translation data (XMLTM file reader)
       
    47      *
       
    48      * @param  string  $locale    Locale/Language to add data for, identical with locale identifier,
       
    49      *                            see Zend_Locale for more information
       
    50      * @param  string  $filename  XMLTM file to add, full path must be given for access
       
    51      * @param  array   $option    OPTIONAL Options to use
       
    52      * @throws Zend_Translation_Exception
       
    53      * @return array
       
    54      */
       
    55     protected function _loadTranslationData($filename, $locale, array $options = array())
       
    56     {
       
    57         $this->_data = array();
       
    58         $this->_lang = $locale;
       
    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         switch(strtolower($name)) {
       
    86             case 'tm:tu':
       
    87                 $this->_tag     = $attrib['id'];
       
    88                 $this->_content = null;
       
    89                 break;
       
    90             default:
       
    91                 break;
       
    92         }
       
    93     }
       
    94 
       
    95     private function _endElement($file, $name)
       
    96     {
       
    97         switch (strtolower($name)) {
       
    98             case 'tm:tu':
       
    99                 if (!empty($this->_tag) and !empty($this->_content) or
       
   100                     (isset($this->_data[$this->_lang][$this->_tag]) === false)) {
       
   101                     $this->_data[$this->_lang][$this->_tag] = $this->_content;
       
   102                 }
       
   103                 $this->_tag     = null;
       
   104                 $this->_content = null;
       
   105                 break;
       
   106 
       
   107             default:
       
   108                 break;
       
   109         }
       
   110     }
       
   111 
       
   112     private function _contentElement($file, $data)
       
   113     {
       
   114         if (($this->_tag !== null)) {
       
   115             $this->_content .= $data;
       
   116         }
       
   117     }
       
   118 
       
   119     private function _findEncoding($filename)
       
   120     {
       
   121         $file = file_get_contents($filename, null, null, 0, 100);
       
   122         if (strpos($file, "encoding") !== false) {
       
   123             $encoding = substr($file, strpos($file, "encoding") + 9);
       
   124             $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
       
   125             return $encoding;
       
   126         }
       
   127         return 'UTF-8';
       
   128     }
       
   129 
       
   130     /**
       
   131      * Returns the adapter name
       
   132      *
       
   133      * @return string
       
   134      */
       
   135     public function toString()
       
   136     {
       
   137         return "XmlTm";
       
   138     }
       
   139 }