/library/Zend/Translate/Adapter/Qt.php

https://bitbucket.org/Ebozavrik/test-application · PHP · 166 lines · 102 code · 20 blank · 44 comment · 8 complexity · ddb54b8427d5d3b94e0cfd2435c26a8b 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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Qt.php 24649 2012-02-26 03:37:54Z adamlundrigan $
  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-2012 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. {
  33. // Internal variables
  34. private $_file = false;
  35. private $_cleared = array();
  36. private $_transunit = null;
  37. private $_source = null;
  38. private $_target = null;
  39. private $_scontent = null;
  40. private $_tcontent = null;
  41. private $_stag = false;
  42. private $_ttag = true;
  43. private $_data = array();
  44. /**
  45. * Load translation data (QT file reader)
  46. *
  47. * @param string $locale Locale/Language to add data for, identical with locale identifier,
  48. * see Zend_Locale for more information
  49. * @param string $filename QT file to add, full path must be given for access
  50. * @param array $option OPTIONAL Options to use
  51. *
  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. $this->_target = $locale;
  63. $encoding = $this->_findEncoding($filename);
  64. $this->_file = xml_parser_create($encoding);
  65. xml_set_object($this->_file, $this);
  66. xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
  67. xml_set_element_handler($this->_file, "_startElement", "_endElement");
  68. xml_set_character_data_handler($this->_file, "_contentElement");
  69. if (!xml_parse($this->_file, file_get_contents($filename))) {
  70. $ex = sprintf('XML error: %s at line %d of file %s',
  71. xml_error_string(xml_get_error_code($this->_file)),
  72. xml_get_current_line_number($this->_file),
  73. $filename);
  74. xml_parser_free($this->_file);
  75. require_once 'Zend/Translate/Exception.php';
  76. throw new Zend_Translate_Exception( $ex );
  77. }
  78. return $this->_data;
  79. }
  80. private function _startElement ($file, $name, $attrib)
  81. {
  82. switch (strtolower($name)) {
  83. case 'message':
  84. $this->_source = null;
  85. $this->_stag = false;
  86. $this->_ttag = false;
  87. $this->_scontent = null;
  88. $this->_tcontent = null;
  89. break;
  90. case 'source':
  91. $this->_stag = true;
  92. break;
  93. case 'translation':
  94. $this->_ttag = true;
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. private function _endElement ($file, $name)
  101. {
  102. switch (strtolower($name)) {
  103. case 'source':
  104. $this->_stag = false;
  105. break;
  106. case 'translation':
  107. if (!empty( $this->_scontent ) and !empty( $this->_tcontent ) or
  108. ( isset( $this->_data[$this->_target][$this->_scontent] ) === false )
  109. ) {
  110. $this->_data[$this->_target][$this->_scontent] = $this->_tcontent;
  111. }
  112. $this->_ttag = false;
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. private function _contentElement ($file, $data)
  119. {
  120. if ($this->_stag === true) {
  121. $this->_scontent .= $data;
  122. }
  123. if ($this->_ttag === true) {
  124. $this->_tcontent .= $data;
  125. }
  126. }
  127. private function _findEncoding ($filename)
  128. {
  129. $file = file_get_contents($filename, null, null, 0, 100);
  130. if (strpos($file, "encoding") !== false) {
  131. $encoding = substr($file, strpos($file, "encoding") + 9);
  132. $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
  133. return $encoding;
  134. }
  135. return 'UTF-8';
  136. }
  137. /**
  138. * Returns the adapter name
  139. *
  140. * @return string
  141. */
  142. public function toString ()
  143. {
  144. return "Qt";
  145. }
  146. }