Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Tmx.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 $_useId = true;
45  private $_srclang = null;
46  private $_tu = null;
47  private $_tuv = null;
48  private $_seg = null;
49  private $_content = null;
50  private $_data = array();
51 
62  protected function _loadTranslationData($filename, $locale, array $options = array())
63  {
64  $this->_data = array();
65  if (!is_readable($filename)) {
66  #require_once 'Zend/Translate/Exception.php';
67  throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
68  }
69 
70  if (isset($options['useId'])) {
71  $this->_useId = (boolean) $options['useId'];
72  }
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 
110  protected function _startElement($file, $name, $attrib)
111  {
112  if ($this->_seg !== null) {
113  $this->_content .= "<".$name;
114  foreach($attrib as $key => $value) {
115  $this->_content .= " $key=\"$value\"";
116  }
117  $this->_content .= ">";
118  } else {
119  switch(strtolower($name)) {
120  case 'header':
121  if (empty($this->_useId) && isset($attrib['srclang'])) {
122  if (Zend_Locale::isLocale($attrib['srclang'])) {
123  $this->_srclang = Zend_Locale::findLocale($attrib['srclang']);
124  } else {
125  if (!$this->_options['disableNotices']) {
126  if ($this->_options['log']) {
127  $this->_options['log']->notice("The language '{$attrib['srclang']}' can not be set because it does not exist.");
128  } else {
129  trigger_error("The language '{$attrib['srclang']}' can not be set because it does not exist.", E_USER_NOTICE);
130  }
131  }
132 
133  $this->_srclang = $attrib['srclang'];
134  }
135  }
136  break;
137  case 'tu':
138  if (isset($attrib['tuid'])) {
139  $this->_tu = $attrib['tuid'];
140  }
141  break;
142  case 'tuv':
143  if (isset($attrib['xml:lang'])) {
144  if (Zend_Locale::isLocale($attrib['xml:lang'])) {
145  $this->_tuv = Zend_Locale::findLocale($attrib['xml:lang']);
146  } else {
147  if (!$this->_options['disableNotices']) {
148  if ($this->_options['log']) {
149  $this->_options['log']->notice("The language '{$attrib['xml:lang']}' can not be set because it does not exist.");
150  } else {
151  trigger_error("The language '{$attrib['xml:lang']}' can not be set because it does not exist.", E_USER_NOTICE);
152  }
153  }
154 
155  $this->_tuv = $attrib['xml:lang'];
156  }
157 
158  if (!isset($this->_data[$this->_tuv])) {
159  $this->_data[$this->_tuv] = array();
160  }
161  }
162  break;
163  case 'seg':
164  $this->_seg = true;
165  $this->_content = null;
166  break;
167  default:
168  break;
169  }
170  }
171  }
172 
173 
180  protected function _endElement($file, $name)
181  {
182  if (($this->_seg !== null) and ($name !== 'seg')) {
183  $this->_content .= "</".$name.">";
184  } else {
185  switch (strtolower($name)) {
186  case 'tu':
187  $this->_tu = null;
188  break;
189  case 'tuv':
190  $this->_tuv = null;
191  break;
192  case 'seg':
193  $this->_seg = null;
194  if (!empty($this->_srclang) && ($this->_srclang == $this->_tuv)) {
195  $this->_tu = $this->_content;
196  }
197 
198  if (!empty($this->_content) or (!isset($this->_data[$this->_tuv][$this->_tu]))) {
199  $this->_data[$this->_tuv][$this->_tu] = $this->_content;
200  }
201  break;
202  default:
203  break;
204  }
205  }
206  }
207 
214  protected function _contentElement($file, $data)
215  {
216  if (($this->_seg !== null) and ($this->_tu !== null) and ($this->_tuv !== null)) {
217  $this->_content .= $data;
218  }
219  }
220 
221 
228  protected function _findEncoding($filename)
229  {
230  $file = file_get_contents($filename, null, null, 0, 100);
231  if (strpos($file, "encoding") !== false) {
232  $encoding = substr($file, strpos($file, "encoding") + 9);
233  $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
234  return $encoding;
235  }
236  return 'UTF-8';
237  }
238 
244  public function toString()
245  {
246  return "Tmx";
247  }
248 }
_loadTranslationData($filename, $locale, array $options=array())
Definition: Tmx.php:62