PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Translate/Adapter/Tbx.php

https://github.com/manubamba/site
PHP | 179 lines | 112 code | 15 blank | 52 comment | 14 complexity | 7ec6032712756ad3dd940dbb9bd7ff95 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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Tbx.php 16971 2009-07-22 18:05:45Z mikaelkael $
  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_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. * Generates the tbx adapter
  42. * This adapter reads with php's xml_parser
  43. *
  44. * @param string $data Translation data
  45. * @param string|Zend_Locale $locale OPTIONAL Locale/Language to set, identical with locale identifier,
  46. * see Zend_Locale for more information
  47. * @param array $options OPTIONAL Options to set
  48. */
  49. public function __construct($data, $locale = null, array $options = array())
  50. {
  51. parent::__construct($data, $locale, $options);
  52. }
  53. /**
  54. * Load translation data (TBX file reader)
  55. *
  56. * @param string $filename TBX file to add, full path must be given for access
  57. * @param string $locale Locale has no effect for TBX because TBX defines all languages within
  58. * the source file
  59. * @param array $option OPTIONAL Options to use
  60. * @throws Zend_Translation_Exception
  61. * @return array
  62. */
  63. protected function _loadTranslationData($filename, $locale, array $options = array())
  64. {
  65. $this->_data = array();
  66. if (!is_readable($filename)) {
  67. require_once 'Zend/Translate/Exception.php';
  68. throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
  69. }
  70. $encoding = $this->_findEncoding($filename);
  71. $this->_file = xml_parser_create($encoding);
  72. xml_set_object($this->_file, $this);
  73. xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
  74. xml_set_element_handler($this->_file, "_startElement", "_endElement");
  75. xml_set_character_data_handler($this->_file, "_contentElement");
  76. if (!xml_parse($this->_file, file_get_contents($filename))) {
  77. $ex = sprintf('XML error: %s at line %d',
  78. xml_error_string(xml_get_error_code($this->_file)),
  79. xml_get_current_line_number($this->_file));
  80. xml_parser_free($this->_file);
  81. require_once 'Zend/Translate/Exception.php';
  82. throw new Zend_Translate_Exception($ex);
  83. }
  84. return $this->_data;
  85. }
  86. private function _startElement($file, $name, $attrib)
  87. {
  88. if ($this->_term !== null) {
  89. $this->_content .= "<".$name;
  90. foreach($attrib as $key => $value) {
  91. $this->_content .= " $key=\"$value\"";
  92. }
  93. $this->_content .= ">";
  94. } else {
  95. switch(strtolower($name)) {
  96. case 'termentry':
  97. $this->_termentry = null;
  98. break;
  99. case 'langset':
  100. if (isset($attrib['xml:lang']) === true) {
  101. $this->_langset = $attrib['xml:lang'];
  102. if (isset($this->_data[$this->_langset]) === false) {
  103. $this->_data[$this->_langset] = array();
  104. }
  105. }
  106. break;
  107. case 'term':
  108. $this->_term = true;
  109. $this->_content = null;
  110. break;
  111. default:
  112. break;
  113. }
  114. }
  115. }
  116. private function _endElement($file, $name)
  117. {
  118. if (($this->_term !== null) and ($name != "term")) {
  119. $this->_content .= "</".$name.">";
  120. } else {
  121. switch (strtolower($name)) {
  122. case 'langset':
  123. $this->_langset = null;
  124. break;
  125. case 'term':
  126. $this->_term = null;
  127. if (empty($this->_termentry)) {
  128. $this->_termentry = $this->_content;
  129. }
  130. if (!empty($this->_content) or (isset($this->_data[$this->_langset][$this->_termentry]) === false)) {
  131. $this->_data[$this->_langset][$this->_termentry] = $this->_content;
  132. }
  133. break;
  134. default:
  135. break;
  136. }
  137. }
  138. }
  139. private function _contentElement($file, $data)
  140. {
  141. if ($this->_term !== null) {
  142. $this->_content .= $data;
  143. }
  144. }
  145. private function _findEncoding($filename)
  146. {
  147. $file = file_get_contents($filename, null, null, 0, 100);
  148. if (strpos($file, "encoding") !== false) {
  149. $encoding = substr($file, strpos($file, "encoding") + 9);
  150. $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
  151. return $encoding;
  152. }
  153. return 'UTF-8';
  154. }
  155. /**
  156. * Returns the adapter name
  157. *
  158. * @return string
  159. */
  160. public function toString()
  161. {
  162. return "Tbx";
  163. }
  164. }