PageRenderTime 46ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/concrete/libraries/3rdparty/Zend/Translate/Adapter/Tmx.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 233 lines | 144 code | 21 blank | 68 comment | 29 complexity | c2c6c37186ebc8bf08588a445e75378d 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Tmx.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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_Tmx extends Zend_Translate_Adapter {
  32. // Internal variables
  33. private $_file = false;
  34. private $_useId = true;
  35. private $_srclang = null;
  36. private $_tu = null;
  37. private $_tuv = null;
  38. private $_seg = null;
  39. private $_content = null;
  40. private $_data = array();
  41. /**
  42. * Load translation data (TMX file reader)
  43. *
  44. * @param string $filename TMX file to add, full path must be given for access
  45. * @param string $locale Locale has no effect for TMX because TMX defines all languages within
  46. * the source file
  47. * @param array $option OPTIONAL Options to use
  48. * @throws Zend_Translation_Exception
  49. * @return array
  50. */
  51. protected function _loadTranslationData($filename, $locale, array $options = array())
  52. {
  53. $this->_data = array();
  54. if (!is_readable($filename)) {
  55. require_once 'Zend/Translate/Exception.php';
  56. throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
  57. }
  58. if (isset($options['useId'])) {
  59. $this->_useId = (boolean) $options['useId'];
  60. }
  61. $encoding = $this->_findEncoding($filename);
  62. $this->_file = xml_parser_create($encoding);
  63. xml_set_object($this->_file, $this);
  64. xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
  65. xml_set_element_handler($this->_file, "_startElement", "_endElement");
  66. xml_set_character_data_handler($this->_file, "_contentElement");
  67. if (!xml_parse($this->_file, file_get_contents($filename))) {
  68. $ex = sprintf('XML error: %s at line %d',
  69. xml_error_string(xml_get_error_code($this->_file)),
  70. xml_get_current_line_number($this->_file));
  71. xml_parser_free($this->_file);
  72. require_once 'Zend/Translate/Exception.php';
  73. throw new Zend_Translate_Exception($ex);
  74. }
  75. return $this->_data;
  76. }
  77. /**
  78. * Internal method, called by xml element handler at start
  79. *
  80. * @param resource $file File handler
  81. * @param string $name Elements name
  82. * @param array $attrib Attributes for this element
  83. */
  84. protected function _startElement($file, $name, $attrib)
  85. {
  86. if ($this->_seg !== null) {
  87. $this->_content .= "<".$name;
  88. foreach($attrib as $key => $value) {
  89. $this->_content .= " $key=\"$value\"";
  90. }
  91. $this->_content .= ">";
  92. } else {
  93. switch(strtolower($name)) {
  94. case 'header':
  95. if (empty($this->_useId) && isset($attrib['srclang'])) {
  96. if (Zend_Locale::isLocale($attrib['srclang'])) {
  97. $this->_srclang = Zend_Locale::findLocale($attrib['srclang']);
  98. } else {
  99. if (!$this->_options['disableNotices']) {
  100. if ($this->_options['log']) {
  101. $this->_options['log']->notice("The language '{$attrib['srclang']}' can not be set because it does not exist.");
  102. } else {
  103. trigger_error("The language '{$attrib['srclang']}' can not be set because it does not exist.", E_USER_NOTICE);
  104. }
  105. }
  106. $this->_srclang = $attrib['srclang'];
  107. }
  108. }
  109. break;
  110. case 'tu':
  111. if (isset($attrib['tuid'])) {
  112. $this->_tu = $attrib['tuid'];
  113. }
  114. break;
  115. case 'tuv':
  116. if (isset($attrib['xml:lang'])) {
  117. if (Zend_Locale::isLocale($attrib['xml:lang'])) {
  118. $this->_tuv = Zend_Locale::findLocale($attrib['xml:lang']);
  119. } else {
  120. if (!$this->_options['disableNotices']) {
  121. if ($this->_options['log']) {
  122. $this->_options['log']->notice("The language '{$attrib['xml:lang']}' can not be set because it does not exist.");
  123. } else {
  124. trigger_error("The language '{$attrib['xml:lang']}' can not be set because it does not exist.", E_USER_NOTICE);
  125. }
  126. }
  127. $this->_tuv = $attrib['xml:lang'];
  128. }
  129. if (!isset($this->_data[$this->_tuv])) {
  130. $this->_data[$this->_tuv] = array();
  131. }
  132. }
  133. break;
  134. case 'seg':
  135. $this->_seg = true;
  136. $this->_content = null;
  137. break;
  138. default:
  139. break;
  140. }
  141. }
  142. }
  143. /**
  144. * Internal method, called by xml element handler at end
  145. *
  146. * @param resource $file File handler
  147. * @param string $name Elements name
  148. */
  149. protected function _endElement($file, $name)
  150. {
  151. if (($this->_seg !== null) and ($name !== 'seg')) {
  152. $this->_content .= "</".$name.">";
  153. } else {
  154. switch (strtolower($name)) {
  155. case 'tu':
  156. $this->_tu = null;
  157. break;
  158. case 'tuv':
  159. $this->_tuv = null;
  160. break;
  161. case 'seg':
  162. $this->_seg = null;
  163. if (!empty($this->_srclang) && ($this->_srclang == $this->_tuv)) {
  164. $this->_tu = $this->_content;
  165. }
  166. if (!empty($this->_content) or (!isset($this->_data[$this->_tuv][$this->_tu]))) {
  167. $this->_data[$this->_tuv][$this->_tu] = $this->_content;
  168. }
  169. break;
  170. default:
  171. break;
  172. }
  173. }
  174. }
  175. /**
  176. * Internal method, called by xml element handler for content
  177. *
  178. * @param resource $file File handler
  179. * @param string $data Elements content
  180. */
  181. protected function _contentElement($file, $data)
  182. {
  183. if (($this->_seg !== null) and ($this->_tu !== null) and ($this->_tuv !== null)) {
  184. $this->_content .= $data;
  185. }
  186. }
  187. /**
  188. * Internal method, detects the encoding of the xml file
  189. *
  190. * @param string $name Filename
  191. * @return string Encoding
  192. */
  193. protected function _findEncoding($filename)
  194. {
  195. $file = file_get_contents($filename, null, null, 0, 100);
  196. if (strpos($file, "encoding") !== false) {
  197. $encoding = substr($file, strpos($file, "encoding") + 9);
  198. $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
  199. return $encoding;
  200. }
  201. return 'UTF-8';
  202. }
  203. /**
  204. * Returns the adapter name
  205. *
  206. * @return string
  207. */
  208. public function toString()
  209. {
  210. return "Tmx";
  211. }
  212. }