/Zend/Translate/Adapter/Tbx.php

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