PageRenderTime 67ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

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

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