PageRenderTime 64ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 229 lines | 163 code | 19 blank | 47 comment | 32 complexity | 7110c11588d46fdedeaf77b9c7c3f4ef MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Xliff.php 20096 2010-01-06 02:05:09Z bkarwin $
  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-2010 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',
  76. xml_error_string(xml_get_error_code($this->_file)),
  77. xml_get_current_line_number($this->_file));
  78. xml_parser_free($this->_file);
  79. #require_once 'Zend/Translate/Exception.php';
  80. throw new Zend_Translate_Exception($ex);
  81. }
  82. return $this->_data;
  83. }
  84. private function _startElement($file, $name, $attrib)
  85. {
  86. if ($this->_stag === true) {
  87. $this->_scontent .= "<".$name;
  88. foreach($attrib as $key => $value) {
  89. $this->_scontent .= " $key=\"$value\"";
  90. }
  91. $this->_scontent .= ">";
  92. } else if ($this->_ttag === true) {
  93. $this->_tcontent .= "<".$name;
  94. foreach($attrib as $key => $value) {
  95. $this->_tcontent .= " $key=\"$value\"";
  96. }
  97. $this->_tcontent .= ">";
  98. } else {
  99. switch(strtolower($name)) {
  100. case 'file':
  101. $this->_source = $attrib['source-language'];
  102. if (isset($attrib['target-language'])) {
  103. $this->_target = $attrib['target-language'];
  104. }
  105. if (!isset($this->_data[$this->_source])) {
  106. $this->_data[$this->_source] = array();
  107. }
  108. if (!isset($this->_data[$this->_target])) {
  109. $this->_data[$this->_target] = array();
  110. }
  111. break;
  112. case 'trans-unit':
  113. $this->_transunit = true;
  114. $this->_langId = $attrib['id'];
  115. break;
  116. case 'source':
  117. if ($this->_transunit === true) {
  118. $this->_scontent = null;
  119. $this->_stag = true;
  120. $this->_ttag = false;
  121. }
  122. break;
  123. case 'target':
  124. if ($this->_transunit === true) {
  125. $this->_tcontent = null;
  126. $this->_ttag = true;
  127. $this->_stag = false;
  128. }
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. }
  135. private function _endElement($file, $name)
  136. {
  137. if (($this->_stag === true) and ($name !== 'source')) {
  138. $this->_scontent .= "</".$name.">";
  139. } else if (($this->_ttag === true) and ($name !== 'target')) {
  140. $this->_tcontent .= "</".$name.">";
  141. } else {
  142. switch (strtolower($name)) {
  143. case 'trans-unit':
  144. $this->_transunit = null;
  145. $this->_langId = null;
  146. $this->_scontent = null;
  147. $this->_tcontent = null;
  148. break;
  149. case 'source':
  150. if ($this->_useId) {
  151. if (!empty($this->_scontent) && !empty($this->_langId) &&
  152. !isset($this->_data[$this->_source][$this->_langId])) {
  153. $this->_data[$this->_source][$this->_langId] = $this->_scontent;
  154. }
  155. } else {
  156. if (!empty($this->_scontent) &&
  157. !isset($this->_data[$this->_source][$this->_scontent])) {
  158. $this->_data[$this->_source][$this->_scontent] = $this->_scontent;
  159. }
  160. }
  161. $this->_stag = false;
  162. break;
  163. case 'target':
  164. if ($this->_useId) {
  165. if (!empty($this->_tcontent) && !empty($this->_langId) &&
  166. !isset($this->_data[$this->_target][$this->_langId])) {
  167. $this->_data[$this->_target][$this->_langId] = $this->_tcontent;
  168. }
  169. } else {
  170. if (!empty($this->_tcontent) && !empty($this->_scontent) &&
  171. !isset($this->_data[$this->_target][$this->_scontent])) {
  172. $this->_data[$this->_target][$this->_scontent] = $this->_tcontent;
  173. }
  174. }
  175. $this->_ttag = false;
  176. break;
  177. default:
  178. break;
  179. }
  180. }
  181. }
  182. private function _contentElement($file, $data)
  183. {
  184. if (($this->_transunit !== null) and ($this->_source !== null) and ($this->_stag === true)) {
  185. $this->_scontent .= $data;
  186. }
  187. if (($this->_transunit !== null) and ($this->_target !== null) and ($this->_ttag === true)) {
  188. $this->_tcontent .= $data;
  189. }
  190. }
  191. private function _findEncoding($filename)
  192. {
  193. $file = file_get_contents($filename, null, null, 0, 100);
  194. if (strpos($file, "encoding") !== false) {
  195. $encoding = substr($file, strpos($file, "encoding") + 9);
  196. $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
  197. return $encoding;
  198. }
  199. return 'UTF-8';
  200. }
  201. /**
  202. * Returns the adapter name
  203. *
  204. * @return string
  205. */
  206. public function toString()
  207. {
  208. return "Xliff";
  209. }
  210. }