/library/Zend/Translator/Adapter/Xliff.php

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