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

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 
0030 /**
0031  * @category   Zend
0032  * @package    Zend_Translate
0033  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0034  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0035  */
0036 class Zend_Translate_Adapter_Csv extends Zend_Translate_Adapter
0037 {
0038     private $_data    = array();
0039 
0040     /**
0041      * Generates the adapter
0042      *
0043      * @param  array|Zend_Config $options Translation content
0044      */
0045     public function __construct($options = array())
0046     {
0047         $this->_options['delimiter'] = ";";
0048         $this->_options['length']    = 0;
0049         $this->_options['enclosure'] = '"';
0050 
0051         if ($options instanceof Zend_Config) {
0052             $options = $options->toArray();
0053         } else if (func_num_args() > 1) {
0054             $args               = func_get_args();
0055             $options            = array();
0056             $options['content'] = array_shift($args);
0057 
0058             if (!empty($args)) {
0059                 $options['locale'] = array_shift($args);
0060             }
0061 
0062             if (!empty($args)) {
0063                 $opt     = array_shift($args);
0064                 $options = array_merge($opt, $options);
0065             }
0066         } else if (!is_array($options)) {
0067             $options = array('content' => $options);
0068         }
0069 
0070         parent::__construct($options);
0071     }
0072 
0073     /**
0074      * Load translation data
0075      *
0076      * @param  string|array  $filename  Filename and full path to the translation source
0077      * @param  string        $locale    Locale/Language to add data for, identical with locale identifier,
0078      *                                  see Zend_Locale for more information
0079      * @param  array         $option    OPTIONAL Options to use
0080      * @return array
0081      */
0082     protected function _loadTranslationData($filename, $locale, array $options = array())
0083     {
0084         $this->_data = array();
0085         $options     = $options + $this->_options;
0086         $this->_file = @fopen($filename, 'rb');
0087         if (!$this->_file) {
0088             // require_once 'Zend/Translate/Exception.php';
0089             throw new Zend_Translate_Exception('Error opening translation file \'' . $filename . '\'.');
0090         }
0091 
0092         while(($data = fgetcsv($this->_file, $options['length'], $options['delimiter'], $options['enclosure'])) !== false) {
0093             if (substr($data[0], 0, 1) === '#') {
0094                 continue;
0095             }
0096 
0097             if (!isset($data[1])) {
0098                 continue;
0099             }
0100 
0101             if (count($data) == 2) {
0102                 $this->_data[$locale][$data[0]] = $data[1];
0103             } else {
0104                 $singular = array_shift($data);
0105                 $this->_data[$locale][$singular] = $data;
0106             }
0107         }
0108 
0109         return $this->_data;
0110     }
0111 
0112     /**
0113      * returns the adapters name
0114      *
0115      * @return string
0116      */
0117     public function toString()
0118     {
0119         return "Csv";
0120     }
0121 }