PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

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