PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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