/pimcore/lib/Zend/Translate/Adapter/XmlTm.php

https://github.com/timglabisch/pimcore · PHP · 140 lines · 78 code · 15 blank · 47 comment · 6 complexity · 078f46fa3cb80ac6d9b61d8c0d22b0a5 MD5 · raw file

  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: XmlTm.php 24649 2012-02-26 03:37:54Z adamlundrigan $
  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-2012 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_XmlTm extends Zend_Translate_Adapter {
  32. // Internal variables
  33. private $_file = false;
  34. private $_cleared = array();
  35. private $_lang = null;
  36. private $_content = null;
  37. private $_tag = null;
  38. private $_data = array();
  39. /**
  40. * Load translation data (XMLTM file reader)
  41. *
  42. * @param string $locale Locale/Language to add data for, identical with locale identifier,
  43. * see Zend_Locale for more information
  44. * @param string $filename XMLTM file to add, full path must be given for access
  45. * @param array $option OPTIONAL Options to use
  46. * @throws Zend_Translation_Exception
  47. * @return array
  48. */
  49. protected function _loadTranslationData($filename, $locale, array $options = array())
  50. {
  51. $this->_data = array();
  52. $this->_lang = $locale;
  53. if (!is_readable($filename)) {
  54. // require_once 'Zend/Translate/Exception.php';
  55. throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
  56. }
  57. $encoding = $this->_findEncoding($filename);
  58. $this->_file = xml_parser_create($encoding);
  59. xml_set_object($this->_file, $this);
  60. xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
  61. xml_set_element_handler($this->_file, "_startElement", "_endElement");
  62. xml_set_character_data_handler($this->_file, "_contentElement");
  63. if (!xml_parse($this->_file, file_get_contents($filename))) {
  64. $ex = sprintf('XML error: %s at line %d of file %s',
  65. xml_error_string(xml_get_error_code($this->_file)),
  66. xml_get_current_line_number($this->_file),
  67. $filename);
  68. xml_parser_free($this->_file);
  69. // require_once 'Zend/Translate/Exception.php';
  70. throw new Zend_Translate_Exception($ex);
  71. }
  72. return $this->_data;
  73. }
  74. private function _startElement($file, $name, $attrib)
  75. {
  76. switch(strtolower($name)) {
  77. case 'tm:tu':
  78. $this->_tag = $attrib['id'];
  79. $this->_content = null;
  80. break;
  81. default:
  82. break;
  83. }
  84. }
  85. private function _endElement($file, $name)
  86. {
  87. switch (strtolower($name)) {
  88. case 'tm:tu':
  89. if (!empty($this->_tag) and !empty($this->_content) or
  90. (isset($this->_data[$this->_lang][$this->_tag]) === false)) {
  91. $this->_data[$this->_lang][$this->_tag] = $this->_content;
  92. }
  93. $this->_tag = null;
  94. $this->_content = null;
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. private function _contentElement($file, $data)
  101. {
  102. if (($this->_tag !== null)) {
  103. $this->_content .= $data;
  104. }
  105. }
  106. private function _findEncoding($filename)
  107. {
  108. $file = file_get_contents($filename, null, null, 0, 100);
  109. if (strpos($file, "encoding") !== false) {
  110. $encoding = substr($file, strpos($file, "encoding") + 9);
  111. $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
  112. return $encoding;
  113. }
  114. return 'UTF-8';
  115. }
  116. /**
  117. * Returns the adapter name
  118. *
  119. * @return string
  120. */
  121. public function toString()
  122. {
  123. return "XmlTm";
  124. }
  125. }