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

/library/Zend/Translate/Adapter/Tmx.php

https://bitbucket.org/ksekar/campus
PHP | 234 lines | 145 code | 21 blank | 68 comment | 29 complexity | a67a293f30562bd6807464b75d03999b MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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: Tmx.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_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 of file %s',
  69. xml_error_string(xml_get_error_code($this->_file)),
  70. xml_get_current_line_number($this->_file),
  71. $filename);
  72. xml_parser_free($this->_file);
  73. require_once 'Zend/Translate/Exception.php';
  74. throw new Zend_Translate_Exception($ex);
  75. }
  76. return $this->_data;
  77. }
  78. /**
  79. * Internal method, called by xml element handler at start
  80. *
  81. * @param resource $file File handler
  82. * @param string $name Elements name
  83. * @param array $attrib Attributes for this element
  84. */
  85. protected function _startElement($file, $name, $attrib)
  86. {
  87. if ($this->_seg !== null) {
  88. $this->_content .= "<".$name;
  89. foreach($attrib as $key => $value) {
  90. $this->_content .= " $key=\"$value\"";
  91. }
  92. $this->_content .= ">";
  93. } else {
  94. switch(strtolower($name)) {
  95. case 'header':
  96. if (empty($this->_useId) && isset($attrib['srclang'])) {
  97. if (Zend_Locale::isLocale($attrib['srclang'])) {
  98. $this->_srclang = Zend_Locale::findLocale($attrib['srclang']);
  99. } else {
  100. if (!$this->_options['disableNotices']) {
  101. if ($this->_options['log']) {
  102. $this->_options['log']->notice("The language '{$attrib['srclang']}' can not be set because it does not exist.");
  103. } else {
  104. trigger_error("The language '{$attrib['srclang']}' can not be set because it does not exist.", E_USER_NOTICE);
  105. }
  106. }
  107. $this->_srclang = $attrib['srclang'];
  108. }
  109. }
  110. break;
  111. case 'tu':
  112. if (isset($attrib['tuid'])) {
  113. $this->_tu = $attrib['tuid'];
  114. }
  115. break;
  116. case 'tuv':
  117. if (isset($attrib['xml:lang'])) {
  118. if (Zend_Locale::isLocale($attrib['xml:lang'])) {
  119. $this->_tuv = Zend_Locale::findLocale($attrib['xml:lang']);
  120. } else {
  121. if (!$this->_options['disableNotices']) {
  122. if ($this->_options['log']) {
  123. $this->_options['log']->notice("The language '{$attrib['xml:lang']}' can not be set because it does not exist.");
  124. } else {
  125. trigger_error("The language '{$attrib['xml:lang']}' can not be set because it does not exist.", E_USER_NOTICE);
  126. }
  127. }
  128. $this->_tuv = $attrib['xml:lang'];
  129. }
  130. if (!isset($this->_data[$this->_tuv])) {
  131. $this->_data[$this->_tuv] = array();
  132. }
  133. }
  134. break;
  135. case 'seg':
  136. $this->_seg = true;
  137. $this->_content = null;
  138. break;
  139. default:
  140. break;
  141. }
  142. }
  143. }
  144. /**
  145. * Internal method, called by xml element handler at end
  146. *
  147. * @param resource $file File handler
  148. * @param string $name Elements name
  149. */
  150. protected function _endElement($file, $name)
  151. {
  152. if (($this->_seg !== null) and ($name !== 'seg')) {
  153. $this->_content .= "</".$name.">";
  154. } else {
  155. switch (strtolower($name)) {
  156. case 'tu':
  157. $this->_tu = null;
  158. break;
  159. case 'tuv':
  160. $this->_tuv = null;
  161. break;
  162. case 'seg':
  163. $this->_seg = null;
  164. if (!empty($this->_srclang) && ($this->_srclang == $this->_tuv)) {
  165. $this->_tu = $this->_content;
  166. }
  167. if (!empty($this->_content) or (!isset($this->_data[$this->_tuv][$this->_tu]))) {
  168. $this->_data[$this->_tuv][$this->_tu] = $this->_content;
  169. }
  170. break;
  171. default:
  172. break;
  173. }
  174. }
  175. }
  176. /**
  177. * Internal method, called by xml element handler for content
  178. *
  179. * @param resource $file File handler
  180. * @param string $data Elements content
  181. */
  182. protected function _contentElement($file, $data)
  183. {
  184. if (($this->_seg !== null) and ($this->_tu !== null) and ($this->_tuv !== null)) {
  185. $this->_content .= $data;
  186. }
  187. }
  188. /**
  189. * Internal method, detects the encoding of the xml file
  190. *
  191. * @param string $name Filename
  192. * @return string Encoding
  193. */
  194. protected function _findEncoding($filename)
  195. {
  196. $file = file_get_contents($filename, null, null, 0, 100);
  197. if (strpos($file, "encoding") !== false) {
  198. $encoding = substr($file, strpos($file, "encoding") + 9);
  199. $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
  200. return $encoding;
  201. }
  202. return 'UTF-8';
  203. }
  204. /**
  205. * Returns the adapter name
  206. *
  207. * @return string
  208. */
  209. public function toString()
  210. {
  211. return "Tmx";
  212. }
  213. }