Tmx.php 6.1 KB

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