2
0

Qt.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Date.php 2498 2006-12-23 22:13:38Z thomas $
  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-2008 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_Qt extends Zend_Translate_Adapter {
  32. // Internal variables
  33. private $_file = false;
  34. private $_cleared = array();
  35. private $_transunit = null;
  36. private $_source = null;
  37. private $_target = null;
  38. private $_scontent = null;
  39. private $_tcontent = null;
  40. private $_stag = false;
  41. private $_ttag = true;
  42. private $_data = array();
  43. /**
  44. * Generates the Qt adapter
  45. * This adapter reads with php's xml_parser
  46. *
  47. * @param string $data Translation data
  48. * @param string|Zend_Locale $locale OPTIONAL Locale/Language to set, identical with locale identifier,
  49. * see Zend_Locale for more information
  50. * @param array $options OPTIONAL Options to set
  51. */
  52. public function __construct($data, $locale = null, array $options = array())
  53. {
  54. parent::__construct($data, $locale, $options);
  55. }
  56. /**
  57. * Load translation data (QT file reader)
  58. *
  59. * @param string $locale Locale/Language to add data for, identical with locale identifier,
  60. * see Zend_Locale for more information
  61. * @param string $filename QT file to add, full path must be given for access
  62. * @param array $option OPTIONAL Options to use
  63. * @throws Zend_Translation_Exception
  64. * @return array
  65. */
  66. protected function _loadTranslationData($filename, $locale, array $options = array())
  67. {
  68. $this->_data = array();
  69. if (!is_readable($filename)) {
  70. require_once 'Zend/Translate/Exception.php';
  71. throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
  72. }
  73. $this->_target = $locale;
  74. $encoding = $this->_findEncoding($filename);
  75. $this->_file = xml_parser_create($encoding);
  76. xml_set_object($this->_file, $this);
  77. xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
  78. xml_set_element_handler($this->_file, "_startElement", "_endElement");
  79. xml_set_character_data_handler($this->_file, "_contentElement");
  80. if (!xml_parse($this->_file, file_get_contents($filename))) {
  81. $ex = sprintf('XML error: %s at line %d',
  82. xml_error_string(xml_get_error_code($this->_file)),
  83. xml_get_current_line_number($this->_file));
  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. private function _startElement($file, $name, $attrib)
  91. {
  92. switch(strtolower($name)) {
  93. case 'message':
  94. $this->_source = null;
  95. $this->_stag = false;
  96. $this->_ttag = false;
  97. $this->_scontent = null;
  98. $this->_tcontent = null;
  99. break;
  100. case 'source':
  101. $this->_stag = true;
  102. break;
  103. case 'translation':
  104. $this->_ttag = true;
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. private function _endElement($file, $name)
  111. {
  112. switch (strtolower($name)) {
  113. case 'source':
  114. $this->_stag = false;
  115. break;
  116. case 'translation':
  117. if (!empty($this->_scontent) and !empty($this->_tcontent) or
  118. (isset($this->_data[$this->_target][$this->_scontent]) === false)) {
  119. $this->_data[$this->_target][$this->_scontent] = $this->_tcontent;
  120. }
  121. $this->_ttag = false;
  122. break;
  123. default:
  124. break;
  125. }
  126. }
  127. private function _contentElement($file, $data)
  128. {
  129. if ($this->_stag === true) {
  130. $this->_scontent .= $data;
  131. }
  132. if ($this->_ttag === true) {
  133. $this->_tcontent .= $data;
  134. }
  135. }
  136. private function _findEncoding($filename)
  137. {
  138. $file = file_get_contents($filename, null, null, 0, 100);
  139. if (strpos($file, "encoding") !== false) {
  140. $encoding = substr($file, strpos($file, "encoding") + 9);
  141. $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
  142. return $encoding;
  143. }
  144. return 'UTF-8';
  145. }
  146. /**
  147. * Returns the adapter name
  148. *
  149. * @return string
  150. */
  151. public function toString()
  152. {
  153. return "Qt";
  154. }
  155. }