/library/Zend/Translator/Adapter/Qt.php

https://github.com/MarcelloDuarte/zf2 · PHP · 164 lines · 101 code · 15 blank · 48 comment · 7 complexity · 520dce7ab429772459f3a5f783830993 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_Translator
  17. * @copyright Copyright (c) 2005-2011 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\Translator\Adapter\Exception\InvalidArgumentException,
  27. Zend\Translator\Adapter\Exception\InvalidFileTypeException;
  28. /**
  29. * @uses \Zend\Locale\Locale
  30. * @uses \Zend\Translator\Adapter\Adapter
  31. * @uses \Zend\Translator\Adapter\Exception\InvalidFileTypeException
  32. * @uses \Zend\Translator\Adapter\Exception\InvalidArgumentException
  33. * @category Zend
  34. * @package Zend_Translator
  35. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class Qt extends TranslationAdapter
  39. {
  40. // Internal variables
  41. private $_file = false;
  42. private $_cleared = array();
  43. private $_transunit = null;
  44. private $_source = null;
  45. private $_target = null;
  46. private $_scontent = null;
  47. private $_tcontent = null;
  48. private $_stag = false;
  49. private $_ttag = true;
  50. private $_data = array();
  51. /**
  52. * Load translation data (QT file reader)
  53. *
  54. * @param string $locale Locale/Language to add data for, identical with locale identifier,
  55. * see Zend_Locale for more information
  56. * @param string $filename QT file to add, full path must be given for access
  57. * @param array $option OPTIONAL Options to use
  58. * @throws \Zend\Translator\Adapter\Exception\InvalidArgumentException
  59. * @throws \Zend\Translator\Adapter\Exception\InvalidFileTypeException
  60. * @return array
  61. */
  62. protected function _loadTranslationData($filename, $locale, array $options = array())
  63. {
  64. $this->_data = array();
  65. if (!is_readable($filename)) {
  66. throw new InvalidArgumentException('Translation file \'' . $filename . '\' is not readable.');
  67. }
  68. $this->_target = $locale;
  69. $encoding = $this->_findEncoding($filename);
  70. $this->_file = xml_parser_create($encoding);
  71. xml_set_object($this->_file, $this);
  72. xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
  73. xml_set_element_handler($this->_file, "_startElement", "_endElement");
  74. xml_set_character_data_handler($this->_file, "_contentElement");
  75. if (!xml_parse($this->_file, file_get_contents($filename))) {
  76. $ex = sprintf('XML error: %s at line %d',
  77. xml_error_string(xml_get_error_code($this->_file)),
  78. xml_get_current_line_number($this->_file));
  79. xml_parser_free($this->_file);
  80. throw new InvalidFileTypeException($ex);
  81. }
  82. return $this->_data;
  83. }
  84. private function _startElement($file, $name, $attrib)
  85. {
  86. switch(strtolower($name)) {
  87. case 'message':
  88. $this->_source = null;
  89. $this->_stag = false;
  90. $this->_ttag = false;
  91. $this->_scontent = null;
  92. $this->_tcontent = null;
  93. break;
  94. case 'source':
  95. $this->_stag = true;
  96. break;
  97. case 'translation':
  98. $this->_ttag = true;
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. private function _endElement($file, $name)
  105. {
  106. switch (strtolower($name)) {
  107. case 'source':
  108. $this->_stag = false;
  109. break;
  110. case 'translation':
  111. if (!empty($this->_scontent) and !empty($this->_tcontent) or
  112. (isset($this->_data[$this->_target][$this->_scontent]) === false)) {
  113. $this->_data[$this->_target][$this->_scontent] = $this->_tcontent;
  114. }
  115. $this->_ttag = false;
  116. break;
  117. default:
  118. break;
  119. }
  120. }
  121. private function _contentElement($file, $data)
  122. {
  123. if ($this->_stag === true) {
  124. $this->_scontent .= $data;
  125. }
  126. if ($this->_ttag === true) {
  127. $this->_tcontent .= $data;
  128. }
  129. }
  130. private function _findEncoding($filename)
  131. {
  132. $file = file_get_contents($filename, null, null, 0, 100);
  133. if (strpos($file, "encoding") !== false) {
  134. $encoding = substr($file, strpos($file, "encoding") + 9);
  135. $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
  136. return $encoding;
  137. }
  138. return 'UTF-8';
  139. }
  140. /**
  141. * Returns the adapter name
  142. *
  143. * @return string
  144. */
  145. public function toString()
  146. {
  147. return "Qt";
  148. }
  149. }