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

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

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