Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Translator.php
Go to the documentation of this file.
1 <?php
7 
9 
11 {
19  public function translateActionParameters(Element $node, &$args)
20  {
21  if (false === $this->_isNodeTranslatable($node)) {
22  return;
23  }
24 
25  foreach ($this->_getNodeNamesToTranslate($node) as $translatableArg) {
26  /*
27  * .(dot) character is used as a path separator in nodes hierarchy
28  * e.g. info.title means that Magento needs to translate value of <title> node
29  * that is a child of <info> node
30  */
31  // @var $argumentHierarchy array - path to translatable item in $args array
32  $argumentHierarchy = explode('.', $translatableArg);
33  $argumentStack = & $args;
34  $canTranslate = true;
35  while (is_array($argumentStack) && count($argumentStack) > 0) {
36  $argumentName = array_shift($argumentHierarchy);
37  if (isset($argumentStack[$argumentName])) {
38  /*
39  * Move to the next element in arguments hierarchy
40  * in order to find target translatable argument
41  */
42  $argumentStack = & $argumentStack[$argumentName];
43  } else {
44  // Target argument cannot be found
45  $canTranslate = false;
46  break;
47  }
48  }
49  if ($canTranslate && is_string($argumentStack)) {
50  // $argumentStack is now a reference to target translatable argument so it can be translated
51  $argumentStack = $this->_translateValue($argumentStack);
52  }
53  }
54  }
55 
62  public function translateArgument(Element $node)
63  {
64  $value = $this->_getNodeValue($node);
65 
66  if ($this->_isSelfTranslatable($node)) {
67  $value = $this->_translateValue($value);
68  } elseif ($this->_isNodeTranslatable($node->getParent())) {
69  if (true === in_array($node->getName(), $this->_getNodeNamesToTranslate($node->getParent()))) {
70  $value = $this->_translateValue($value);
71  }
72  }
73 
74  return $value;
75  }
76 
83  protected function _getNodeNamesToTranslate(Element $node)
84  {
85  return explode(' ', (string)$node['translate']);
86  }
87 
94  protected function _isNodeTranslatable(Element $node)
95  {
96  return isset($node['translate']);
97  }
98 
105  protected function _isSelfTranslatable(Element $node)
106  {
107  return $this->_isNodeTranslatable($node) && 'true' == (string)$node['translate'];
108  }
109 
116  protected function _getNodeValue(Element $node)
117  {
118  return trim((string)$node);
119  }
120 
127  protected function _translateValue($value)
128  {
129  return (string)new \Magento\Framework\Phrase($value);
130  }
131 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
translateActionParameters(Element $node, &$args)
Definition: Translator.php:19
$value
Definition: gender.phtml:16