Translator.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Model\Layout;
  7. use Magento\Framework\Simplexml\Element;
  8. class Translator
  9. {
  10. /**
  11. * Translate layout node
  12. *
  13. * @param Element $node
  14. * @param array $args
  15. * @return void
  16. **/
  17. public function translateActionParameters(Element $node, &$args)
  18. {
  19. if (false === $this->_isNodeTranslatable($node)) {
  20. return;
  21. }
  22. foreach ($this->_getNodeNamesToTranslate($node) as $translatableArg) {
  23. /*
  24. * .(dot) character is used as a path separator in nodes hierarchy
  25. * e.g. info.title means that Magento needs to translate value of <title> node
  26. * that is a child of <info> node
  27. */
  28. // @var $argumentHierarchy array - path to translatable item in $args array
  29. $argumentHierarchy = explode('.', $translatableArg);
  30. $argumentStack = & $args;
  31. $canTranslate = true;
  32. while (is_array($argumentStack) && count($argumentStack) > 0) {
  33. $argumentName = array_shift($argumentHierarchy);
  34. if (isset($argumentStack[$argumentName])) {
  35. /*
  36. * Move to the next element in arguments hierarchy
  37. * in order to find target translatable argument
  38. */
  39. $argumentStack = & $argumentStack[$argumentName];
  40. } else {
  41. // Target argument cannot be found
  42. $canTranslate = false;
  43. break;
  44. }
  45. }
  46. if ($canTranslate && is_string($argumentStack)) {
  47. // $argumentStack is now a reference to target translatable argument so it can be translated
  48. $argumentStack = $this->_translateValue($argumentStack);
  49. }
  50. }
  51. }
  52. /**
  53. * Translate argument value
  54. *
  55. * @param Element $node
  56. * @return string
  57. */
  58. public function translateArgument(Element $node)
  59. {
  60. $value = $this->_getNodeValue($node);
  61. if ($this->_isSelfTranslatable($node)) {
  62. $value = $this->_translateValue($value);
  63. } elseif ($this->_isNodeTranslatable($node->getParent())) {
  64. if (true === in_array($node->getName(), $this->_getNodeNamesToTranslate($node->getParent()))) {
  65. $value = $this->_translateValue($value);
  66. }
  67. }
  68. return $value;
  69. }
  70. /**
  71. * Get node names that have to be translated
  72. *
  73. * @param Element $node
  74. * @return array
  75. */
  76. protected function _getNodeNamesToTranslate(Element $node)
  77. {
  78. return explode(' ', (string)$node['translate']);
  79. }
  80. /**
  81. * Check if node has to be translated
  82. *
  83. * @param Element $node
  84. * @return bool
  85. */
  86. protected function _isNodeTranslatable(Element $node)
  87. {
  88. return isset($node['translate']);
  89. }
  90. /**
  91. * Check if node has to translate own value
  92. *
  93. * @param Element $node
  94. * @return bool
  95. */
  96. protected function _isSelfTranslatable(Element $node)
  97. {
  98. return $this->_isNodeTranslatable($node) && 'true' == (string)$node['translate'];
  99. }
  100. /**
  101. * Get node value
  102. *
  103. * @param Element $node
  104. * @return string
  105. */
  106. protected function _getNodeValue(Element $node)
  107. {
  108. return trim((string)$node);
  109. }
  110. /**
  111. * Translate node value
  112. *
  113. * @param string $value
  114. * @return \Magento\Framework\Phrase
  115. */
  116. protected function _translateValue($value)
  117. {
  118. return (string)new \Magento\Framework\Phrase($value);
  119. }
  120. }