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

https://bitbucket.org/blackriver/openx · PHP · 174 lines · 106 code · 17 blank · 51 comment · 15 complexity · b270befce8390aa7ad792a4b3a9e805b 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Date.php 2498 2006-12-23 22:13:38Z thomas $
  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-2008 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 $_defined = false;
  39. private $_term = null;
  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. */
  62. protected function _loadTranslationData($filename, $locale, array $options = array())
  63. {
  64. $options = array_merge($this->_options, $options);
  65. if ($options['clear']) {
  66. $this->_translate = array();
  67. }
  68. if ((in_array('defined_language', $options)) and !empty($options['defined_language'])) {
  69. $this->_defined = true;
  70. }
  71. if (!is_readable($filename)) {
  72. require_once 'Zend/Translate/Exception.php';
  73. throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
  74. }
  75. $this->_file = xml_parser_create();
  76. xml_set_object($this->_file, $this);
  77. xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
  78. xml_set_element_handler($this->_file, "_startElement", "_endElement");
  79. xml_set_character_data_handler($this->_file, "_contentElement");
  80. if (!xml_parse($this->_file, file_get_contents($filename))) {
  81. $ex = sprintf('XML error: %s at line %d',
  82. xml_error_string(xml_get_error_code($this->_file)),
  83. xml_get_current_line_number($this->_file));
  84. xml_parser_free($this->_file);
  85. require_once 'Zend/Translate/Exception.php';
  86. throw new Zend_Translate_Exception($ex);
  87. }
  88. }
  89. private function _startElement($file, $name, $attrib)
  90. {
  91. if ($this->_term !== null) {
  92. $this->_content .= "<".$name;
  93. foreach($attrib as $key => $value) {
  94. $this->_content .= " $key=\"$value\"";
  95. }
  96. $this->_content .= ">";
  97. } else {
  98. switch(strtolower($name)) {
  99. case 'termentry':
  100. $this->_termentry = null;
  101. break;
  102. case 'langset':
  103. if (array_key_exists('xml:lang', $attrib)) {
  104. $this->_langset = $attrib['xml:lang'];
  105. if (!array_key_exists($this->_langset, $this->_translate)) {
  106. $this->_translate[$this->_langset] = array();
  107. }
  108. }
  109. break;
  110. case 'term':
  111. $this->_term = true;
  112. $this->_content = null;
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. }
  119. private function _endElement($file, $name)
  120. {
  121. if (($this->_term !== null) and ($name != "term")) {
  122. $this->_content .= "</".$name.">";
  123. } else {
  124. switch (strtolower($name)) {
  125. case 'langset':
  126. $this->_langset = null;
  127. break;
  128. case 'term':
  129. $this->_term = null;
  130. if (empty($this->_termentry)) {
  131. $this->_termentry = $this->_content;
  132. }
  133. if (!empty($this->_content) or !array_key_exists($this->_termentry, $this->_translate[$this->_langset])) {
  134. $this->_translate[$this->_langset][$this->_termentry] = $this->_content;
  135. }
  136. break;
  137. default:
  138. break;
  139. }
  140. }
  141. }
  142. private function _contentElement($file, $data)
  143. {
  144. if ($this->_term !== null) {
  145. $this->_content .= $data;
  146. }
  147. }
  148. /**
  149. * Returns the adapter name
  150. *
  151. * @return string
  152. */
  153. public function toString()
  154. {
  155. return "Tbx";
  156. }
  157. }