Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Xliff.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 $_cleared = array();
46  private $_transunit = null;
47  private $_source = null;
48  private $_target = null;
49  private $_langId = null;
50  private $_scontent = null;
51  private $_tcontent = null;
52  private $_stag = false;
53  private $_ttag = false;
54  private $_data = array();
55 
66  protected function _loadTranslationData($filename, $locale, array $options = array())
67  {
68  $this->_data = array();
69  if (!is_readable($filename)) {
70  #require_once 'Zend/Translate/Exception.php';
71  throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
72  }
73 
74  if (empty($options['useId'])) {
75  $this->_useId = false;
76  } else {
77  $this->_useId = true;
78  }
79 
80  $encoding = $this->_findEncoding($filename);
81  $this->_target = $locale;
82  $this->_file = xml_parser_create($encoding);
83  xml_set_object($this->_file, $this);
84  xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
85  xml_set_element_handler($this->_file, "_startElement", "_endElement");
86  xml_set_character_data_handler($this->_file, "_contentElement");
87 
88  try {
89  Zend_Xml_Security::scanFile($filename);
90  } catch (Zend_Xml_Exception $e) {
91  #require_once 'Zend/Translate/Exception.php';
92  throw new Zend_Translate_Exception(
93  $e->getMessage()
94  );
95  }
96 
97  if (!xml_parse($this->_file, file_get_contents($filename))) {
98  $ex = sprintf('XML error: %s at line %d of file %s',
99  xml_error_string(xml_get_error_code($this->_file)),
100  xml_get_current_line_number($this->_file),
101  $filename);
102  xml_parser_free($this->_file);
103  #require_once 'Zend/Translate/Exception.php';
104  throw new Zend_Translate_Exception($ex);
105  }
106 
107  return $this->_data;
108  }
109 
110  private function _startElement($file, $name, $attrib)
111  {
112  if ($this->_stag === true) {
113  $this->_scontent .= "<".$name;
114  foreach($attrib as $key => $value) {
115  $this->_scontent .= " $key=\"$value\"";
116  }
117  $this->_scontent .= ">";
118  } else if ($this->_ttag === true) {
119  $this->_tcontent .= "<".$name;
120  foreach($attrib as $key => $value) {
121  $this->_tcontent .= " $key=\"$value\"";
122  }
123  $this->_tcontent .= ">";
124  } else {
125  switch(strtolower($name)) {
126  case 'file':
127  $this->_source = $attrib['source-language'];
128  if (isset($attrib['target-language'])) {
129  $this->_target = $attrib['target-language'];
130  }
131 
132  if (!isset($this->_data[$this->_source])) {
133  $this->_data[$this->_source] = array();
134  }
135 
136  if (!isset($this->_data[$this->_target])) {
137  $this->_data[$this->_target] = array();
138  }
139 
140  break;
141  case 'trans-unit':
142  $this->_transunit = true;
143  $this->_langId = $attrib['id'];
144  break;
145  case 'source':
146  if ($this->_transunit === true) {
147  $this->_scontent = null;
148  $this->_stag = true;
149  $this->_ttag = false;
150  }
151  break;
152  case 'target':
153  if ($this->_transunit === true) {
154  $this->_tcontent = null;
155  $this->_ttag = true;
156  $this->_stag = false;
157  }
158  break;
159  default:
160  break;
161  }
162  }
163  }
164 
165  private function _endElement($file, $name)
166  {
167  if (($this->_stag === true) and ($name !== 'source')) {
168  $this->_scontent .= "</".$name.">";
169  } else if (($this->_ttag === true) and ($name !== 'target')) {
170  $this->_tcontent .= "</".$name.">";
171  } else {
172  switch (strtolower($name)) {
173  case 'trans-unit':
174  $this->_transunit = null;
175  $this->_langId = null;
176  $this->_scontent = null;
177  $this->_tcontent = null;
178  break;
179  case 'source':
180  if ($this->_useId) {
181  if (!empty($this->_scontent) && !empty($this->_langId) &&
182  !isset($this->_data[$this->_source][$this->_langId])) {
183  $this->_data[$this->_source][$this->_langId] = $this->_scontent;
184  }
185  } else {
186  if (!empty($this->_scontent) &&
187  !isset($this->_data[$this->_source][$this->_scontent])) {
188  $this->_data[$this->_source][$this->_scontent] = $this->_scontent;
189  }
190  }
191  $this->_stag = false;
192  break;
193  case 'target':
194  if ($this->_useId) {
195  if (!empty($this->_tcontent) && !empty($this->_langId) &&
196  !isset($this->_data[$this->_target][$this->_langId])) {
197  $this->_data[$this->_target][$this->_langId] = $this->_tcontent;
198  }
199  } else {
200  if (!empty($this->_tcontent) && !empty($this->_scontent) &&
201  !isset($this->_data[$this->_target][$this->_scontent])) {
202  $this->_data[$this->_target][$this->_scontent] = $this->_tcontent;
203  }
204  }
205  $this->_ttag = false;
206  break;
207  default:
208  break;
209  }
210  }
211  }
212 
213  private function _contentElement($file, $data)
214  {
215  if (($this->_transunit !== null) and ($this->_source !== null) and ($this->_stag === true)) {
216  $this->_scontent .= $data;
217  }
218 
219  if (($this->_transunit !== null) and ($this->_target !== null) and ($this->_ttag === true)) {
220  $this->_tcontent .= $data;
221  }
222  }
223 
224  private function _findEncoding($filename)
225  {
226  $file = file_get_contents($filename, null, null, 0, 100);
227  if (strpos($file, "encoding") !== false) {
228  $encoding = substr($file, strpos($file, "encoding") + 9);
229  $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
230  return $encoding;
231  }
232  return 'UTF-8';
233  }
234 
240  public function toString()
241  {
242  return "Xliff";
243  }
244 }
_loadTranslationData($filename, $locale, array $options=array())
Definition: Xliff.php:66