PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Translate/Adapter/Xliff.php

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