Csv.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id$
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Locale */
  22. require_once 'Zend/Locale.php';
  23. /** Zend_Translate_Adapter */
  24. require_once 'Zend/Translate/Adapter.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Translate
  28. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Translate_Adapter_Csv extends Zend_Translate_Adapter
  32. {
  33. private $_data = array();
  34. /**
  35. * Generates the adapter
  36. *
  37. * @param string $data OPTIONAL Translation data
  38. * @param string|Zend_Locale $locale OPTIONAL Locale/Language to set, identical with locale identifier,
  39. * see Zend_Locale for more information
  40. * @param array $options OPTIONAL Options for this adapter
  41. */
  42. public function __construct($data = null, $locale = null, array $options = array())
  43. {
  44. $this->_options['delimiter'] = ";";
  45. $this->_options['length'] = 0;
  46. $this->_options['enclosure'] = '"';
  47. parent::__construct($data, $locale, $options);
  48. }
  49. /**
  50. * Load translation data
  51. *
  52. * @param string|array $filename Filename and full path to the translation source
  53. * @param string $locale Locale/Language to add data for, identical with locale identifier,
  54. * see Zend_Locale for more information
  55. * @param array $option OPTIONAL Options to use
  56. * @return array
  57. */
  58. protected function _loadTranslationData($filename, $locale, array $options = array())
  59. {
  60. $this->_data = array();
  61. $options = $options + $this->_options;
  62. $this->_file = @fopen($filename, 'rb');
  63. if (!$this->_file) {
  64. require_once 'Zend/Translate/Exception.php';
  65. throw new Zend_Translate_Exception('Error opening translation file \'' . $filename . '\'.');
  66. }
  67. while(($data = fgetcsv($this->_file, $options['length'], $options['delimiter'], $options['enclosure'])) !== false) {
  68. if (substr($data[0], 0, 1) === '#') {
  69. continue;
  70. }
  71. if (!isset($data[1])) {
  72. continue;
  73. }
  74. if (count($data) == 2) {
  75. $this->_data[$locale][$data[0]] = $data[1];
  76. } else {
  77. $singular = array_shift($data);
  78. $this->_data[$locale][$singular] = $data;
  79. }
  80. }
  81. return $this->_data;
  82. }
  83. /**
  84. * returns the adapters name
  85. *
  86. * @return string
  87. */
  88. public function toString()
  89. {
  90. return "Csv";
  91. }
  92. }