Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Tbx.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 $_langset = null;
46  private $_termentry = null;
47  private $_content = null;
48  private $_term = null;
49  private $_data = array();
50 
61  protected function _loadTranslationData($filename, $locale, array $options = array())
62  {
63  $this->_data = array();
64  if (!is_readable($filename)) {
65  #require_once 'Zend/Translate/Exception.php';
66  throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
67  }
68 
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 
76  try {
77  Zend_Xml_Security::scanFile($filename);
78  } catch (Zend_Xml_Exception $e) {
79  #require_once 'Zend/Translate/Exception.php';
80  throw new Zend_Translate_Exception(
81  $e->getMessage()
82  );
83  }
84 
85  if (!xml_parse($this->_file, file_get_contents($filename))) {
86  $ex = sprintf('XML error: %s at line %d of file %s',
87  xml_error_string(xml_get_error_code($this->_file)),
88  xml_get_current_line_number($this->_file),
89  $filename);
90  xml_parser_free($this->_file);
91  #require_once 'Zend/Translate/Exception.php';
92  throw new Zend_Translate_Exception($ex);
93  }
94 
95  return $this->_data;
96  }
97 
98  private function _startElement($file, $name, $attrib)
99  {
100  if ($this->_term !== null) {
101  $this->_content .= "<".$name;
102  foreach($attrib as $key => $value) {
103  $this->_content .= " $key=\"$value\"";
104  }
105  $this->_content .= ">";
106  } else {
107  switch(strtolower($name)) {
108  case 'termentry':
109  $this->_termentry = null;
110  break;
111  case 'langset':
112  if (isset($attrib['xml:lang']) === true) {
113  $this->_langset = $attrib['xml:lang'];
114  if (isset($this->_data[$this->_langset]) === false) {
115  $this->_data[$this->_langset] = array();
116  }
117  }
118  break;
119  case 'term':
120  $this->_term = true;
121  $this->_content = null;
122  break;
123  default:
124  break;
125  }
126  }
127  }
128 
129  private function _endElement($file, $name)
130  {
131  if (($this->_term !== null) and ($name != "term")) {
132  $this->_content .= "</".$name.">";
133  } else {
134  switch (strtolower($name)) {
135  case 'langset':
136  $this->_langset = null;
137  break;
138  case 'term':
139  $this->_term = null;
140  if (empty($this->_termentry)) {
141  $this->_termentry = $this->_content;
142  }
143  if (!empty($this->_content) or (isset($this->_data[$this->_langset][$this->_termentry]) === false)) {
144  $this->_data[$this->_langset][$this->_termentry] = $this->_content;
145  }
146  break;
147  default:
148  break;
149  }
150  }
151  }
152 
153  private function _contentElement($file, $data)
154  {
155  if ($this->_term !== null) {
156  $this->_content .= $data;
157  }
158  }
159 
160  private function _findEncoding($filename)
161  {
162  $file = file_get_contents($filename, null, null, 0, 100);
163  if (strpos($file, "encoding") !== false) {
164  $encoding = substr($file, strpos($file, "encoding") + 9);
165  $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
166  return $encoding;
167  }
168  return 'UTF-8';
169  }
170 
176  public function toString()
177  {
178  return "Tbx";
179  }
180 }
_loadTranslationData($filename, $locale, array $options=array())
Definition: Tbx.php:61