config = $config; $this->areaList = $areaList; $this->translate = $translate; } /** * Transform content and/or content type for the specified preprocessing chain object * * @param Chain $chain * @return void */ public function process(Chain $chain) { if ($this->config->isEmbeddedStrategy()) { $context = $chain->getAsset()->getContext(); $areaCode = \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE; if ($context instanceof FallbackContext) { $areaCode = $context->getAreaCode(); $this->translate->setLocale($context->getLocale()); } $area = $this->areaList->getArea($areaCode); $area->load(\Magento\Framework\App\Area::PART_TRANSLATE); $chain->setContent($this->translate($chain->getContent())); } } /** * Replace translation calls with translation result and return content * * @param string $content * @return string */ public function translate($content) { foreach ($this->config->getPatterns() as $pattern) { $content = preg_replace_callback($pattern, [$this, 'replaceCallback'], $content); } return $content; } /** * Replace callback for preg_replace_callback function * * @param array $matches * @return string */ protected function replaceCallback($matches) { return '"' . __($matches[1]) . '"'; } }