Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
XmlTm.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 $_lang = null;
46  private $_content = null;
47  private $_tag = null;
48  private $_data = array();
49 
60  protected function _loadTranslationData($filename, $locale, array $options = array())
61  {
62  $this->_data = array();
63  $this->_lang = $locale;
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  switch(strtolower($name)) {
101  case 'tm:tu':
102  $this->_tag = $attrib['id'];
103  $this->_content = null;
104  break;
105  default:
106  break;
107  }
108  }
109 
110  private function _endElement($file, $name)
111  {
112  switch (strtolower($name)) {
113  case 'tm:tu':
114  if (!empty($this->_tag) and !empty($this->_content) or
115  (isset($this->_data[$this->_lang][$this->_tag]) === false)) {
116  $this->_data[$this->_lang][$this->_tag] = $this->_content;
117  }
118  $this->_tag = null;
119  $this->_content = null;
120  break;
121 
122  default:
123  break;
124  }
125  }
126 
127  private function _contentElement($file, $data)
128  {
129  if (($this->_tag !== null)) {
130  $this->_content .= $data;
131  }
132  }
133 
134  private function _findEncoding($filename)
135  {
136  $file = file_get_contents($filename, null, null, 0, 100);
137  if (strpos($file, "encoding") !== false) {
138  $encoding = substr($file, strpos($file, "encoding") + 9);
139  $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
140  return $encoding;
141  }
142  return 'UTF-8';
143  }
144 
150  public function toString()
151  {
152  return "XmlTm";
153  }
154 }
_loadTranslationData($filename, $locale, array $options=array())
Definition: XmlTm.php:60