PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/application/libraries/Zend/Translate/Adapter/Tbx.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 165 lines | 108 code | 14 blank | 43 comment | 14 complexity | be9a23407edb9feb8a8bb0d62987a6d9 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Tbx.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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',
  65. xml_error_string(xml_get_error_code($this->_file)),
  66. xml_get_current_line_number($this->_file));
  67. xml_parser_free($this->_file);
  68. require_once 'Zend/Translate/Exception.php';
  69. throw new Zend_Translate_Exception($ex);
  70. }
  71. return $this->_data;
  72. }
  73. private function _startElement($file, $name, $attrib)
  74. {
  75. if ($this->_term !== null) {
  76. $this->_content .= "<".$name;
  77. foreach($attrib as $key => $value) {
  78. $this->_content .= " $key=\"$value\"";
  79. }
  80. $this->_content .= ">";
  81. } else {
  82. switch(strtolower($name)) {
  83. case 'termentry':
  84. $this->_termentry = null;
  85. break;
  86. case 'langset':
  87. if (isset($attrib['xml:lang']) === true) {
  88. $this->_langset = $attrib['xml:lang'];
  89. if (isset($this->_data[$this->_langset]) === false) {
  90. $this->_data[$this->_langset] = array();
  91. }
  92. }
  93. break;
  94. case 'term':
  95. $this->_term = true;
  96. $this->_content = null;
  97. break;
  98. default:
  99. break;
  100. }
  101. }
  102. }
  103. private function _endElement($file, $name)
  104. {
  105. if (($this->_term !== null) and ($name != "term")) {
  106. $this->_content .= "</".$name.">";
  107. } else {
  108. switch (strtolower($name)) {
  109. case 'langset':
  110. $this->_langset = null;
  111. break;
  112. case 'term':
  113. $this->_term = null;
  114. if (empty($this->_termentry)) {
  115. $this->_termentry = $this->_content;
  116. }
  117. if (!empty($this->_content) or (isset($this->_data[$this->_langset][$this->_termentry]) === false)) {
  118. $this->_data[$this->_langset][$this->_termentry] = $this->_content;
  119. }
  120. break;
  121. default:
  122. break;
  123. }
  124. }
  125. }
  126. private function _contentElement($file, $data)
  127. {
  128. if ($this->_term !== null) {
  129. $this->_content .= $data;
  130. }
  131. }
  132. private function _findEncoding($filename)
  133. {
  134. $file = file_get_contents($filename, null, null, 0, 100);
  135. if (strpos($file, "encoding") !== false) {
  136. $encoding = substr($file, strpos($file, "encoding") + 9);
  137. $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
  138. return $encoding;
  139. }
  140. return 'UTF-8';
  141. }
  142. /**
  143. * Returns the adapter name
  144. *
  145. * @return string
  146. */
  147. public function toString()
  148. {
  149. return "Tbx";
  150. }
  151. }