/libraries/Zend/Translator/Adapter/Qt.php

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