Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Xml.php
Go to the documentation of this file.
1 <?php
8 
16 class Xml extends AbstractAdapter
17 {
21  protected function _parse()
22  {
23  foreach ($this->_getNodes($this->_file) as $element) {
24  if (!$element instanceof \SimpleXMLElement) {
25  continue;
26  }
27  $attributes = $element->attributes();
28  if ((string)$attributes['translate'] === 'true' || (string)$attributes['translatable'] === 'true') {
29  $this->_addPhrase((string)$element);
30  } else {
31  $this->parseTranslatableNodes($attributes, $element);
32  }
33  }
34  }
35 
42  protected function _getNodes($file)
43  {
44  libxml_use_internal_errors(true);
45  $xml = simplexml_load_file($file);
46  libxml_use_internal_errors(false);
47  if ($xml) {
48  $nodes = $xml->xpath('//*[@translate|@translatable]');
49  unset($xml);
50 
51  return is_array($nodes) ? $nodes : [];
52  }
53 
54  return [];
55  }
56 
64  private function parseTranslatableNodes(\SimpleXMLElement $attributes, \SimpleXMLElement $element)
65  {
66  $nodesDelimiter = strpos($attributes['translate'], ' ') === false ? ',' : ' ';
67  foreach (explode($nodesDelimiter, $attributes['translate']) as $value) {
68  $phrase = trim((string)$element->{$value});
69  if ($phrase) {
70  $this->_addPhrase($phrase);
71  }
72  $elementAttributes = $element->attributes();
73  if (isset($elementAttributes[$value])) {
74  $phrase = (string)$elementAttributes[$value];
75  if ($phrase) {
76  $this->_addPhrase($phrase);
77  }
78  }
79  }
80  }
81 }
$value
Definition: gender.phtml:16
$attributes
Definition: matrix.phtml:13
$element
Definition: element.phtml:12