PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Translate/Adapter/Qt.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 160 lines | 99 code | 18 blank | 43 comment | 7 complexity | 00c4eeeb443514e4b51fbc71f45431e1 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Qt.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. * Load translation data (QT file reader)
  45. *
  46. * @param string $locale Locale/Language to add data for, identical with locale identifier,
  47. * see Zend_Locale for more information
  48. * @param string $filename QT file to add, full path must be given for access
  49. * @param array $option OPTIONAL Options to use
  50. * @throws Zend_Translation_Exception
  51. * @return array
  52. */
  53. protected function _loadTranslationData($filename, $locale, array $options = array())
  54. {
  55. $this->_data = array();
  56. if (!is_readable($filename)) {
  57. require_once 'Zend/Translate/Exception.php';
  58. throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
  59. }
  60. $this->_target = $locale;
  61. $encoding = $this->_findEncoding($filename);
  62. $this->_file = xml_parser_create($encoding);
  63. xml_set_object($this->_file, $this);
  64. xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
  65. xml_set_element_handler($this->_file, "_startElement", "_endElement");
  66. xml_set_character_data_handler($this->_file, "_contentElement");
  67. if (!xml_parse($this->_file, file_get_contents($filename))) {
  68. $ex = sprintf('XML error: %s at line %d',
  69. xml_error_string(xml_get_error_code($this->_file)),
  70. xml_get_current_line_number($this->_file));
  71. xml_parser_free($this->_file);
  72. require_once 'Zend/Translate/Exception.php';
  73. throw new Zend_Translate_Exception($ex);
  74. }
  75. return $this->_data;
  76. }
  77. private function _startElement($file, $name, $attrib)
  78. {
  79. switch(strtolower($name)) {
  80. case 'message':
  81. $this->_source = null;
  82. $this->_stag = false;
  83. $this->_ttag = false;
  84. $this->_scontent = null;
  85. $this->_tcontent = null;
  86. break;
  87. case 'source':
  88. $this->_stag = true;
  89. break;
  90. case 'translation':
  91. $this->_ttag = true;
  92. break;
  93. default:
  94. break;
  95. }
  96. }
  97. private function _endElement($file, $name)
  98. {
  99. switch (strtolower($name)) {
  100. case 'source':
  101. $this->_stag = false;
  102. break;
  103. case 'translation':
  104. if (!empty($this->_scontent) and !empty($this->_tcontent) or
  105. (isset($this->_data[$this->_target][$this->_scontent]) === false)) {
  106. $this->_data[$this->_target][$this->_scontent] = $this->_tcontent;
  107. }
  108. $this->_ttag = false;
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. private function _contentElement($file, $data)
  115. {
  116. if ($this->_stag === true) {
  117. $this->_scontent .= $data;
  118. }
  119. if ($this->_ttag === true) {
  120. $this->_tcontent .= $data;
  121. }
  122. }
  123. private function _findEncoding($filename)
  124. {
  125. $file = file_get_contents($filename, null, null, 0, 100);
  126. if (strpos($file, "encoding") !== false) {
  127. $encoding = substr($file, strpos($file, "encoding") + 9);
  128. $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
  129. return $encoding;
  130. }
  131. return 'UTF-8';
  132. }
  133. /**
  134. * Returns the adapter name
  135. *
  136. * @return string
  137. */
  138. public function toString()
  139. {
  140. return "Qt";
  141. }
  142. }