Tmx.php 5.5 KB

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