scopeResolver = $scopeResolver; $this->url = $url; $this->layout = $layout; $this->config = $config; $this->parser = $parser; $this->state = $state; $this->templateFileName = $templateFileName; $this->translatorRoute = $translatorRoute; $this->scope = $scope; } /** * Check if Inline Translates is allowed * * @return bool */ public function isAllowed() { if ($this->isAllowed === null) { if (!$this->scope instanceof \Magento\Framework\App\ScopeInterface) { $scope = $this->scopeResolver->getScope($this->scope); } $this->isAllowed = $this->config->isActive($scope) && $this->config->isDevAllowed($scope); } return $this->state->isEnabled() && $this->isAllowed; } /** * Retrieve Inline Parser instance * * @return Inline\ParserInterface */ public function getParser() { return $this->parser; } /** * Replace translation templates with HTML fragments * * @param array|string &$body * @param bool $isJson * @return $this */ public function processResponseBody(&$body, $isJson = false) { if (!$this->isAllowed()) { $this->stripInlineTranslations($body); return $this; } $this->getParser()->setIsJson($isJson); if (is_array($body)) { foreach ($body as &$part) { $this->processResponseBody($part, $isJson); } } elseif (is_string($body)) { $this->getParser()->processResponseBodyString($body); $this->addInlineScript(); $body = $this->getParser()->getContent(); } $this->getParser()->setIsJson(false); return $this; } /** * Additional translation mode html attribute is not needed for base inline translation. * * @param mixed|string|null $tagName * @return null * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function getAdditionalHtmlAttribute($tagName = null) { return null; } /** * Add inline script code * * Insert script and html with * added inline translation content. * * @return void */ protected function addInlineScript() { $content = $this->getParser()->getContent(); if (stripos($content, '') === false) { return; } if (!$this->isScriptInserted) { $this->getParser()->setContent(str_ireplace('', $this->getInlineScript() . '', $content)); $this->isScriptInserted = true; } } /** * Retrieve inline script code * * Create block to render script and html with * added inline translation content. * * @return string */ protected function getInlineScript() { /** @var $block \Magento\Framework\View\Element\Template */ $block = $this->layout->createBlock(\Magento\Framework\View\Element\Template::class); $block->setAjaxUrl($this->getAjaxUrl()); $block->setTemplate($this->templateFileName); return $block->toHtml(); } /** * Return URL for ajax requests * * @return string */ protected function getAjaxUrl() { return $this->url->getUrl( $this->translatorRoute, ['_secure' => $this->scopeResolver->getScope()->isCurrentlySecure()] ); } /** * Strip inline translations from text * * @param array|string &$body * @return $this */ protected function stripInlineTranslations(&$body) { if (is_array($body)) { foreach ($body as &$part) { $this->stripInlineTranslations($part); } } else { if (is_string($body)) { $body = preg_replace( '#' . \Magento\Framework\Translate\Inline\ParserInterface::REGEXP_TOKEN . '#', '$1', $body ); } } return $this; } }