Xliff.php 7.2 KB

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