Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Qt.php
Go to the documentation of this file.
1 <?php
24 #require_once 'Zend/Locale.php';
25 
27 #require_once 'Zend/Translate/Adapter.php';
28 
30 #require_once 'Zend/Xml/Security.php';
31 
33 #require_once 'Zend/Xml/Exception.php';
34 
42  // Internal variables
43  private $_file = false;
44  private $_cleared = array();
45  private $_transunit = null;
46  private $_source = null;
47  private $_target = null;
48  private $_scontent = null;
49  private $_tcontent = null;
50  private $_stag = false;
51  private $_ttag = true;
52  private $_data = array();
53 
64  protected function _loadTranslationData($filename, $locale, array $options = array())
65  {
66  $this->_data = array();
67  if (!is_readable($filename)) {
68  #require_once 'Zend/Translate/Exception.php';
69  throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
70  }
71 
72  $this->_target = $locale;
73 
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 
81  try {
82  Zend_Xml_Security::scanFile($filename);
83  } catch (Zend_Xml_Exception $e) {
84  #require_once 'Zend/Translate/Exception.php';
85  throw new Zend_Translate_Exception(
86  $e->getMessage()
87  );
88  }
89 
90  if (!xml_parse($this->_file, file_get_contents($filename))) {
91  $ex = sprintf('XML error: %s at line %d of file %s',
92  xml_error_string(xml_get_error_code($this->_file)),
93  xml_get_current_line_number($this->_file),
94  $filename);
95  xml_parser_free($this->_file);
96  #require_once 'Zend/Translate/Exception.php';
97  throw new Zend_Translate_Exception($ex);
98  }
99 
100  return $this->_data;
101  }
102 
103  private function _startElement($file, $name, $attrib)
104  {
105  switch(strtolower($name)) {
106  case 'message':
107  $this->_source = null;
108  $this->_stag = false;
109  $this->_ttag = false;
110  $this->_scontent = null;
111  $this->_tcontent = null;
112  break;
113  case 'source':
114  $this->_stag = true;
115  break;
116  case 'translation':
117  $this->_ttag = true;
118  break;
119  default:
120  break;
121  }
122  }
123 
124  private function _endElement($file, $name)
125  {
126  switch (strtolower($name)) {
127  case 'source':
128  $this->_stag = false;
129  break;
130 
131  case 'translation':
132  if (!empty($this->_scontent) and !empty($this->_tcontent) or
133  (isset($this->_data[$this->_target][$this->_scontent]) === false)) {
134  $this->_data[$this->_target][$this->_scontent] = $this->_tcontent;
135  }
136  $this->_ttag = false;
137  break;
138 
139  default:
140  break;
141  }
142  }
143 
144  private function _contentElement($file, $data)
145  {
146  if ($this->_stag === true) {
147  $this->_scontent .= $data;
148  }
149 
150  if ($this->_ttag === true) {
151  $this->_tcontent .= $data;
152  }
153  }
154 
155  private function _findEncoding($filename)
156  {
157  $file = file_get_contents($filename, null, null, 0, 100);
158  if (strpos($file, "encoding") !== false) {
159  $encoding = substr($file, strpos($file, "encoding") + 9);
160  $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
161  return $encoding;
162  }
163  return 'UTF-8';
164  }
165 
171  public function toString()
172  {
173  return "Qt";
174  }
175 }
_loadTranslationData($filename, $locale, array $options=array())
Definition: Qt.php:64