PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Translate/Adapter/Xliff.php

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