/Zend/Translate/Adapter/XmlTm.php

https://github.com/ftaiolivista/Zend-Framework-Namespaced- · PHP · 144 lines · 83 code · 15 blank · 46 comment · 6 complexity · f18e46ab3a40a7e9bcda0da32b154219 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-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. * @namespace
  23. */
  24. namespace Zend\Translate\Adapter;
  25. use Zend\Translate;
  26. /** Zend_Locale */
  27. require_once 'Zend/Locale.php';
  28. /** Zend_Translate_Adapter */
  29. require_once 'Zend/Translate/Adapter.php';
  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 XmlTm extends 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. * Load translation data (XMLTM file reader)
  46. *
  47. * @param string $locale Locale/Language to add data for, identical with locale identifier,
  48. * see Zend_Locale for more information
  49. * @param string $filename XMLTM file to add, full path must be given for access
  50. * @param array $option OPTIONAL Options to use
  51. * @throws Zend_Translation_Exception
  52. * @return array
  53. */
  54. protected function _loadTranslationData($filename, $locale, array $options = array())
  55. {
  56. $this->_data = array();
  57. $this->_lang = $locale;
  58. if (!is_readable($filename)) {
  59. require_once 'Zend/Translate/Exception.php';
  60. throw new Translate\Exception('Translation file \'' . $filename . '\' is not readable.');
  61. }
  62. $encoding = $this->_findEncoding($filename);
  63. $this->_file = xml_parser_create($encoding);
  64. xml_set_object($this->_file, $this);
  65. xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
  66. xml_set_element_handler($this->_file, "_startElement", "_endElement");
  67. xml_set_character_data_handler($this->_file, "_contentElement");
  68. if (!xml_parse($this->_file, file_get_contents($filename))) {
  69. $ex = sprintf('XML error: %s at line %d',
  70. xml_error_string(xml_get_error_code($this->_file)),
  71. xml_get_current_line_number($this->_file));
  72. xml_parser_free($this->_file);
  73. require_once 'Zend/Translate/Exception.php';
  74. throw new Translate\Exception($ex);
  75. }
  76. return $this->_data;
  77. }
  78. private function _startElement($file, $name, $attrib)
  79. {
  80. switch(strtolower($name)) {
  81. case 'tm:tu':
  82. $this->_tag = $attrib['id'];
  83. $this->_content = null;
  84. break;
  85. default:
  86. break;
  87. }
  88. }
  89. private function _endElement($file, $name)
  90. {
  91. switch (strtolower($name)) {
  92. case 'tm:tu':
  93. if (!empty($this->_tag) and !empty($this->_content) or
  94. (isset($this->_data[$this->_lang][$this->_tag]) === false)) {
  95. $this->_data[$this->_lang][$this->_tag] = $this->_content;
  96. }
  97. $this->_tag = null;
  98. $this->_content = null;
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. private function _contentElement($file, $data)
  105. {
  106. if (($this->_tag !== null)) {
  107. $this->_content .= $data;
  108. }
  109. }
  110. private function _findEncoding($filename)
  111. {
  112. $file = file_get_contents($filename, null, null, 0, 100);
  113. if (strpos($file, "encoding") !== false) {
  114. $encoding = substr($file, strpos($file, "encoding") + 9);
  115. $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
  116. return $encoding;
  117. }
  118. return 'UTF-8';
  119. }
  120. /**
  121. * Returns the adapter name
  122. *
  123. * @return string
  124. */
  125. public function toString()
  126. {
  127. return "XmlTm";
  128. }
  129. }