PageRenderTime 69ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Translate/Adapter/Tbx.php

https://gitlab.com/gregtyka/SiberianCMS
PHP | 166 lines | 109 code | 14 blank | 43 comment | 14 complexity | d8592c2ccab8683c8f50ee33876912b9 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: Tbx.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_Tbx extends Zend_Translate_Adapter {
  32. // Internal variables
  33. private $_file = false;
  34. private $_cleared = array();
  35. private $_langset = null;
  36. private $_termentry = null;
  37. private $_content = null;
  38. private $_term = null;
  39. private $_data = array();
  40. /**
  41. * Load translation data (TBX file reader)
  42. *
  43. * @param string $filename TBX file to add, full path must be given for access
  44. * @param string $locale Locale has no effect for TBX because TBX defines all languages within
  45. * the source file
  46. * @param array $option OPTIONAL Options to use
  47. * @throws Zend_Translation_Exception
  48. * @return array
  49. */
  50. protected function _loadTranslationData($filename, $locale, array $options = array())
  51. {
  52. $this->_data = array();
  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. if ($this->_term !== null) {
  77. $this->_content .= "<".$name;
  78. foreach($attrib as $key => $value) {
  79. $this->_content .= " $key=\"$value\"";
  80. }
  81. $this->_content .= ">";
  82. } else {
  83. switch(strtolower($name)) {
  84. case 'termentry':
  85. $this->_termentry = null;
  86. break;
  87. case 'langset':
  88. if (isset($attrib['xml:lang']) === true) {
  89. $this->_langset = $attrib['xml:lang'];
  90. if (isset($this->_data[$this->_langset]) === false) {
  91. $this->_data[$this->_langset] = array();
  92. }
  93. }
  94. break;
  95. case 'term':
  96. $this->_term = true;
  97. $this->_content = null;
  98. break;
  99. default:
  100. break;
  101. }
  102. }
  103. }
  104. private function _endElement($file, $name)
  105. {
  106. if (($this->_term !== null) and ($name != "term")) {
  107. $this->_content .= "</".$name.">";
  108. } else {
  109. switch (strtolower($name)) {
  110. case 'langset':
  111. $this->_langset = null;
  112. break;
  113. case 'term':
  114. $this->_term = null;
  115. if (empty($this->_termentry)) {
  116. $this->_termentry = $this->_content;
  117. }
  118. if (!empty($this->_content) or (isset($this->_data[$this->_langset][$this->_termentry]) === false)) {
  119. $this->_data[$this->_langset][$this->_termentry] = $this->_content;
  120. }
  121. break;
  122. default:
  123. break;
  124. }
  125. }
  126. }
  127. private function _contentElement($file, $data)
  128. {
  129. if ($this->_term !== null) {
  130. $this->_content .= $data;
  131. }
  132. }
  133. private function _findEncoding($filename)
  134. {
  135. $file = file_get_contents($filename, null, null, 0, 100);
  136. if (strpos($file, "encoding") !== false) {
  137. $encoding = substr($file, strpos($file, "encoding") + 9);
  138. $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
  139. return $encoding;
  140. }
  141. return 'UTF-8';
  142. }
  143. /**
  144. * Returns the adapter name
  145. *
  146. * @return string
  147. */
  148. public function toString()
  149. {
  150. return "Tbx";
  151. }
  152. }