Xliff.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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-2015 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. /** @see Zend_Xml_Security */
  26. #require_once 'Zend/Xml/Security.php';
  27. /** @See Zend_Xml_Exception */
  28. #require_once 'Zend/Xml/Exception.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Translate
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Translate_Adapter_Xliff extends Zend_Translate_Adapter {
  36. // Internal variables
  37. private $_file = false;
  38. private $_useId = true;
  39. private $_cleared = array();
  40. private $_transunit = null;
  41. private $_source = null;
  42. private $_target = null;
  43. private $_langId = null;
  44. private $_scontent = null;
  45. private $_tcontent = null;
  46. private $_stag = false;
  47. private $_ttag = false;
  48. private $_data = array();
  49. /**
  50. * Load translation data (XLIFF file reader)
  51. *
  52. * @param string $locale Locale/Language to add data for, identical with locale identifier,
  53. * see Zend_Locale for more information
  54. * @param string $filename XLIFF file to add, full path must be given for access
  55. * @param array $option OPTIONAL Options to use
  56. * @throws Zend_Translation_Exception
  57. * @return array
  58. */
  59. protected function _loadTranslationData($filename, $locale, array $options = array())
  60. {
  61. $this->_data = array();
  62. if (!is_readable($filename)) {
  63. #require_once 'Zend/Translate/Exception.php';
  64. throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
  65. }
  66. if (empty($options['useId'])) {
  67. $this->_useId = false;
  68. } else {
  69. $this->_useId = true;
  70. }
  71. $encoding = $this->_findEncoding($filename);
  72. $this->_target = $locale;
  73. $this->_file = xml_parser_create($encoding);
  74. xml_set_object($this->_file, $this);
  75. xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
  76. xml_set_element_handler($this->_file, "_startElement", "_endElement");
  77. xml_set_character_data_handler($this->_file, "_contentElement");
  78. try {
  79. Zend_Xml_Security::scanFile($filename);
  80. } catch (Zend_Xml_Exception $e) {
  81. #require_once 'Zend/Translate/Exception.php';
  82. throw new Zend_Translate_Exception(
  83. $e->getMessage()
  84. );
  85. }
  86. if (!xml_parse($this->_file, file_get_contents($filename))) {
  87. $ex = sprintf('XML error: %s at line %d of file %s',
  88. xml_error_string(xml_get_error_code($this->_file)),
  89. xml_get_current_line_number($this->_file),
  90. $filename);
  91. xml_parser_free($this->_file);
  92. #require_once 'Zend/Translate/Exception.php';
  93. throw new Zend_Translate_Exception($ex);
  94. }
  95. return $this->_data;
  96. }
  97. private function _startElement($file, $name, $attrib)
  98. {
  99. if ($this->_stag === true) {
  100. $this->_scontent .= "<".$name;
  101. foreach($attrib as $key => $value) {
  102. $this->_scontent .= " $key=\"$value\"";
  103. }
  104. $this->_scontent .= ">";
  105. } else if ($this->_ttag === true) {
  106. $this->_tcontent .= "<".$name;
  107. foreach($attrib as $key => $value) {
  108. $this->_tcontent .= " $key=\"$value\"";
  109. }
  110. $this->_tcontent .= ">";
  111. } else {
  112. switch(strtolower($name)) {
  113. case 'file':
  114. $this->_source = $attrib['source-language'];
  115. if (isset($attrib['target-language'])) {
  116. $this->_target = $attrib['target-language'];
  117. }
  118. if (!isset($this->_data[$this->_source])) {
  119. $this->_data[$this->_source] = array();
  120. }
  121. if (!isset($this->_data[$this->_target])) {
  122. $this->_data[$this->_target] = array();
  123. }
  124. break;
  125. case 'trans-unit':
  126. $this->_transunit = true;
  127. $this->_langId = $attrib['id'];
  128. break;
  129. case 'source':
  130. if ($this->_transunit === true) {
  131. $this->_scontent = null;
  132. $this->_stag = true;
  133. $this->_ttag = false;
  134. }
  135. break;
  136. case 'target':
  137. if ($this->_transunit === true) {
  138. $this->_tcontent = null;
  139. $this->_ttag = true;
  140. $this->_stag = false;
  141. }
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. }
  148. private function _endElement($file, $name)
  149. {
  150. if (($this->_stag === true) and ($name !== 'source')) {
  151. $this->_scontent .= "</".$name.">";
  152. } else if (($this->_ttag === true) and ($name !== 'target')) {
  153. $this->_tcontent .= "</".$name.">";
  154. } else {
  155. switch (strtolower($name)) {
  156. case 'trans-unit':
  157. $this->_transunit = null;
  158. $this->_langId = null;
  159. $this->_scontent = null;
  160. $this->_tcontent = null;
  161. break;
  162. case 'source':
  163. if ($this->_useId) {
  164. if (!empty($this->_scontent) && !empty($this->_langId) &&
  165. !isset($this->_data[$this->_source][$this->_langId])) {
  166. $this->_data[$this->_source][$this->_langId] = $this->_scontent;
  167. }
  168. } else {
  169. if (!empty($this->_scontent) &&
  170. !isset($this->_data[$this->_source][$this->_scontent])) {
  171. $this->_data[$this->_source][$this->_scontent] = $this->_scontent;
  172. }
  173. }
  174. $this->_stag = false;
  175. break;
  176. case 'target':
  177. if ($this->_useId) {
  178. if (!empty($this->_tcontent) && !empty($this->_langId) &&
  179. !isset($this->_data[$this->_target][$this->_langId])) {
  180. $this->_data[$this->_target][$this->_langId] = $this->_tcontent;
  181. }
  182. } else {
  183. if (!empty($this->_tcontent) && !empty($this->_scontent) &&
  184. !isset($this->_data[$this->_target][$this->_scontent])) {
  185. $this->_data[$this->_target][$this->_scontent] = $this->_tcontent;
  186. }
  187. }
  188. $this->_ttag = false;
  189. break;
  190. default:
  191. break;
  192. }
  193. }
  194. }
  195. private function _contentElement($file, $data)
  196. {
  197. if (($this->_transunit !== null) and ($this->_source !== null) and ($this->_stag === true)) {
  198. $this->_scontent .= $data;
  199. }
  200. if (($this->_transunit !== null) and ($this->_target !== null) and ($this->_ttag === true)) {
  201. $this->_tcontent .= $data;
  202. }
  203. }
  204. private function _findEncoding($filename)
  205. {
  206. $file = file_get_contents($filename, null, null, 0, 100);
  207. if (strpos($file, "encoding") !== false) {
  208. $encoding = substr($file, strpos($file, "encoding") + 9);
  209. $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
  210. return $encoding;
  211. }
  212. return 'UTF-8';
  213. }
  214. /**
  215. * Returns the adapter name
  216. *
  217. * @return string
  218. */
  219. public function toString()
  220. {
  221. return "Xliff";
  222. }
  223. }