web/Zend/Translate/Adapter/Csv.php
changeset 0 4eba9c11703f
equal deleted inserted replaced
-1:000000000000 0:4eba9c11703f
       
     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: Csv.php 21661 2010-03-27 20:20:27Z 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-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_Csv extends Zend_Translate_Adapter
       
    37 {
       
    38     private $_data    = array();
       
    39 
       
    40     /**
       
    41      * Generates the adapter
       
    42      *
       
    43      * @param  array|Zend_Config $options Translation content
       
    44      */
       
    45     public function __construct($options = array())
       
    46     {
       
    47         $this->_options['delimiter'] = ";";
       
    48         $this->_options['length']    = 0;
       
    49         $this->_options['enclosure'] = '"';
       
    50 
       
    51         if ($options instanceof Zend_Config) {
       
    52             $options = $options->toArray();
       
    53         } else if (func_num_args() > 1) {
       
    54             $args               = func_get_args();
       
    55             $options            = array();
       
    56             $options['content'] = array_shift($args);
       
    57 
       
    58             if (!empty($args)) {
       
    59                 $options['locale'] = array_shift($args);
       
    60             }
       
    61 
       
    62             if (!empty($args)) {
       
    63                 $opt     = array_shift($args);
       
    64                 $options = array_merge($opt, $options);
       
    65             }
       
    66         } else if (!is_array($options)) {
       
    67             $options = array('content' => $options);
       
    68         }
       
    69 
       
    70         parent::__construct($options);
       
    71     }
       
    72 
       
    73     /**
       
    74      * Load translation data
       
    75      *
       
    76      * @param  string|array  $filename  Filename and full path to the translation source
       
    77      * @param  string        $locale    Locale/Language to add data for, identical with locale identifier,
       
    78      *                                  see Zend_Locale for more information
       
    79      * @param  array         $option    OPTIONAL Options to use
       
    80      * @return array
       
    81      */
       
    82     protected function _loadTranslationData($filename, $locale, array $options = array())
       
    83     {
       
    84         $this->_data = array();
       
    85         $options     = $options + $this->_options;
       
    86         $this->_file = @fopen($filename, 'rb');
       
    87         if (!$this->_file) {
       
    88             require_once 'Zend/Translate/Exception.php';
       
    89             throw new Zend_Translate_Exception('Error opening translation file \'' . $filename . '\'.');
       
    90         }
       
    91 
       
    92         while(($data = fgetcsv($this->_file, $options['length'], $options['delimiter'], $options['enclosure'])) !== false) {
       
    93             if (substr($data[0], 0, 1) === '#') {
       
    94                 continue;
       
    95             }
       
    96 
       
    97             if (!isset($data[1])) {
       
    98                 continue;
       
    99             }
       
   100 
       
   101             if (count($data) == 2) {
       
   102                 $this->_data[$locale][$data[0]] = $data[1];
       
   103             } else {
       
   104                 $singular = array_shift($data);
       
   105                 $this->_data[$locale][$singular] = $data;
       
   106             }
       
   107         }
       
   108 
       
   109         return $this->_data;
       
   110     }
       
   111 
       
   112     /**
       
   113      * returns the adapters name
       
   114      *
       
   115      * @return string
       
   116      */
       
   117     public function toString()
       
   118     {
       
   119         return "Csv";
       
   120     }
       
   121 }