/library/Zend/Translator/Adapter/Tmx.php

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