Tmx.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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-2015 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. /** Zend_Locale */
  22. #require_once 'Zend/Locale.php';
  23. /** Zend_Translate_Adapter */
  24. #require_once 'Zend/Translate/Adapter.php';
  25. /** @see Zend_Xml_Security */
  26. #require_once 'Zend/Xml/Security.php';
  27. /** @See Zend_Xml_Exception */
  28. #require_once 'Zend/Xml/Exception.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Translate
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Translate_Adapter_Tmx extends Zend_Translate_Adapter {
  36. // Internal variables
  37. private $_file = false;
  38. private $_useId = true;
  39. private $_srclang = null;
  40. private $_tu = null;
  41. private $_tuv = null;
  42. private $_seg = null;
  43. private $_content = null;
  44. private $_data = array();
  45. /**
  46. * Load translation data (TMX file reader)
  47. *
  48. * @param string $filename TMX file to add, full path must be given for access
  49. * @param string $locale Locale has no effect for TMX because TMX defines all languages within
  50. * the source file
  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 (isset($options['useId'])) {
  63. $this->_useId = (boolean) $options['useId'];
  64. }
  65. $encoding = $this->_findEncoding($filename);
  66. $this->_file = xml_parser_create($encoding);
  67. xml_set_object($this->_file, $this);
  68. xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
  69. xml_set_element_handler($this->_file, "_startElement", "_endElement");
  70. xml_set_character_data_handler($this->_file, "_contentElement");
  71. try {
  72. Zend_Xml_Security::scanFile($filename);
  73. } catch (Zend_Xml_Exception $e) {
  74. #require_once 'Zend/Translate/Exception.php';
  75. throw new Zend_Translate_Exception(
  76. $e->getMessage()
  77. );
  78. }
  79. if (!xml_parse($this->_file, file_get_contents($filename))) {
  80. $ex = sprintf('XML error: %s at line %d of file %s',
  81. xml_error_string(xml_get_error_code($this->_file)),
  82. xml_get_current_line_number($this->_file),
  83. $filename);
  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. /**
  91. * Internal method, called by xml element handler at start
  92. *
  93. * @param resource $file File handler
  94. * @param string $name Elements name
  95. * @param array $attrib Attributes for this element
  96. */
  97. protected function _startElement($file, $name, $attrib)
  98. {
  99. if ($this->_seg !== null) {
  100. $this->_content .= "<".$name;
  101. foreach($attrib as $key => $value) {
  102. $this->_content .= " $key=\"$value\"";
  103. }
  104. $this->_content .= ">";
  105. } else {
  106. switch(strtolower($name)) {
  107. case 'header':
  108. if (empty($this->_useId) && isset($attrib['srclang'])) {
  109. if (Zend_Locale::isLocale($attrib['srclang'])) {
  110. $this->_srclang = Zend_Locale::findLocale($attrib['srclang']);
  111. } else {
  112. if (!$this->_options['disableNotices']) {
  113. if ($this->_options['log']) {
  114. $this->_options['log']->notice("The language '{$attrib['srclang']}' can not be set because it does not exist.");
  115. } else {
  116. trigger_error("The language '{$attrib['srclang']}' can not be set because it does not exist.", E_USER_NOTICE);
  117. }
  118. }
  119. $this->_srclang = $attrib['srclang'];
  120. }
  121. }
  122. break;
  123. case 'tu':
  124. if (isset($attrib['tuid'])) {
  125. $this->_tu = $attrib['tuid'];
  126. }
  127. break;
  128. case 'tuv':
  129. if (isset($attrib['xml:lang'])) {
  130. if (Zend_Locale::isLocale($attrib['xml:lang'])) {
  131. $this->_tuv = Zend_Locale::findLocale($attrib['xml:lang']);
  132. } else {
  133. if (!$this->_options['disableNotices']) {
  134. if ($this->_options['log']) {
  135. $this->_options['log']->notice("The language '{$attrib['xml:lang']}' can not be set because it does not exist.");
  136. } else {
  137. trigger_error("The language '{$attrib['xml:lang']}' can not be set because it does not exist.", E_USER_NOTICE);
  138. }
  139. }
  140. $this->_tuv = $attrib['xml:lang'];
  141. }
  142. if (!isset($this->_data[$this->_tuv])) {
  143. $this->_data[$this->_tuv] = array();
  144. }
  145. }
  146. break;
  147. case 'seg':
  148. $this->_seg = true;
  149. $this->_content = null;
  150. break;
  151. default:
  152. break;
  153. }
  154. }
  155. }
  156. /**
  157. * Internal method, called by xml element handler at end
  158. *
  159. * @param resource $file File handler
  160. * @param string $name Elements name
  161. */
  162. protected function _endElement($file, $name)
  163. {
  164. if (($this->_seg !== null) and ($name !== 'seg')) {
  165. $this->_content .= "</".$name.">";
  166. } else {
  167. switch (strtolower($name)) {
  168. case 'tu':
  169. $this->_tu = null;
  170. break;
  171. case 'tuv':
  172. $this->_tuv = null;
  173. break;
  174. case 'seg':
  175. $this->_seg = null;
  176. if (!empty($this->_srclang) && ($this->_srclang == $this->_tuv)) {
  177. $this->_tu = $this->_content;
  178. }
  179. if (!empty($this->_content) or (!isset($this->_data[$this->_tuv][$this->_tu]))) {
  180. $this->_data[$this->_tuv][$this->_tu] = $this->_content;
  181. }
  182. break;
  183. default:
  184. break;
  185. }
  186. }
  187. }
  188. /**
  189. * Internal method, called by xml element handler for content
  190. *
  191. * @param resource $file File handler
  192. * @param string $data Elements content
  193. */
  194. protected function _contentElement($file, $data)
  195. {
  196. if (($this->_seg !== null) and ($this->_tu !== null) and ($this->_tuv !== null)) {
  197. $this->_content .= $data;
  198. }
  199. }
  200. /**
  201. * Internal method, detects the encoding of the xml file
  202. *
  203. * @param string $name Filename
  204. * @return string Encoding
  205. */
  206. protected function _findEncoding($filename)
  207. {
  208. $file = file_get_contents($filename, null, null, 0, 100);
  209. if (strpos($file, "encoding") !== false) {
  210. $encoding = substr($file, strpos($file, "encoding") + 9);
  211. $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
  212. return $encoding;
  213. }
  214. return 'UTF-8';
  215. }
  216. /**
  217. * Returns the adapter name
  218. *
  219. * @return string
  220. */
  221. public function toString()
  222. {
  223. return "Tmx";
  224. }
  225. }